Solr连接数据库

  1.根据你的数据库下载不同的数据库数据库驱动(solr会调用数据库驱动来与数据库建立连接) 将其复制到Tomcat根目录下的webapps\solr\WEB-INF\lib下面

名字任意,路径也可以任意,告诉solr到相关数据库导入相关数据的文件) 

3.Tomcat根目录\solr\collection1\conf \solrconfig.xml,文件里配置data-confing.xml路径

   这一步则是告诉solr在哪儿找数据库配置的文件  config节点的值是你新建的data-config所在目录

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
 <lst name="defaults">
 <str name="config">C:\Program Files\Apache Software Foundation\Tomcat 7.0\solr\collection1\conf\data-config.xml</str>
</lst></requestHandler>

 

solr提前封装好的

  5.在Tomcat根目录\solr\collection1\conf \solrconfig.xml,文件里配置dist, contrib这两个文件夹的路径(solrconfig.xml已存在这几个节点。则修改一下就行了)

写你tomcat所在目录哦  我这里只是假设在c盘

  这一步配置 告诉solr他需要的相关jar包到哪里去找  (jar包 类似c#封装好的类库而已)

<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/contrib/extraction/lib" regex=".*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/dist/" regex="solr-cell-\d.*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/contrib/clustering/lib/" regex=".*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/dist/" regex="solr-clustering-\d.*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/contrib/langid/lib/" regex=".*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/dist/" regex="solr-langid-\d.*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/contrib/velocity/lib" regex=".*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/dist/" regex="solr-velocity-\d.*\.jar"/>
<lib dir="C:/Program Files/Apache Software Foundation/Tomcat 7.0/dist/" regex="solr-dataimporthandler-\d.*\.jar"/>

6.将dist文件夹下的这两个文件

solr 用户名密码springboot solr配置数据库_solr

 



solr导入命令使用的jar)

    以上配置的路径我是用绝对路径配置的,也可以用相对路径,

   配置数据库以及连接信息

    首先是配置data-confing.xml文件,data-confing.xml文件就是连接数据库的配置文件(刚才新建的),根据你的需求配置 如果是其他数据库则jdbc写其他数据库 链接也写对应的数据库的格式

   这里嗮一下我的

<dataConfig>
<dataSource type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@//127.0.0.1:1521/b2c" user="ff" password="ff" /> 
<document>
<entity name="test"    pk="ID" 
       query="select p.productid as ID,p.name as productName,p.retailprice as ProductPrice,p.details,tab.tagTitle,tab.tagId,p.isdelete,p.createtime,p.Issell,cm.title as categoryMenuName,cm.recordid as cmRecordid,p.ImagePath,p.CostPrice,p.WholesalePrice,p.MarketPrice,p.Recordid from product p
           left join(
           select p.recordid,strcatSpace(t.title) tagTitle,strcatSpace(t.recordid) tagId from product p left join OBJECTTAG o on p.recordid=o.objectrowid
           left join tagattribute t on o.tagattributerowid=t.recordid
            group by p.recordid
            )tab on p.recordid=tab.recordid
        left join CategoryMenu cm on p.categoryid=cm.recordid
           where p.isdelete='0'" 
       transformer="ClobTransformer" 
   deltaImportQuery="select p.productid as ID,p.name as productName,p.retailprice as ProductPrice,p.details,tab.tagTitle,tab.tagId,p.isdelete,p.createtime,p.Issell,cm.title as categoryMenuName,cm.recordid as cmRecordid,p.ImagePath,p.CostPrice,p.WholesalePrice,p.MarketPrice,p.Recordid from product p
           left join(
           select p.recordid,strcatSpace(t.title) tagTitle,strcatSpace(t.recordid) tagId from product p left join OBJECTTAG o on p.recordid=o.objectrowid
           left join tagattribute t on o.tagattributerowid=t.recordid
            group by p.recordid
            )tab on p.recordid=tab.recordid
                left join CategoryMenu cm on p.categoryid=cm.recordid
                 where  p.productid='${dataimporter.delta.ID}'" 
   deltaQuery="select productid as ID from product where lastupdatetime>to_date('${dataimporter.last_index_time}','YYYY-MM-DD HH24:MI:SS')"  
   deletedPkQuery="select productid as ID from product where isdelete=1" 
