How to install and configure solr + php
Problem
How to install and configure solr + php
Solution 1:
Solr provides an http request query interface. The client retrieves json, xml, and other data formats by triggering an http request, and parses and displays the data. Generally, various languages have encapsulated client plug-ins, such as java solrj, python solrpy, and php-solr-client, which are indexed and queried based on the provided api. For the installation and configuration of solr, you can read this book solr cookbook.
Solution 2:
I was confused at the beginning for the php program to use solr for search.
What if I don't know java?
How to create an index for the search system?
How does php write code?
Solr is written in java and is a server program with a management interface that provides an http interface
In addition to the configuration file, you do not need to write java code. php has solr extensions.
Therefore, it is relatively simple.
1. install solr and verify that you have installed it.
Install Solr5.3 in CentOS
Solr5.3 is already a separate server, including the web management background.
You only need to ensure that your linux has jdk
Download solr5.3 package directly, Apache Download Mirrors
Be sure to download solr5.3.tgz, do not download the src package, and decompress the package to obtain a directory similar to solr5.3.
The most critical two points for installation 1. you can run an install script in the solr directory without decompression, and specify the data directory (to store the index) 2. solr has a core concept, it can be understood as a database. you can create a core, which also has a script, but ensure that the data directory is the permission of the solr user.
# Solr-5.3.0/bin/install_solr_service.sh solr-5.3.0.tgz-d/data/solr-I/usr/local
-I indicates where you install the solr server
# Su-solr-c "/usr/local/solr/bin/solr create-c corename
Create your own core
Service solr start service
Http: // your ip address: 8983/solr. this is the management interface.
2. design your own search index
Http: // your ip address: 8983/solr. this is the management interface. select the core
You only need to pay attention to add Documents and query. you can add an index to a query task, which is particularly easy to understand.
The add Documents interface has an example. two field IDs and titles are indexed. you can modify the data submission.
Query interface q = *: * Query all
Q = title: a * query
Easy to understand
Now I have id and title. I want to add a field of my own. what should I do with type?
Go to the data directory grep "title" and find the configuration items of id and title, similar:
Refer to enter type below.
Service solr stop/start restart service
Return to the management page and add data. assume that you want to search for the title and type jointly.
Q = title: xx fq = type: xx. add another fq.
3. write a php program
After 1 or 2 studies, data can be basically created, and a php program can be written to find data to complete these functions.
Reference: PHP: Solr-Manual
SolrInputDocument input document
SolrClient: the user connects to the solr server.
SolrQuery: this is the condition for building search.
Directly program:
Class Search {private $ client; public function _ construct () {$ options = ['hostname' => 'localhost ', // solr server ip 'path' => 'solr/corename', // solves the core problem, corename is your core 'WT '=> 'json',]; $ this-> client = new \ SolrClient ($ options);} public function addIndex ($ id, $ type, $ content) {// The Index adding function $ doc = new \ SolrInputDocument (); $ doc-> addField ('id', $ type. ". ". $ id); // ensure that the id is unique. I added $ doc-> addField ('typ E ', $ type); $ doc-> addField ('title', $ content); $ client = $ this-> client; $ updateResponse = $ client-> addDocument ($ doc); $ client-> commit (); // commit is required to take effect immediately $ ret = ($ updateResponse-> getResponse (); if (isset ($ ret-> responseHeader ['status']) {return $ ret-> responseHeader ['status'] = 0? True: false;} return false;} public function search ($ key, $ type = 0, $ page = 0, $ limit = 15) {$ query = new \ SolrQuery (); $ query-> setQuery ('Title :'. $ key. "*"); // set keyword $ query-> setStart ($ page); // paginated $ query-> setRows ($ limit); if ($ type) {$ query-> addFilterQuery ('type :'. $ type); // fq used} $ query-> addField ('id')-> addField ('title')-> addField ('type '); // what fields do you want to check? $ client = $ this-> client; $ query_respo Neuron = $ client-> query ($ query); $ response = $ query_response-> getResponse (); $ response = ($ response-> response ); if (is_array ($ response-> docs) {foreach ($ response-> docs as & $ doc) {$ id = explode (". ", $ doc-> id); if (isset ($ id [1]) {$ doc-> id = $ id [1];} $ doc-> type = isset ($ doc-> type [0])? $ Doc-> type [0]: '';} return ($ response );}}
Related articles:
Php solr operation class and demo
Install php-solr extension
Integrate PHP applications and Solr search engines