Integrates PHP applications and Solr search engines. Why do you need a search engine? Is database alone insufficient? If you only create a small website, the database is enough. But when you create a neutral or large application, the search engine is more clear why do you need a search engine? Is database alone insufficient? If you only create a small website, the database is enough. However, when you create a neutral or large application, search engines are the smartest choice. Of course, a small website can also use Solr to obtain highly correlated search results.
Imagine you are writing a search and query program for an e-commerce website. The most direct idea is the following database query statement:
SELECT * FROM PRODUCTSWHERE LOWER(title) like LOWER('%$phrase%')OR LOWER(description) like LOWER('%$phrase%');
It works normally when querying the phrase in the title or description. But the real thing is complicated, for example, Apple iPhone 4G black 16 GB (Apple 4G network iPhone black 16 GB ). When searching for the "iPhone 16G", there is no result. You can replace the space with % to cope with this situation.
$phrase = str_replace(' ', '%', $phrase);
When querying the "iPhone 16 GB 4G? Obviously, the word order has changed and it cannot work normally. I guess you will add another field to save the word order. What should I do if I write the wrong word? What about synonyms? It is very challenging to think of a good solution for such a search system.
Designing an exquisite algorithm is not the key to solving such problems. text search requires resource consumption. putting too much pressure on the database is never a good solution. the reason is that the database cannot be easily expanded. you cannot simply add an instance like web server or Memcached. the extended database requires some preparation, code modification, configuration, and down machine maintenance time. In short, the cost is very expensive. the good news is that Solr is dedicated to solving such problems.
Solr is an enterprise-level search platform based on Apache Lucene. fast and stable. there are good documents, of course, which can be easily expanded. since Solr has a powerful solution, all its features are not listed in this article. this guy collects it easily.
First download the latest version from the official website. Solr is an application written in the Java language. you need a Java Runtime environment to run it.
$ cd solr-4.1.0/example/$ java -jar start.jar
In a few seconds, you can see the following information:
2013-03-09 18:47:41.177:INFO:oejs.AbstractConnector:Started SocketConnector@0.0.0.0:8983
Solr has a web interface working on port 8983. open a browser to access http: // localhost: 8983/solr /.
In the left-hand navigation area, you will find "collection1 ″. In Solr, Collections is similar to a database table. you can query data. Click a collection and select her sub-menu "query ".
The first option is "Request-Handler (qt)" with the default value "/select ". Request handlers is a set of predefined queries. if you view the Solr config file, you will see the following:
$ vim solr-4.1.0/example/solr/collection1/conf/solrconfig.xml
explicit
10
text
The second parameter is what we are most interested in. The default value "*: *" means to query any content. if you click "execute query", you can get the following content:
0
1
true
*:*
xml
The index result is null, but this is not a problem. you need to insert some sample data.
$ cd solr-4.1.0/example/exampledocs/$ java -jar post.jar monitor.xml SimplePostTool version 1.5Posting files to base url http://localhost:8983/solr/update using content-type application/xml..POSTing file monitor.xml1 files indexed.COMMITting Solr index changes to http://localhost:8983/solr/update..
Now you can return to the query interface, and a document will be returned this time.
The data structure of Collection is defined in the schema file.
$ vim solr-4.1.0/example/solr/collection1/conf/schema.xml
This file has a lot of annotations, so you can easily tell what they do. If you want to modify the scheme file, do not delete the field "text" (if there is no good reason). it is associated with other fields and query statements (including select, look and so on ).
$ grep text solr-4.1.0/example/solr/collection1/conf/schema.xml | grep copy
If you are using a relational database, you do not want duplicate data. Solr is not a database, and most fields are also processed as text fields. the default request handler is like this.
Accessing Solr from PHP requires a client. I suggest you download one from PECL. It is fast, with clear APIs and excellent documentation. Note that the current version 1.0.2 does not support Solr 4.x. The Solr 3. x and 4. x protocols are slightly different. But don't worry. I have made some changes. you can download the compatible libraries from https://github.com/lukaszkujawa/php-pecl-solr. I have used it for a while and it is reliable. It is a little different from the official one in the SolrClient constructor. This patch will be released in official versions, so you don't have to worry about future compatibility.
$ git clone https://github.com/lukaszkujawa/php-pecl-solr.git$ cd php-pecl-solr/$ phpize$ whereis php-configphp-config: /usr/bin/php-config /usr/bin/X11/php-config$ ./configure --with-php-config=/usr/bin/php-config$ make$ make install
Add to Your php. ini
extension=solr.so
Restart the Web server
$ /etc/init.d/apache2 restart
Now you can write php to add content to the index.
'2017. 0.0.1 ',); $ client = new SolrClient ($ options, "4.0"); // parameter 4.0 for Solr4.x. $ doc = new SolrInputDocument () is ignored in other versions (); $ doc-> addField ('id', 100); $ doc-> addField ('title', 'Hello Wolrd '); $ doc-> addField ('Description ', 'example document'); $ doc-> addField ('cat', 'Foo'); $ doc-> addField ('cat', 'bar '); $ response = $ client-> addDocument ($ doc); $ client-> commit ();/* ------------------------------- */$ query = new SolrQuery (); $ query-> setQuery ('Hello'); $ query-> addField ('id')-> addField ('title')-> addField ('Description ') -> addField ('cat'); $ queryResponse = $ client-> query ($ query); $ response = $ queryResponse-> getResponse (); print_r ($ response-> docs );
If you add more than one document, she can handle it well without frequent commit.
Knowing how Solr works is very valuable and you can use it in many projects. She has a great feature that allows you to pull all the required data in one request. Of course, it takes some time to master her, but it is worth your effort. Solr has an active community and complete documentation resources. if you are still worried about using it in the project, please read Solr 3's Enterprise Search Server. it is not just for you to quickly build a search service, it is also the basis of your data mining.
Related articles:
Php solr operation class and demo
Install php-solr extension
How does one install and configure solr + php?