六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 706|回复: 0

Apache Solr + PHP 进行全文查询

[复制链接]
 楼主| 发表于 2014-9-3 16:20:54 | 显示全部楼层 |阅读模式
环境:  
JDK 1.6  
apache-solr-1.2.0 [http://lucene.apache.org/solr]  
tomcat 5.5.17  
wamp 2.0  
  
1.         安装solr  
a)         下载apache-solr-1.2.0.zip,解压。将apache-solr-1.2.0\dist 下的apache-solr-1.2.0.war 改名为solr.war并拷贝到tomcat目录下的webapps目录中。  
b)        apache-solr-1.2.0\example\ 下的 solr 目录拷贝到任意位置,如:E:\solr  
c)         tomcat目录下的conf\Catalina\localhost 目录中(如果没有则手工创建该目录)创建solr.xml文件,文件内容如下:
<Context docBase="D:/tomcat/webapps/solr.war" debug="0" crossContext="true" >  
1.         <Environment name="solr/home" type="java.lang.String" value="E:/solr" override="true" />  
</Context>  
d)        修改tomcatserver.xml文件,找到<Connector port="8080" … 项(假设tomcat监听8080端口),添加编码方式,修改后如下<Connector port="8080"  URIEncoding="UTF-8" …  
e)         启动tomcat。在浏览器中输入http://localhost:8080/solr/,出现“Welcom to Solr”页面,说明安装成功。  
  
2.         建立自定义索引模式  
a)         打开E:\solr\conf\schema.xml 文件 找到
<fields>
……
<fields>
替换为
<fields>  
1.         <field name="id" type="string" indexed="true" stored="true" required="true" />   
2.         <field name="name" type=" string " indexed="true" stored="true" required="true" />  
3.         <field name="address" type="text" indexed="true" stored="true" multiValued="true" required="true" />               
</fields>

<defaultSearchField>text</defaultSearchField>
替换为

<defaultSearchField>name</defaultSearchField>

删除所有< copyField …>   
3.         建立PHP客户端
wampwww目录下建立solr目录。
SolrPhpClient.zip解压,并将其中的Apache目录拷贝到www/solr目录下。  
创建index.php文件,内容如下:
<!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服务器  
  $solr = new Apache_Solr_Service( '127.0.0.1', '8080', '/solr' );  
  //测试是否联通  
  if ( ! $solr->ping() ) {  
    echo 'Solr service not responding.';  
    exit;  
  }  
    
  //  
  // 创建两条记录nuby zhangyan  
  //  
  
  
  $parts = array(  
    'nuby' => array(  
      'id' => 1,  
      'name' => '张岩',  
      'address' => array( '天安门', '北京天安门' ),  
    ),  
    'zhangyan' => array(  
      'partno' => 2,  
      'name' => '张岩',  
      'model' => '北京五道口',  
    )  
  );  
      
  $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: 张岩',  
    'name: 天安门'  
  );  
  
  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>

摘自:http://blog.163.com/chen98_2006@ ... 584272008821929636/

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表