How to use solr

 

Please read document via http://www.ibm.com/developerworks/cn/opensource/os-php-apachesolr/ firstly

 

Environment:

JDK 1.6
apache-solr-1.2.0 [http://lucene.apache.org/solr]
tomcat 5.5.17
xampp
SolrPhpClient development kit [https://issues.apache.org/jira/browse/SOLR-341 ]

1. Install solr

a) Download apache-solr-1.2.0.zip and decompress it. Rename apache-solr-1.2.0.war in apache-solr-1.2.0\dist to solr.war and copy it to the webapps directory in the tomcat directory.
b) Copy the solr directory under apache-solr-1.2.0\example\ to any location, such as: E:\solr
c) In the conf\Catalina\localhost directory under the tomcat directory (if not, create the directory manually ) to create a solr.xml file with the following content:

<Context docBase="D:/tomcat/webapps/solr.war" debug="0" crossContext="true" >
    <Environment name="solr/home" type=" java.lang.String" value="E:/solr" override="true" />
</Context>

d) Modify the server.xml file of tomcat, find the item <Connector port="8080" … (assuming tomcat listens on port 8080), add the encoding method, and modify it as follows <Connector port="8080" URIEncoding="UTF-8" …
e) Start tomcat. Enter http://localhost:8080/solr/ in the browser, and the "Welcom to Solr" page appears, indicating that the installation is successful.

2. Create a custom index schema

a) Open the E:\solr\conf\schema.xml file and find
<fields>
......
<fields>
is replaced with
<fields>
 <field name="id" type="string" indexed=" true" stored="true" required="true" />
 <field name="name" type=" string " indexed="true" stored="true" required="true" />
 <field name="address" type="text" indexed="true" stored="true"






Delete all <copyField...> items

3. Create a PHP client
Create a solr directory under the www directory of wamp.
Unzip SolrPhpClient.zip and copy the Apache directory to the www/solr directory.

Create an index.php file with the following content:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional. dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">   
<head>   
<meta content="text/html ; charset=UTF-8" http-equiv="Content-Type"/>
<title></title>
</head>
<body>
<?php
  require_once( 'Apache/Solr/Service.php' );
  $solr = new Apache_Solr_Service( '127.0.0.1', '8080', '/solr' );





  $parts = array(
    'a' => array(
      'id' => 1,
      'name' => 'xxx',
      'address' => array( '111', '222' ),
    ),

    'b' => array(
      'partno' => 2,
      'name' => 'yyy',
      'model' => '3333',
    )
  );

  $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();
  }
  $offset = 0;
  $limit = 10;
  $queries = array(
    'id: 1 OR id: 2',
    'name: xxx',
    'name: 111'
  );

  foreach ( $queries as $query ) {
    $response = $solr->search( $query, $offset, $limit );
    if ( $response->getHttpStatus() == 200 ) {
      if ( $response->response->numFound > 0 ) {
        foreach ( $response->response->docs as $doc ) {
          echo "$doc->partno $doc->name <br />";
        }
        echo '<br />';
      }
    }
    else {
        echo $response->getHttpStatusMessage();
    }
  }
?>
</body>
</html>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988533&siteId=291194637