solr导入mysql数据建全量索引

背景:想要练习solr的各种操作,不能直接在服务器上做。所以在本地的windows系统上,尝试用mysql数据建solr全量索引(接之前的solr环境搭建内容)。

1.首先加入建立索引的相关配置,在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>
2.创建数据配置文件 solr\collection1\conf \data-config.xml
<dataConfig>
  <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
   url="jdbc:mysql://localhost:3306/malltest" user="root" password="123456"/>
   <document name="search_object">
      <entity name="goods_info" query="select goods_id,goods_name 
              from ecm_goods where if_show=1 and closed=0 and price>0 
              and if_optimize!=1 ">
        <field column="goods_id" name="id"/>
        <field column="goods_id" name="goods_id"/>
        <field column="goods_name" name="goods_name"/>
        <entity name="store_info" query="select store_id,store_name
                from ecm_store where store_id='${goods_info.store_id}'">
          <field column="store_id" name="store_id"/>
          <field column="store_name" name="store_name"/>
        </entity>
        <entity name="store_extends_info" query="select is_behalfof,is_entity,
                is_enterprise from ecm_store_extends 
                where store_id='${store_info.store_id}'">
          <field column="is_behalfof" name="is_behalfof"/>
          <field column="is_entity" name="is_entity"/>
          <field column="is_enterprise" name="is_enterprise"/>
        </entity>
        <entity name="spc_info" query="select SUM(stock) AS goods_stock 
                from ecm_goods_spec where goods_id='${goods_info.goods_id}' 
                group by goods_id">
          <field column="goods_stock" name="goods_stock"/>
        </entity>
        <entity name="attr_temp_info" query="select attr_id from ecm_abgoods_attr_temp 
                where goods_id='${goods_info.goods_id}' group by goods_id">
          <field column="attr_id" name="attr_id"/>
        </entity>
        <entity name="ecm_goods_price" query="select lower_limit,price 
                from ecm_goods_price where goods_id='${goods_info.goods_id}' 
                AND price>0 group by goods_id">
          <field column="price" name="price"/>
          <field column="lower_limit" name="lower_limit"/>
        </entity>
      </entity>
   </document>
</dataConfig>
此处的配置,问同事和查资料后,个人理解为获取到mysql数据库里的字段信息。并定义在solr里应用时对应的别名。
3.配置完数据以及定义好别名后,需要将solr别名对应的数据类型定义下,这个步骤的配置文件在 solr\collection1\conf \schema.xml里。找到<field...节点,在之后添加
<!-- write yourself info start -->
     <field name="return_goods_status" type="int" indexed="true" stored="true"/>
     <field name="is_enterprise" type="int" indexed="true" stored="true"/>
     <field name="is_entity" type="int" indexed="true" stored="true"/>
     <field name="is_behalfof" type="int" indexed="true" stored="true"/>
     <field name="goods_name" type="textMaxWord" indexed="true" stored="true"/>
     <field name="goods_id" type="int" indexed="true" stored="true"/>
     <field name="store_id" type="int" indexed="true" stored="true"/>
     <!-- write yourself info end -->
以上的别名字段类型配置决定了你的查询时可以用的条件字段和结果的展示字段,没配置的不会展示。
4.配置好配置文件后,导入将要用到的jar包,主要是mysql驱动用的和solr导入用的,包名称mysql-connector-java-5.1.26-bin.jar,
solr-dataimporthandler-4.5.0.jar,solr-dataimporthandler-extras-4.5.0.jar(solr相关的两个包可以在下载后解压solr文件夹solr-4.5.0\dist里找到)
5.重启tomcat然后再访问solr建索引地址, http://localhost:8080/solr/#/collection1/dataimport//dataimport(也可以通过首页一步步点击到建索引地址页)如下图

点击Excute按钮就可以开始创建了,创建的时候最好勾选上下方的Auto-Refresh Status选项,方便查看索引创建信息,如果有错误,可以点击左上logo下的Logging选项,查看日志信息。

6.索引查询的时候在需要分词的字段别名上需要定义类型,以达到指定分词模式

    例如:我需要分词查询的字段是goods_name,所以我需要在上面提到的配置文件schema.xml里将goods_name的type定义成textMaxWord(这个类型是在导入搜狗分词后配置的,详细的搜狗分词可以查看之前的内容http://blog.csdn.net/cdy102688/article/details/16805645)

7.到此solr导入mysql数据创建全量索引就结束了,后面有机会的话,会在看看solr增量索引创建相关的内容

备注:推荐一些solr相关的资料

    qq交流群:187670960    solr中国官网:http://www.solr.cc/blog/

            solr在线文档:http://www.solrcn.com/books/#2-en

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值