zoukankan      html  css  js  c++  java
  • Solr-DIH建立索引并执行简单初步的查询

    我们将solr的安装目录设置为$SOLR_INSTALL,
     
    ./solr start,不使用任何原有的examples来进行,启动完成后,不存在任何的core,提示No cores available。
     
    在手动执行Add Core操作时,需要保证instanceDir和dataDir事先必须存在,以便能够建立目录成功。
     


     
     
    根据错误提示,目录应该建立在$SOLR_INSTALL/server/solr/下,且必须保证存在对应的配置文件;可以从example/conf目录下拷贝所有的配置文件至该目录下(solrconfig.xml,managed-schema等文件也必须存在)。
     
    在solrconfig.xml配置文件中,增加requestHandler来处理数据导入相关:
     
    <requestHandler name="/dataimport" class="solr.DataImportHandler">
        <lst name="defaults">
          <str name="config">db-data-config.xml</str>
        </lst>
      </requestHandler>
     
     
    由于使用DIH需要依赖其他的jar包,所以需要定义lib标签,dataimporthandler的相关jar包在${SOLR_INSTALL}/dist下:
     
      
    <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*.jar" />  
     
     
    需要定义db-data-config.xml配置文件,对应数据库相关的配置以及商品查询相关配置:
     
    <dataConfig>  
      <dataSource type="JdbcDataSource"  
                  driver="com.mysql.jdbc.Driver"  
                  convertType="true"  
                  url="jdbc:mysql://xxxx"  
                  user="xxx"  
                  password="xxx"/>  
      <document>  
        <entity name="gt_goods" query="select goods_id, goods_sn, goods_name,price, stock from gt_goods" >  
        </entity>  
      </document>  
    </dataConfig>  
     
     
    注意,我们使用到的mysql驱动需要在目录下存在:$SOLR_INSTALL/server/solr-webapp/webapp/WEB-INF/lib。
     
    entity标签的配置需要与另外一个配置文件配合使用:schema.xml(新版本后名称已经改为managed-schema),在其中定义了field和fieldtype,根据我们前面的配置需要重新定义。
     
    在schema.xml(managed-schema)中,需要将对应的unique-key等标签设置正确,否则系统启动时会进行校验并提示错误。
     
    但是启动后,提示no dataimport-handler defined!
     



     
     
     
    原来居然是配置文件没有保存成功,出现下面的界面后,说明/dataimport已经可用:
     


     
     
    但是却没有索引成功,提示下面的相关信息:
     

    Last Update: 20:41:08

    Indexing completed. Added/Updated: 0 documents. Deleted 0 documents. (Duration: 12s)
    Requests: 1 (0/s)Fetched: 54223 (4519/s)Skipped: 0, Processed: 0 (0/s)
    Started
     
    没有增加任何documents,持续十二秒,根据fetched的size,拿到的数据库表记录条目数量与count(*)得到的结果一致,但是却没有加入任何的数据。
     
    经过一通查找,将managed-schema中的<schema name="gt_goods" version="1.6”>需要与db-data-config.xml文件中的entity标签名称保持一致:    <entity name="gt_goods" query="select goods_id, goods_sn, goods_name,price, stock from gt_goods" >  。
     
    执行后,仍然没有成功(说明不是前一步导致),最后发现将所有的dynamicField都已经被删除,加上一个就可以了:
     
    <dynamicField name="*"  type="string"  indexed="true"  stored="true" />
     
     
    分词策略
     
    我们使用提供的query进行查询时,例如查询商品名称 goods_name:刺绣花边,查出的结果为472条,经过对查询结果的分析,可以看出与下面的查询非常类似,solr只是简单地对词语进行逐字分词查询。
     
    select count(*) from gt_goods where goods_name like '%刺%' or goods_name like '%花%' or goods_name like '%边%' or goods_name like '%绣%';
     
     
    这种分词对于查询来说非常原始,而且不准确。
     



     
     
     
    mmseg4j
     
     
    加入依赖,对于mmseg4j的支持,以及lucene-analysis的实现,便于以后加入到solr中。
     
    <dependency>
                <groupId>com.chenlb.mmseg4j</groupId>
                <artifactId>mmseg4j-core</artifactId>
                <version>1.9.1</version>
            </dependency>
    
            <dependency>
                <groupId>com.chenlb.mmseg4j</groupId>
                <artifactId>mmseg4j-analysis</artifactId>
                <version>1.9.1</version>
            </dependency>
     
     
    在mmseg4j-core-with-dic中(只有1.8.6版本)有example包,对应各种分词策略的示例,执行main()函数即可。此外,还包括/data/目录,用于存放mmseg4j用到的中文字典文件。
     
    字典文件的加载是从Dictionary.getInstance()开始,public static Dictionary getInstance(),从默认目录加载词库文件,查找默认目录顺序:
    • 从系统属性mmseg.dic.path指定的目录中加载
    • 从classpath/data目录
    • 从user.dir/data目录
     
    可以加载自定义分词的文件,加载words-xxx.dic作为扩展词库目录,我们可以根据需要在该目录下加上对应的词组。
     
    我们在普通情况下,并不会将“阿玛尼”作为一个品牌,拆出来的词以每个单词划分:“阿 | 玛 | 尼”;而当我们在文件words-xxx.dic中加入该单词后,就可以正常地将该词分开。
     
    在managed-schema中加入mmseg4j的这3种分析类型:
     
    <fieldType name="text_mmseg4j_simple" class="solr.TextField" positionIncrementGap="100">
          <analyzer type="index">
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="/Users/xxx/develop/tools/solr-5.5.0/data"/>
          </analyzer>
        </fieldType>
        <fieldType name="text_mmseg4j_complex" class="solr.TextField" positionIncrementGap="100">
          <analyzer type="index">
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="/Users/xxx/develop/tools/solr-5.5.0/data"/>
          </analyzer>
        </fieldType>
        <fieldType name="text_mmseg4j_maxword" class="solr.TextField" positionIncrementGap="100">
          <analyzer type="index">
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="/Users/xxx/develop/tools/solr-5.5.0/data"/>
          </analyzer>
        </fieldType>
     
     
    并设置我们使用的goods_name为这种类型:
     
       <field name="goods_name" type="text_mmseg4j_complex" indexed="true" stored="true"/>
    
     
    重启solr,显示该core已经无法正常加载出来:
     
    xxx: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load conf for core zhentest: Can't load schema /Users/xxx/develop/tools/solr-5.5.0/server/solr/zhentest/conf/managed-schema: Plugin init failure for [schema.xml] fieldType "text_mmseg4j_simple": Plugin init failure for [schema.xml] analyzer/tokenizer: Error loading class 'com.chenlb.mmseg4j.solr.MMSegTokenizerFactory'
     
     
    原因是我们依赖的jar包并没有加入到WEB-INF/lib中,此外还需要依赖:
     
    <dependency>
                <groupId>com.chenlb.mmseg4j</groupId>
                <artifactId>mmseg4j-solr</artifactId>
                <version>1.9.1</version>
            </dependency>
     
     
    作为mmseg4j和solr之间的桥梁,否则会出现com.chenlb.mmseg4j.solr.MMSegTokenizerFactory找不到的错误。
     
    版本兼容也是一个大问题,最终的依赖关系为:
     
    • solr: 5.5.0
    • mmseg4j-solr: 2.3.0
    • mmseg4j-core: 1.10.0
     
    终于成功查询出商品的结果:
     


     
     
    因为之前索引用到的数据也没有经过合理地分词,所以在更改分词策略后,需要对所有的数据进行统一的/dataimport操作。在对所有的数据进行重新整理后,查询出来的记录条目数量就可以满足基本的需求,在对“刺绣花边”进行搜索时,就可以查询出最终的结果与下面的sql查询数量保持一致(因为words.dic中存在“刺绣”和“花边”两个词):
     
    select count(*) from gt_goods where goods_name like '%刺绣%' or goods_name like '%花边%';
    
     
     
     
     
  • 相关阅读:
    [对android程序作代码混淆]
    用smali实现Android apk的简单汉化
    防止java反编译的一些常用方法
    http抓包工具推荐WSockExpert/httpwatch/HttpAnalyzer/DebugBar
    JSP动态网站环境搭建应用中的详细步骤(Tomcat和Apache/IIS的整合)
    22、Secondary Tiles
    21、磁贴和磁贴通知(tile)(下)
    19、Context Menu
    代码管理工具
    20、磁贴和磁贴通知(tile)(上)
  • 原文地址:https://www.cnblogs.com/mmaa/p/5789864.html
Copyright © 2011-2022 走看看