美文网首页
03_Solr导入数据库的索引

03_Solr导入数据库的索引

作者: 对方不想理你并向你抛出一个异常 | 来源:发表于2018-01-21 18:06 被阅读0次

    搭建数据库的环境

    • 新建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

    相关文章

      网友评论

          本文标题:03_Solr导入数据库的索引

          本文链接:https://www.haomeiwen.com/subject/fuhqaxtx.html