zoukankan      html  css  js  c++  java
  • Solr使用入门指南

    本文转自 http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0

    因为搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量须要搜索引擎的功能需求,眼下在实现搜索引擎的方案上有集中方案可供选择:

    1. 基于Lucene自己进行封装实现站内搜索。工作量及扩展性都较大,不採用。

    2. 调用Google、Baidu的API实现站内搜索。同第三方搜索引擎绑定太死,无法满足后期业务扩展须要,临时不採用。

    3. 基于Compass+Lucene实现站内搜索。适合于对数据库驱动的应用数据进行索引,尤其是替代传统的like ‘%expression%’来实现对varchar或clob等字段的索引,对于实现站内搜索是一种值得採纳的方案。但在分布式处理、接口封装上尚须要自己进行一定程度的封装,临时不採用。

    4. 基于Solr实现站内搜索。封装及扩展性较好,提供了较为完备的解决方式,因此在门户社区中採用此方案,后期添�Compass方案。

    1Solr简单介绍

    Solr是一个基于Lucene的Java搜索引擎server。Solr 提供了层面搜索、命中醒目显示并且支持多种输出格式(包含 XML/XSLT 和 JSON 格式)。它易于安装和配置,并且附带了一个基于 HTTP 的管理界面。Solr已经在众多大型的站点中使用,较为成熟和稳定。Solr 包装并扩展了 Lucene,所以Solr的基本上沿用了Lucene的相关术语。更重要的是,Solr 创建的索引与 Lucene 搜索引擎库全然兼容。通过对Solr 进行适当的配置,某些情况下可能须要进行编码,Solr 能够阅读和使用构建到其它 Lucene 应用程序中的索引。此外,非常多 Lucene 工具(如Nutch、 Luke)也能够使用Solr 创建的索引。


    2、 Tomcat下Solr安装配置
    因为Solr基于java开发,因此Solr在windows及Linux都能较好部署使用,但因为Solr提供了一些用于測试及管理、维护较为方便的shell脚本,因此在生产部署时候建议安装在Linux上,測试时候能够在windows使用。

    以下以Linux下安装配置Solr进行说明,windows与此类似。
    wget http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.zip
    unzip apache-tomcat-6.0.16.zip
    mv apache-tomcat-6.0.16 /opt/tomcat
    chmod 755 /opt/tomcat/bin/*
    wget http://apache.mirror.phpchina.com/lucene/solr/1.2/apache-solr-1.2.0.tgz
    tar zxvf apache-solr-1.2.0.tgz

    Solr的安装配置最为麻烦的是对solr.solr.home的理解和配置,主要有三种

    基于当前路径的方式
    cp apache-solr-1.2.0/dist/apache-solr-1.2.0.war /opt/tomcat/webapps/solr.war
    mkdir /opt/solr-tomcat
    cp -r apache-solr-1.2.0/example/solr/ /opt/solr-tomcat/
    cd /opt/solr-tomcat
    /opt/tomcat/bin/startup.sh

    因为在此种情况下(没有设定solr.solr.home环境变量或JNDI的情况下),Solr查找./solr,因此在启动时候须要切换到/opt/solr-tomcat

    基于环境变量solr.solr.home
    在当前用户的环境变量中(.bash_profile)或在/opt/tomcat/catalina.sh中加入�例如以下环境变量
    export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/opt/solr-tomcat/solr"

    基于JNDI配置
    mkdir –p /opt/tomcat/conf/Catalina/localhost
    touch /opt/tomcat/conf/Catalina/localhost/solr.xml ,内容例如以下:

    Xml代码 复制代码
    1. <Context docBase="/opt/tomcat/webapps/solr.war" debug="0" crossContext="true" >  
    2.   <Environment name="solr/home" type="java.lang.String" value="/opt/solr-tomcat/solr" override="true" />  
    3. </Context>  

     

    訪问solr管理界面 http://ip:port/solr


    3、 Solr原理

    Solr对外提供标准的http接口来实现对数据的索引的添加�、删除、改动、查询。在 Solr 中,用户通过向部署在servlet 容器中的 Solr Web 应用程序发送 HTTP 请求来启动索引和搜索。Solr 接受请求,确定要使用的适当SolrRequestHandler,然后处理请求。通过 HTTP 以相同的方式返回响应。默认配置返回Solr 的标准 XML 响应,也能够配置Solr 的备用响应格式。

    能够向 Solr 索引 servlet 传递四个不同的索引请求:
    add/update 同意向 Solr 加入�文档或更新文档。直到提交后才干搜索到这些加入�和更新。
    commit 告诉 Solr,应该使上次提交以来所做的全部更改都能够搜索到。
    optimize 重构 Lucene 的文件以改进搜索性能。索引完毕后执行一下优化通常比較好。假设更新比較频繁,则应该在使用率较低的时候安排优化。一个索引无需优化也能够正常地执行。优化是一个耗时较多的过程。
    delete 能够通过 id 或查询来指定。按 id 删除将删除具有指定 id 的文档;按查询删除将删除查询返回的全部文档。

    一个典型的Add请求报文

    Xml代码 复制代码
    1. <add>  
    2.   <doc>  
    3.     <field name="id">TWINX2048-3200PRO</field>  
    4.     <field name="name">CORSAIR  XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</field>  
    5.     <field name="manu">Corsair Microsystems Inc.</field>  
    6.     <field name="cat">electronics</field>  
    7.     <field name="cat">memory</field>  
    8.     <field name="features">CAS latency 22-3-3-6 timing, 2.75v, unbuffered, heat-spreader</field>  
    9.     <field name="price">185</field>  
    10.     <field name="popularity">5</field>  
    11.     <field name="inStock">true</field>  
    12.   </doc>  
    13.   <doc>  
    14.     <field name="id">VS1GB400C3</field>  
    15.     <field name="name">CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail</field>  
    16.     <field name="manu">Corsair Microsystems Inc.</field>  
    17.     <field name="cat">electronics</field>  
    18.     <field name="cat">memory</field>  
    19.     <field name="price">74.99</field>  
    20.     <field name="popularity">7</field>  
    21.     <field name="inStock">true</field>  
    22.   </doc>  
    23. </add> 

    一个典型的搜索结果报文:

    Xml代码 复制代码
    1. <response>  
    2.     <lst name="responseHeader">  
    3.         <int name="status">0</int>  
    4.         <int name="QTime">6</int>  
    5.         <lst name="params">  
    6.             <str name="rows">10</str>  
    7.             <str name="start">0</str>  
    8.             <str name="fl">*,score</str>  
    9.             <str name="hl">true</str>  
    10.             <str name="q">content:"faceted browsing"</str>  
    11.         </lst>  
    12.     </lst>  
    13.   
    14.     <result name="response" numFound="1" start="0" maxScore="1.058217">  
    15.         <doc>  
    16.             <float name="score">1.058217</float>  
    17.             <arr name="all">  
    18.                 <str>http://localhost/myBlog/solr-rocks-again.html</str>  
    19.                 <str>Solr is Great</str>  
    20.                 <str>solr,lucene,enterprise,search,greatness</str>  
    21.                 <str>Solr has some really great features, like faceted browsing and replication</str>  
    22.             </arr>  
    23.             <arr name="content">  
    24.                 <str>Solr has some really great features, like faceted browsing and replication</str>  
    25.             </arr>  
    26.             <date name="creationDate">2007-01-07T05:04:00.000Z</date>  
    27.             <arr name="keywords">  
    28.                 <str>solr,lucene,enterprise,search,greatness</str>  
    29.             </arr>  
    30.             <int name="rating">8</int>  
    31.             <str name="title">Solr is Great</str>  
    32.             <str name="url">http://localhost/myBlog/solr-rocks-again.html</str>  
    33.         </doc>  
    34.     </result>  
    35.   
    36.     <lst name="highlighting">  
    37.         <lst name="http://localhost/myBlog/solr-rocks-again.html">  
    38.             <arr name="content">  
    39.                 <str>Solr has some really great features, like <em>faceted</em>  
    40.                 <em>browsing</em> and replication</str>  
    41.             </arr>  
    42.         </lst>  
    43.     </lst>  
    44. </response>  

    关于solr的具体使用说明,请參考
    http://wiki.apache.org/solr/FrontPage

    4、 Solr測试使用

    Solr的安装包包括了相关的測试例子,路径在apache-solr-1.2.0/example/exampledocs

    1. 使用shell脚本(curl)測试Solr的操作:
    cd apache-solr-1.2.0/example/exampledocs
    vi post.sh,依据tomcat的ip、port改动URL变量的值URL=http://localhost:8080/solr/update
    ./post.sh *.xml                 #

    2. 使用Solr的java 包測试Solr的操作:
    查看帮助:java -jar post.jar –help
    提交測试数据:
    java -Durl=http://localhost:8080/solr/update -Ddata=files -jar post.jar  *.xml     

    以下以添加�索引字段liangchuan、url为例,说明一下Solr中索引命令的使用

    1) 改动solr的schema,配置须要索引字段的说明:
    vi /opt/solr-tomcat/solr/conf/schema.xml ,在<fields>中添加�例如以下内容

    Xml代码 复制代码
    1. <field name="liangchuan"  type="string" indexed="true" stored="true"/>  
    2. <field name="url"  type="string" indexed="true" stored="true"/>  
    <field name="liangchuan"  type="string" indexed="true" stored="true"/>
    <field name="url"  type="string" indexed="true" stored="true"/>


    2) 创建添加�索引请求的xml測试文件
    touch /root/apache-solr-1.2.0/example/exampledocs/liangchuan.xml,内容例如以下:

    Xml代码 复制代码
    1. <add>  
    2.   <doc>  
    3.     <field name="id">liangchuan000</field>  
    4.     <field name="name">Solr, the Enterprise Search Server</field>  
    5.     <field name="manu">Apache Software Foundation</field>  
    6.     <field name="liangchuan">liangchuan's solr "hello,world" test</field>  
    7.     <field name="url">http://www.google.com</field>  
    8.   </doc>  
    9. </add>  

    3) 提交索引请求
    cd apache-solr-1.2.0/example/exampledocs
    ./post.sh liangchuan.xml

    4) 查询
    通过solr的管理员界面http://localhost:8080/solr/admin查询
    或通过curl 測试:
    export URL="http://localhost:8080/solr/select/"
    curl "$URL?indent=on&q=liangchuan&fl=*,score"

    5、Solr查询条件參数说明
    參数     描写叙述     演示样例
    q    

    Solr 中用来搜索的查询。能够通过追加一个分号和已索引且未进行断词的字段的名称来包括排序信息。默认的排序是 score desc,指按记分降序排序。    
    q=myField:Java AND otherField:developerWorks; date asc
    此查询搜索指定的两个字段并依据一个日期字段对结果进行排序。

    start     将初始偏移量指定到结果集中。可用于对结果进行分页。默认值为 0。    
    start=15
    返回从第 15 个结果開始的结果。

    rows     返回文档的最大数目。默认值为 10。     rows=25

    fq     提供一个可选的筛选器查询。查询结果被限制为仅搜索筛选器查询返回的结果。筛选过的查询由 Solr 进行缓存。它们对提高复杂查询的速度很实用。
    不论什么能够用 q 參数传递的有效查询,排序信息除外。

    hl     当 hl=true 时,在查询响应中醒目显示片段。默觉得 false。參看醒目显示參数上的 Solr Wiki 部分能够查看很多其它选项     hl=true

    fl     作为逗号分隔的列表指定文档结果中应返回的 Field 集。默觉得 “*”,指全部的字段。“score” 指还应返回记分。    
    *,score

    当中关于Solr查询相关的參数具体的信息请參看:
    http://wiki.apache.org/solr/CommonQueryParameters

    Solr的查询条件參数q的格式与Lucene同样,详细參看:
    http://lucene.apache.org/java/docs/queryparsersyntax.html

    支持一下吆 请收藏一下:非常好

    6、 在门户社区中solr使用模式
    在门户社区中须要使用solr,可採用例如以下模式:

    对原有系统已有的数据或须要索引的数据量较大的情况
    直接採用通过http方式调用solr的接口方式,效率较差,採用solr本身对csv 的支持(http://wiki.apache.org/solr/UpdateCSV
    ),将数据导出为csv格式,然后调用solr的csv接口http://localhost:8080/solr/update/csv

    对系统新增的数据
    先将须要索引查询的数据组装成xml格式,然后使用httpclient 将数据提交到solr 的http接口,比如   
    http://localhost:8080/solr/update

    也能够參考post.jar中的SimplePostTool的实现。
    http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/SimplePostTool.java?view=co

    中文分词
    採用庖丁解牛作为solr(Lucene)缺省的中文分词方案
    项目库:http://code.google.com/p/paoding/

    Google groups:http://groups.google.com/group/paoding
    Javaeye的groups:http://analysis.group.javaeye.com/


    与nutch的集成使用
    http://blog.foofactory.fi/2007/02/online-indexing-integrating-nutch-with.html

    嵌入式Solr
    http://wiki.apache.org/solr/Solrj#EmbeddedSolrServer

    分布式索引
    http://wiki.apache.org/solr/CollectionDistribution


    7、參考资料
    http://wiki.apache.org/solr/
    http://www.ibm.com/developerworks/cn/java/j-solr1/
    http://www.ibm.com/developerworks/cn/java/j-solr2/
    http://www.xml.com/pub/a/2006/08/09/solr-indexing-xml-with-lucene-andrest.html?page=1
    http://lucene.apache.org/java/docs/queryparsersyntax.html
    http://www.blogjava.net/RongHao/archive/2007/11/06/158621.html

  • 相关阅读:
    php简单的server登陆验证
    CentOS更新php(PHP 5.2+ is required问题)
    CentOS 5.5 x86_64下安装oci8与pdo_oci扩展
    httpd.conf详解
    Thinkphp中去除URL里的index.php
    Oracle使用虚拟表dual一次插入多条记录【摘录】
    【问题】apache目录403错误
    mysql中存储引擎MyISAM与InnoDB的一些区别
    从开辟蓝海到保卫蓝海(三)
    答MM问:经济泡沫破了不更好吗?
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4004404.html
Copyright © 2011-2022 走看看