SOLR
1. Solr server setup
LJava environment setup
Download linux JDK 6 from this website:
Http://java.sun.com/javase/downloads/index.jsp
After installing JDK, edit/ect/profile, add these code to the end of the file
JAVA_HOME =/usr/java/jdk1.6.0 _ 16
PATH = $ JAVA_HOME/bin: $ PATH
CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar
Export JAVA_HOME
Export PATH
Export CLASSPATH
/Usr/java/jdk1.6.0 _ 16 is the folder of the jdk. You should change it, if you don't install jdk in this folder.
LSolr setup
1.Download solr (apache-solr-1.3.0.zip ) from this website:
Http://ftp.kddilabs.jp/infosystems/apache/lucene/solr/
2. Install solr with following steps
#unzip -q apache-solr-1.3.0.zip
#cd apache-solr-1.3.0/example/
# java -jar start.jar
we can see that the Solr is running by loading http://localhost:8983/solr/admin/ in web browser. This is the main starting point for Administering Solr.
This is tutorial of solr http://lucene.apache.org/solr/tutorial.html.
2. Search Apach solr with php.
This is a tutorial of php solr client example:
Http://www.ibm.com/developerworks/opensource/library/os-php-apachesolr/
We use PHP Solr Client to access to solr server. Download PHP Solr Client from this website: http://code.google.com/p/solr-php-client/downloads/list
LChange default Solr index data schema.
Solr index data schema is in the folder of "apache-solr-1.3.0 \ example \ solr \ conf \ schema. xml"
This is the snippet of solr schema.
<schema name="example" version="1.1">
...
<fields>
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="sku" type="textTight" indexed="true" stored="true" omitNorms="true"/>
<field name="name" type="text" indexed="true" stored="true"/>
...
</fields>
<uniqueKey>id</uniqueKey>
...
<defaultSearchField>text</defaultSearchField>
...
</schema>
Edit the field element, change it as below:
<Field name = "id" type = "string" indexed = "true" stored = "true" required = "true"/>
<Field name = "product_name" type = "text" indexed = "true" stored = "true"/>
<Defasearchsearchfield> product_name </defasearchsearchfield>
To make this change active, we have to restart Solr server as command like this:
# Java-jar start. jar
LCreate index by PHP
Using php solr client, we can access to Solr easily. This is an example fo how to create an index by php.
<? Php
Require_once 'apache/Solr/Service. php ';
// 10.60.0.111 is solr service ip.
$ Solr = new Apache_Solr_Service ('10. 60.0.111 ', '123','/solr ');
If (! $ Solr-> ping ())
{
Echo ("service not responding ");
}
Else
{
Echo ("solr Service is available <br/> ");
}
$ Parts = array (
'1' => array (
'Id' => 'a123 ',
'Product _ name' => 'garoon, Test'
),
'2' => array (
'Id' => 'a456 ',
'Product _ name' => '0000360, Test'
)
);
$ Documents = array ();
Foreach ($ parts as $ item => $ fields ){
$ Part = new Apache_Solr_Document ();
Foreach ($ fields as $ key => $ value ){
If (is_array ($ value )){
Foreach ($ value as $ datum ){
$ Part-> setMultiValue ($ key, $ datum );
}
}
Else {
$ Part-> $ key = $ value;
}
}
$ Documents [] = $ part;
}
Try {
$ Solr-> addDocuments ($ documents );
$ Solr-> commit ();
$ Solr-> optimize ();
}
Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
?>
LSearch index by PHP.
This is an example of searching index by php
<? Php
Require_once 'apache/Solr/Service. php ';
$ Solr = new Apache_Solr_Service ('10. 60.0.111 ', '123','/solr ');
If (! $ Solr-> ping ())
{
Echo ("service not responding ");
}
Else
{
Echo ("sucess ");
}
$ Offset = 0;
$ Limit = 10;
$ Query = "garoon ";
$ Response = $ solr-> search ($ query, $ offset, $ limit );
If ($ response-> getHttpStatus () = 200)
{
If ($ response-> numFound> 0 ){
Echo "$ query <br/> ";
Foreach ($ response-> docs as $ doc)
{
Echo "id:". $ doc-> id. "product_name". $ doc-> product_name ."--";
Echo '<br/> ';
}
Echo '<br/> ';
}
}
Else {
Echo $ response-> getHttpStatusMessage ();
}
?>
LDelete index by PHP
<? Php
Require_once 'apache/Solr/Service. php ';
// 10.60.0.111 is solr service ip.
$ Solr = new Apache_Solr_Service ('10. 60.0.111 ', '123','/solr ');
If (! $ Solr-> ping ())
{
Echo ("service not responding <br/> ");
}
Else
{
Echo ("solr Service is available <br/> ");
}
$ Response = $ solr-> deleteById ("a123 ");
Echo ($ response-> getHttpStatusMessage ());
?>
LUpdate index by PHP
If we want to update a document to index, there are two methods to resolve it:
Method 1: delete the document by id, and then add an new one to index.
Method 2: use the add method to directly add the document to index, because id is an indentify field, Solr server will use new document to cover the old one.
How can I enable Solr to support full-text searches in Chinese, Japanese, and English. Apache provides a cjk library function for us to use, specific use reference: http://chaifeng.com/blog/2008/01/_apache_solr.html
By default, Apache Solr does not support Chinese retrieval. If a document contains Chinese characters, you must use a complete Chinese sentence to retrieve the content.
The following uses the demo of Apache Solr as an example. Note: The bold part must be modified.
Find the following three rows:
<FieldType name = "text" class ="Solr. TextField"PositionIncrementGap = "100">
<Analyzer type = "index">
<Tokenizer class ="Solr. WhitespaceTokenizerFactory"/>
To:
<FieldType name = "text" class = "solr. TextField">
<Analyzer type = "index"Class = "org. apache. lucene. analysis. cjk. CJKAnalyzer">
<Tokenizer class ="Org. apache. lucene. analysis. cjk. CJKTokenizer"/>
Find the following two rows:
<Analyzer type = "query">
<Tokenizer class ="Solr. WhitespaceTokenizerFactory"/>
To:
<Analyzer type = "query"Class = "org. apache. lucene. analysis. cjk. CJKAnalyzer">
<Tokenizer class ="Org. apache. lucene. analysis. cjk. CJKTokenizer"/>
After the modification is complete, run Apache Solr again to search for Chinese characters. The imported documents must be re-imported.
Remember that there isPositionIncrementGap = "100"You must delete it. Otherwise, an exception occurs.
Note: For php programming, make sure that the encoding format of the program code is UTF-8. Otherwise, the index creation will fail.