solr插件导入数据库中的数据

solr插件导入数据库中的数据

1:自定义与数据库对应的域:

1.1. 设置业务系统Field

     如果不使用Solr提供的Field可以针对具体的业务需要自定义一套Field。

例如:如下是商品信息Field:

<!--product-->
   <field name="product_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_catalog_name" type="string" indexed="true" 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"/>

2、  批量导入数据:

使用dataimport插件批量导入数据。

第一步:把dataimport插件依赖的jar包添加到solrcore(collection1\lib)中,lib自己建。

还需要mysql的数据库驱动。

 


第二步:配置solrconfig.xml文件,添加一个requestHandler。(/solrHome/collectin1/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> 

第三步:创建一个data-config.xml,保存到collection1\conf\目录下

<?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="root"/>   
<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>

 注意:<document>中包含数据库中的表名(product) ,查询语句,《fileld》中的column对应product表中的列名,name对应是上面我们自定义的域名。

第四步:重启tomcat

 

猜你喜欢

转载自www.cnblogs.com/dw3306/p/9660402.html