zoukankan      html  css  js  c++  java
  • Solr

    1.1 Solr是什么?

    Solr Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务。

    solr可以实现全文检索功能(索引、搜索),solr是可以独立运行在tomcatweb容器中,对外提供http 的索引和搜索服务

    Solr不提供构建UI的功能,Solr提供了一个管理界面,通过管理界面可以查询Solr的配置和运行情况。

    1.2 Solr下载

    从Solr官网(http://lucene.apache.org/solr/)下载Solr4.10.3Solr的运行环境,Linux下需要下载lucene-4.10.3.tgzwindows下需要下载lucene-4.10.3.zip.Solr使用指南可参考:https://wiki.apache.org/solr/FrontPage.

    bin:可执行程序、脚本

    contrib:存放了一些扩展的包,用于索引和搜索

    dist:其中有一个solr-4.10.3.war 将其部署到tomcat容器中,运行solr

    docs:使用说明文档

    example:存储了很多solr开发使用的例子工程及目录结构等

    Solr安装配置

    2.1 SolrCore的安装配置

    2.1.1 SolrHomeSolrcore

    SolrHomeSolr运行的主目录包括多个SolrCore目录每个solrcore目录 对应一个索引文件目录

     SolrCore是一个逻辑的概念,将一个solr工程(部署在tomcat中)逻辑区别开以solrcore为单位的服务.

    2.1.2创建SolrCore整个过程.

    1.将contribdist两个目录 拷贝到f:developsolr下.

    contrib:扩展包IKAnalyzer和数据包databaseDriver

    2.创建目录:F:developsolrsolrHome,将解压的solr的solrHome复制过来.如下:

    在SOlrCoreconf的solrconfig.xml配置扩展包以及处理器例如:

    <!-- 配置ik中文分词器-->
    <lib dir="${solr.install.dir:../..}/contrib/IKAnalyzer/lib" regex=".*.jar" /> 
    <!-- 数据导入处理器-->
    <lib dir="${solr.install.dir:../..}/contrib/dataimporthandler/lib" regex=".*.jar" /> 
    <!-- 连接数据库-->
    <lib dir="${solr.install.dir:../..}/contrib/databaseDriver/lib" regex=".*.jar" />

    ------------------------------------------------------------------------------------------------------------------------------------

    <!-- 添加requestHandler-->
    <requestHandler name="/dataimport" 
    class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
    <str name="config">data-config.xml</str>
    </lst>
    </requestHandler>

    --------------------------------------------------------------------------------------------------------------

    <!-- data的路径,默认在solrCore包下-->

    <dataDir>${solr.data.dir:}</dataDir>

     ============================================================

    在schema.xml配置feild和fieldType

    <!-- IKAnalyzer,class是jar包中lucene的类名-->
         <fieldType name="text_ik" class="solr.TextField">
            <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
            <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
         </fieldType>
    <!--IKAnalyzer Field-->
       <field name="title_ik" type="text_ik" indexed="true" stored="true" />
       <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
       <!-- pid、name、catalog、catalog_name、price、description、picture-->
       <field name="product_name" type="text_ik" indexed="true" stored="true"/>
       <field name="product_catalog" type="string" indexed="true" stored="true"/>
       <field name="product_catalog_name" type="text_ik" indexed="true" stored="true"/>
       <field name="product_price"  type="float" indexed="true" stored="true"/>
       <field name="product_description" type="text_ik" indexed="true" stored="false"/>
       <field name="product_picture" type="string" indexed="false" stored="true"/>
       
       <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
        <copyField source="product_name" dest="product_keywords"/>
       <copyField source="product_description" dest="product_keywords"/>

    在conf创建data-config.xml,

    <?xml version="1.0" encoding="UTF-8" ?>  
    <dataConfig>   
    <dataSource type="JdbcDataSource"   
              driver="com.mysql.jdbc.Driver"   
              url="jdbc:mysql://localhost:3306/lucene"   
              user="root"   
              password="123"/>   
    <document>   
        <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
             <field column="pid" name="id"/> 
             <field column="name" name="product_name"/> 
             <field column="catalog_name" name="product_catalog_name"/> 
             <field column="price" name="product_price"/> 
             <field column="description" name="product_description"/> 
             <field column="picture" name="product_picture"/> 
        </entity>   
    </document>   
    
    </dataConfig>

    在tomcat中的配置:

    在D:apache-tomcat-7.0.52-solrwebappssolrWEB-INF/

    在web.xml
    <env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>F:developsolrsolrHome</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry>

    在classes中配置:

    =============================================================================

    SolrCore目录详解:

    data包.默认在SolrCore下的data数据目录下包括了index索引目录 和tlog日志文件目录。

    如想更改:索引目录:

     在conf下面的Sqlconfig.xml加载jar包,solr.install.dir 表示SolrCore所在目录。

     

     3.配置请求处理器:在solrconfig.xml中

    requestHandler请求处理器定义了索引和搜索.

    通过/update维护索引,可以完成索引的增加 删除 修改操作.;通过/select搜索索引,

     <requestHandler name="/update" class="solr.UpdateRequestHandler">

    <requestHandler name="/select" class="solr.SearchHandler">

        <!-- 设置默认的参数值,可以在请求地址中修改这些参数-->

        <lst name="defaults">

            <str name="echoParams">explicit</str>

            <int name="rows">10</int><!--显示数量-->

            <str name="wt">json</str><!--显示格式-->

            <str name="df">text</str><!--默认搜索字段-->

        </lst>

    </requestHandler>

    2.2 Solr工程部署

    官方提供的solr的war包:distsolr-4.10.3部署到tomcat的webapp目录下改名为solr.war

    启动tomcat/bin/startup.bat 运行完后ctrl c截止,tomcat会自动解压solr.war,将原来的solr.war删除.

    第二步:拷贝扩展包到tomcat的webappsolrWEB-INFlib:

    扩展包目录:F:1ziliaolucene-solr资料所需要的jarsolrsolr-4.10.3solr-4.10.3examplelibext

    第三步设置solrHome: 修改Tomcat目录 下webappsolrWEB-INFweb.xml文件

    第四步:拷贝log4j.properties文件

     TomcatwebappssolrWEB-INF目录中创建文件 classes文件夹

    第五步:启动tomcat startup-bat

    solrcore的索引维护界面:overwrite="true"   commitWithin="1000" solr 在做索引的时候,每个10001秒)毫秒,做一次文档提交

    3.1 schema.xml

    1. 定义Field <field name="name" type="text_general" indexed="true" stored="true"/>
    2. 定义FieldType
    
    <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
    <analyzer type="index">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
    <!-- in this example, we will only use synonyms at query time
    <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
    -->
    <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
    <analyzer type="query">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
    <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
    </fieldType>
    
    3.指定主键(默认)<uniqueKey>id</uniqueKey>
    4. 定义复制域
    <field name="keywords" type="text_general" indexed="true" stored="true" multiValued="true"/>
    <copyField source="name" dest="keywords"/>
    <copyField source="description" dest="keywords"/>
    
    5. 定义动态域(了解):  动态域其实就是一种域的规则
    
    <dynamicField name="*_i" type="int" indexed="true" stored="true"/>

    3.1 安装中文分词器

    1.导入jar包 :F:developsolrcontribIKAnalyzerlib

    2.在solrconfig.xml配置运行时加载ik分词器的jar包:solr.install.dir表示solrCore

    <lib dir="${solr.install.dir:../..}/contrib/IKAnalyzer/lib" regex=".*.jar" />

     3. 自定义FiledType, 然后在该FiledType中去使用ik分词器

    4. 自定义Filed,然后让该Filed使用我们自己定义的FiledType

    5. 配置扩展词字典以及停用词字典在tomcat的classes包下

    3.2 solrj

    Solrj索引维护:

    1.添加和更新索引:
    private String solrUrl="http://localhost:8080/solr"
    //创建solr服务对象,通过此对象向solr发起请求. SolrServer server= new HttpSolrServer(solrUrl); //创建Document对象 SolrInputDocument doc = new SolrInputDocument(); //向文件添加feild doc.add("id","888");... //创建索引 UpdateResponse response = server.add(doc); server.commit();
    --------------------------------------------------

    //删除索引

    //根据主键删除

    server.deleteById("1013");

    //自定义查询条件删除

    server.deleteByQuery("id:1012");

     

    3,3数据导入处理器

    1.在contribdataimporthandler导入dataimport包:在solrconfig,xml配置

    2.在contribdatabaseDriver导入mysql包,在solrconfig.xml配置

    3.创建data-config.xml配置,在solrCore的conf

     4.在solrconfig.xml,添加requestHandler

    solr的查询语法

    q - 查询关键字查询所有使用*:*

    fq - filter query)过虑查询 product_price:[20 TO *]

    sort - 排序 product_price desc

    start - 分页 0开始

    fl - 指定返回那些字段内容,用逗号或空格分隔多个。product_price product_name

    df-指定一个搜索Field  product_keyswords

    hl 是否高亮 ,设置高亮Field,设置格式前缀和后缀

  • 相关阅读:
    seaborn可视化NOTE
    快速入门pandas
    protege下载安装使用
    用上Latex实现编辑伪代码
    决策树可视化
    关于时间
    地理三维模型制作
    Python使用记录
    编码格式
    素数生成算法小结
  • 原文地址:https://www.cnblogs.com/wwwzzz/p/8331288.html
Copyright © 2011-2022 走看看