Solr整合MySQL

本文介绍了如何将MySQL数据库与Solr搜索引擎进行整合,包括在MySQL中创建数据源,设置字段映射,以及在Solr的schema.xml中配置文分词器的详细步骤。特别强调了主键字段必须为string类型以避免搜索问题。

MySQL

拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面
配置E:\solr-4.8.0\example\solr\collection1\conf\solrconfig.xml
<requestHandler name="/dataimport"
     class="org.apache.solr.handler.dataimport.DataImportHandler"> 
       <lst name="defaults"> 
          <str name="config">data-config.xml</str> 
       </lst> 
</requestHandler>
导入依赖库文件:
把<lib dir="../../../dist/" regex="solr-dataimporthandler-\d.*\.jar"/>加在
<lib dir="../../../dist/" regex="solr-cell-\d.*\.jar" />
前面。

创建data-config.xmlE:\solr-4.8.0\example\solr\collection1\conf\data-config.xml
指定MySQL数据库地址用户名密码以及建立索引的数据表

<?xml version="1.0" encoding="UTF-8" ?>
      <dataConfig>  
              <dataSource type="JdbcDataSource"
                          driver="com.mysql.jdbc.Driver"
                          url="jdbc:mysql://localhost:3306/django_blog"
                          user="root"
                          password=""/>  
                  <document name="blog">  
                          <entity name="blog_blog" pk="id"
                                  query="select id,title,content from blog_blog"
                                  deltaImportQuery="select id,title,content from blog_blog where ID='${dataimporter.delta.id}'" 
                                  deltaQuery="select id  from blog_blog where add_time > '${dataimporter.last_index_time}'"  
                                  deletedPkQuery="select id  from blog_blog where id=0">  
                               <field column="id" name="id" />  
                               <field column="title" name="title" />  
                               <field column="content" name="content"/>  
                          </entity>  
                 </document> 
      </dataConfig>
query 用于初次导入到索引的sql语句。
    考虑到数据表中的数据量非常大,比如千万级,不可能一次索引完,
    因此需要分批次完成,那么查询语句query要设置两个参数:
    ${dataimporter.request.length} 
    ${dataimporter.request.offset}
    query=”select id,title,content from blog_blog limit ${dataimporter.request.length} 
    offset ${dataimporter.request.offset}”
    请求:http://localhost:8983/solr/collection2/dataimport?
    command=full-import&commit=true&clean=false&offset=0&length=10000

deltaImportQuery 根据ID取得需要进入的索引的单条数据。
deltaQuery 用于增量索引的sql语句,用于取得需要增量索引的ID。
deletedPkQuery 用于取出需要从索引中删除文档的的ID

为数据库表字段建立域(field),编辑E:\solr-4.8.0\example\solr\collection1\conf\schema.xml:

<!-- mysql -->
   <field name="id" type="string" indexed="true" stored="true" required="true" /> 
   <field name="title" type="text_cn" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 
   <field name="content" type="text_cn" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 
<!-- mysql -->

注意:无论数据库中的主键是什么类型,这里的主键只能是string类型,否则会出现问题。文档找不到。

注意在schema.xml中文分词器的配置:

     <!-- IKAnalyzer-->
    <fieldType name="text_ik" class="solr.TextField">
      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>

原 文 点 这 里

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值