`
haohappy2
  • 浏览: 327588 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

How to use solr

 
阅读更多

 

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

 

环境:

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

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" >
    <Environment name="solr/home" type="java.lang.String" value="E:/solr" override="true" />
</Context>

d) 修改tomcat的server.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>
 <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" multiValued="true" required="true" />
</fields>

<defaultSearchField>text</defaultSearchField>
替换为
<defaultSearchField>name</defaultSearchField>

删除所有< copyField …> 项

3.建立PHP客户端
在wamp的www目录下建立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 = new Apache_Solr_Service( '127.0.0.1', '8080', '/solr' );
  if ( ! $solr->ping() ) {
    echo 'Solr service not responding.';
    exit;
  }

  $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>

分享到:
评论

相关推荐

    Apache.Solr.Search.Patterns.1783981849

    This book is for developers who already know how to use Solr and are looking at procuring advanced strategies for improving their search using Solr. This book is also for people who work with ...

    ChatGPT-Prompt-template-Solr Search Engine 轻松创建和搜索由 Apache Solr

    Your task is now to teach me how to use Solr from scratch. To better understand what I want and need you should always answer by including a question that helps you better understand the context and ...

    Pro Docker: Learn how to use Containers as a Service for development and

    Then he addresses the use of Docker in the Hadoop ecosystem with complete chapters on utilizing not only Hadoop, but Hive, HBase, Sqoop, Kafka, Solr and Spark. What You Will LearnHow to install a ...

    [Solr] Solr 管理教程 (英文版)

    Master the use of Drupal and associated scripts to administrate, monitor, and optimize Solr Overview Learn how to work with monitoring tools like OpsView, New Relic, and SPM Utilize Solr scripts and...

    solr in action

    - **使用方法**(How to use this book)介绍了如何充分利用本书资源进行学习。 - **代码约定与下载**(Code conventions and downloads)章节说明了书中代码示例的格式规范以及如何获取相关代码资源。 - **作者在线...

    Pro Docker(Apress,2015)

    Then he addresses the use of Docker in the Hadoop ecosystem with complete chapters on utilizing not only Hadoop, but Hive, HBase, Sqoop, Kafka, Solr and Spark. What You Will Learn How to install a ...

    英文原版-Monitoring ElasticSearch 1st Edition

    Monitor your Elasticsearch cluster's health, and diagnose and solve its performance and reliability issuesElasticSearch is a distributed search server similar to Apache Solr with a focus on large ...

    Solr.In.Action目录整理版

    此外,书中可能还会有如何使用本书(How to use this book)、代码约定与下载(Code conventions and downloads)、作者在线(Author Online)等相关信息,为读者提供学习Solr的辅助资源和工具。 本书的目录整理版...

    Monitoring ElasticSearch

    ElasticSearch is a distributed search server similar to Apache Solr with a focus on large datasets, a schema-less setup, and high availability. This schema-free architecture allows ElasticSearch to ...

    django2 by examples

    Use Django with other technologies such as Redis, Celery and Solr. Develop pluggable Django applications Create advanced features, optimize your code and use the cache framework Add ...

    Apress.Pro.Docker

    In this fast-paced book on the Docker ... Then he addresses the use of Docker in the Hadoop ecosystem with complete chapters on utilizing not only Hadoop, but Hive, HBase, Sqoop, Kafka, Solr and Spark.

    Next-Generation Big Data: A Practical Guide to Apache Kudu, Impala, and Spark

    Understand enterprise big data topics such as big data governance, metadata management, data lineage, impact analysis, and policy enforcement, and how to use Cloudera Navigator to perform common data ...

    Apache Flume Distributed Log Collection for Hadoop(PACKT,2ed,2015)

    Use a morphline-backed Sink to feed data into Solr Create redundant data flows using sink groups Configure and use various sources to ingest data Inspect data records and move them between ...

Global site tag (gtag.js) - Google Analytics