全文索引----创建多表solr索引

本文介绍如何在Solr中实现多个数据表的联合索引,包括无关联多表和有关联表的索引创建过程,详细展示了data-config.xml和schema.xml的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在使用solr作为索引服务器时,通常会将多个表的多个字段作为联合索引,对多个表快速的数据查询也是solr服务器高效率的体现。这片文章介绍下如何基于多个数据表创建索引。
        一 无关联多表创建索引
        1.1 数据库准备

        我们使用任意的两个表作为数据源,这两个表可以属于一个数据库,也可以属于不同的数据库,如果使用两个数据库,则需要两个数据源连接字符串,我们这里使用同一个库的两个表作为示例。

        两个表结构如下:

        表一:


       表二:


        1.2 配置data-config.xml
        我们之前已经配置好了solr服务器,所以只需要修改数据源配置文件和索引配置文件即可。数据源文件配置如下:
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dataSource name="jfinal_demo" type="JdbcDataSource" driver="com.mysql.jdbc.Driver"   
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. url="jdbc:mysql://192.168.21.20:3306/jfinal_demo" user="root" password="123456" batchSize="-1" />  
  2.     <document name="testDoc">  
  3.         <entity name="user" dataSource="jfinal_demo" pk="id" query="select * from user">  
  4.             <field column="id" name="userId"/>  
  5.         <span style="white-space:pre">    </span><field column="userName" name="userName"/>  
  6.             <field column="userAge" name="userAge"/>  
  7.             <field column="userAddress" name="userAddress"/>  
  8.         </entity>  
  9.         <entity name="role" pk="id" query="select * from role" >  
  10.             <field column="id" name="roleId"/>  
  11.             <field column="name" name="roleName"/>  
  12.         </entity>  
  13.     </document>  


        说明:dataSource标签是数据库连接字符串,name属性作为连接字符串标识符,type是数据源类型,我们这里选用jdbc数据源JdbcDataSource,drive是数据驱动,选择MySQL数据驱动,URL是数据库连接字符串。
       document标签下添加我们需要索引的数据,entity代表一个实体,name属性用来区分不同的实体,pk属性时数据表的主键,这个属性必须要和数据表主键一致,不能修改。
        field标签是需要索引的字段,column是数据列,name是别名。
        注意:设置主键是需要特别注意,如果两个表的主键数据一致,则后面的索引会覆盖掉前边的索引,但是很多情况是,我们使用自增长的数据作为主键,这样不可避免的就会产生主键一致情况,百度了一下,解决方法大致有两种:
        1 主键使用uuid格式,从根本上避免逐渐一致情况,并且比较安全。
        2 去掉schema.xml中uniqueKey属性,或者在表中新建一个字段作为uniqueKey属性。
        1.3 配置schema.xml
        将表中所有需要索引的字段添加到文件中fields标签下,注意,相同的字段就不想要添加了,例如ID。配置如下:


        1.4 重启solr服务,测试。
  
        二 关联表创建索引
        有关联表创建索引的步骤和无关联表一致,只是data-config.xml配置不同,具体如下。

        2.1 数据库结构图如下:


        2.2 data-config.xml配置如下:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.21.20:3306/jrkj_java" user="root" password="123456" batchSize="-1" />  
  2.     <document name="testDoc">  
  3.         <entity name="tj_student" pk="id" query="SELECT id,nickName,isDelete FROM tj_student where isDelete = 0 and applyTeacherState = 1 and isTeacher = 1">  
  4.             <field column="id" name="id"/>  
  5.             <field column="nickName" name="nickName"/>  
  6.             <field column="isDelete" name="isDelete"/>  
  7.             <entity name="tj_course" pk="id" query="select id,courseName from tj_course where isDelete=0 and courseAuditState=1 and studentId='${tj_student.id}'">  
  8.                 <field column="id" name="courseId"/>  
  9.                 <field column="courseName" name="courseName"/>  
  10.             </entity>  
  11.             <entity name="tj_userfield" pk="id" query="select fieldId,studentId from tj_userfield where isDelete = 0 and userFieldType = 1 and studentId = '${tj_student.id}'">  
  12.                 <field column="fieldId" name="fieldId"/>  
  13.                 <field column="studentId" name="studentId"/>  
  14.                 <entity name="tj_field" pk="id" query="select fieldName,pointWord from tj_field where isDelete = 0 and id='${tj_userfield.fieldId}'">  
  15.                     <field column="fieldName" name="fieldName"/>  
  16.                     <field column="pointWord" name="pointWord"/>  
  17.                 </entity>  
  18.             </entity>  
  19.             <entity name="tj_userIndustry" pk="id" query="select industryId from tj_userindustry where isDelete = 0 and userIndustryType = 1 and studentId = '${tj_student.id}'">  
  20.                 <field column="industryId"/>  
  21.                 <entity name="tj_industry" pk="id" query="select industryName from tj_industry where isDelete = 0 and id = '${tj_userIndustry.industryId}'">  
  22.                     <field column="industryName" name="industryName"/>  
  23.                 </entity>  
  24.             </entity>  
  25.         </entity>  
  26.   </document>  

        除了上面这种形式,官网给了第二种方式,结构如下:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.21.20:3306/jrkj_java" user="root" password="123456" batchSize="-1" />  
  2.     <document name="testDoc">  
  3.         <entity name="tj_student" pk="id" query="SELECT id,nickName,isDelete FROM tj_student where isDelete = 0 and applyTeacherState = 1 and isTeacher = 1">  
  4.             <entity name="tj_course" pk="id" query="select id,courseName from tj_course where isDelete=0 and courseAuditState=1 and studentId='${tj_student.id}'">  
  5.             </entity>  
  6.             <entity name="tj_userfield" pk="id" query="select fieldId,studentId from tj_userfield where isDelete = 0 and userFieldType = 1 and studentId = '${tj_student.id}'">  
  7.                 <entity name="tj_field" pk="id" query="select fieldName,pointWord from tj_field where isDelete = 0 and id='${tj_userfield.fieldId}'">  
  8.                 </entity>  
  9.             </entity>  
  10.             <entity name="tj_userIndustry" pk="id" query="select industryId from tj_userindustry where isDelete = 0 and userIndustryType = 1 and studentId = '${tj_student.id}'">  
  11.                 <entity name="tj_industry" pk="id" query="select industryName from tj_industry where isDelete = 0 and id = '${tj_userIndustry.industryId}'">  
  12.                 </entity>  
  13.             </entity>  
  14.         </entity>  
  15.   </document>  

        2.3 配置schema.xml

        配置如下:


        2.4 重启solr服务,测试


        三 总结

        多表索引是solr服务器最常见的索引方式,因为索引关键字很容易重复,特别是主键容易出错,所以创建索引时需要非常小心。


声明:如无特殊声明,本系列博客以solr-4.7.2版本为例,如有错误,敬请斧正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值