03_Solr导入数据库的索引

搭建数据库的环境

  • 新建solr数据库,编码为utf-8
  • 运行lucene.sql文件,导入表
  • 生成product表


配置solr

  • 停止tomcat
  • product的域,放入D:\tools\tomcat\solr\collection1\conf\schema.xml
    <!--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"/>
  • D:\tools\tomcat\solr\collection1下新建lib目录,拷贝三个jar到此目录
    solr-dataimporthandler-extras-4.10.3.jar
    solr-dataimporthandler-4.10.3.jar
    mysql-connector-java-5.1.7-bin.jar
    Dataimport的两个jar包
mysql驱动包
  • 编辑solrconfig.xml,添加requestHandler
    D:\tools\tomcat\solr\collection1\conf\solrconfig.xml
    添加,建议在/select附近添加
    <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
        <lst name="defaults">
            <str name="config">data-config.xml</str>
        </lst>
    </requestHandler>
  • 在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="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>
  • 修改上面的mysql的url,user和password,修改为你自己的mysql环境

  • 重启tomcat

web界面

  • 进入collection,点击Dataimport,看到如下界面,表示配置成功


  • 导入数据库的索引


  • 导入索引成功


    image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。