>
<field column="ID" name="id"/> 
<field column="Recordid" name="Recordid"/>
<field column="productName" name="ProductName"/> 
<field column="DETAILS" name="details" clob="true" /> 
<field column="ProductPrice" name="productprice"/> 
<field column="CostPrice" name="CostPrice"/> 
<field column="WholesalePrice" name="WholesalePrice"/> 
<field column="MarketPrice" name="MarketPrice"/> 
<field column="tagTitle" name="tagTitle"/> 
<field column="tagId" name="tagId"/> 
<field column="Issell" name="Issell"/> 
<field column="isdelete" name="isdelete"/> 
<field column="createtime" name="createtime"/> 
<field column="categoryMenuName" name="categoryMenuName"/> 
<field column="cmRecordid" name="cmRecordid"/> 
<field column="ImagePath" name="ImagePath"/> 
</entity>
  </document>
</dataConfig>

现在开始解释一下各个节点的意思(我的配置文件是根据oracle)

dataSource 节点用于配置数据库相关信息

    driver属性 驱动类  不同的数据库驱动配置不一样 具体百度一下 就可以啦。。

    url属性 则是数据库连接字符串 同理不痛数据库的都不一样

    user属性 数据库用户名

    passowrd属性 数据库密码

 entity表示一张表

   name 属性 逻辑名字 用于你区分这个表的名字

必须大写哦 涉及到id的都跟我一样配置就行了  

   query属性 第一次导入所查询的sql

transformer 这个配置是为了解决oralce导入clob字段不成功的问题

下面的则是增量配置)

  deltaQuery 查询数据库距离上一次同步数据库之后修改的所有数据id

  deletedPkQuery 查询删除的数据的字段id

  deltaImportQuery 根据上面2项查询的id来查询数据库 进行更新数据

   filed导入solr的字段 column 属性 数据库字段名字  name 则是映射到solr类型的name

   在tomcat增加自定义类型映射字段 tomcat根目录下的 solr\Colection1\conf\schema.xml

  在files节点下新增

  type为对应的数据类型  需要分词的字段type则为分词器的name前面配置的 

text_ik 则为ik分词器

text_ws(solr自带) 空格分词器 按空格分词 类似 splict(' ');

indexed表示是否索引  需要搜索的的字段才设置为true   stored 表示是否物理存储  (这边必须name必须跟上面数据导入配置的filed一一对应)

<!--
   自定义类型映射
              -->
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />  
    <field name="Recordid" type="string" indexed="true" stored="true"/>
 <field name="ProductName" type="text_ik" indexed="true" stored="true"/>
  <field name="details" type="text_ik" indexed="true" stored="true"/>
    <field name="productprice" type="double"  indexed="true" stored="true"/>
     <field name="CostPrice" type="double"  indexed="true" stored="true"/>
      <field name="WholesalePrice" type="double"  indexed="true" stored="true"/>
       <field name="MarketPrice" type="double"  indexed="true" stored="true"/>
    <field name="tagTitle" type="text_ws" indexed="true" stored="true"/>
     <field name="tagId" type="text_ws" indexed="true" stored="true"/>
     <field name="Issell" type="string" indexed="true" stored="true"/>
     <field name="isdelete" type="string" indexed="true" stored="true"/>
     <field name="createtime" type="date" indexed="true" stored="true"/>
      <field name="categoryMenuName" type="string" indexed="true" stored="true"/>
       <field name="cmRecordid" type="string" indexed="true" stored="true"/>
        <field name="ImagePath" type="string" indexed="true" stored="true"/>

 

下面讲下我当前的数据结构

 一对多关系 可以嵌套entity来解决 

 我这里用了自己的解决方案

表示一个商品可以有多个标签)  商品标签表 可以有多个

 

select p.recordid,strcatSpace(t.title) tagTitle,strcatSpace(t.recordid) tagId from product p left join OBJECTTAG o on p.recordid=o.objectrowid
left join tagattribute t on o.tagattributerowid=t.recordid
group by p.recordid

如果我一个商品打了2个标签  沙发 和 家具   那么查询出来就是商品id  tagTitle为:(沙发id  家具id)   tagtile则是(沙发 家具)  搜索的时候通过标签id去查沙发也可以查出这条数据信息 查家具也可以查出来

用空格的原因是我这2个字段用了空格分词器  每个空格隔开的当一个词)

最终导入的数据就变成了

solr 用户名密码springboot solr配置数据库_jar_02

网站实例http://www.iwo72.com/Product

配置完这些重启tomcat  访问solr 如果日志没有报错 的话执行导入命令

solr 用户名密码springboot solr配置数据库_数据库_03

        或者可以通过http接口  其实管理页面的execute也是请求这个接口   直接在浏览器中输入 http://192.168.0.9:8080/solr/collection1/dataimport?command=full-import  ip则为你配置的ip

    执行完后查看日志是否报错   如果没有报错 在通过query查询

solr 用户名密码springboot solr配置数据库_jar_04

 

 表示导入成功

后面将记录通过增量数据同步问题