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

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

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

        两个表结构如下:

        表一:

       表二:

        1.2 配置data-config.xml
        我们之前已经配置好了solr服务器,所以只需要修改数据源配置文件和索引配置文件即可。数据源文件配置如下:

<dataSource name="jfinal_demo" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
url="jdbc:mysql://192.168.21.20:3306/jfinal_demo" user="root" password="123456" batchSize="-1" />
  	<document name="testDoc">
		<entity name="user" dataSource="jfinal_demo" pk="id" query="select * from user">
			<field column="id" name="userId"/>
     	<span style="white-space:pre">	</span><field column="userName" name="userName"/>
			<field column="userAge" name="userAge"/>
			<field column="userAddress" name="userAddress"/>
		</entity>
		<entity name="role" pk="id" query="select * from role" >
			<field column="id" name="roleId"/>
			<field column="name" name="roleName"/>
		</entity>
	</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配置如下:

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.21.20:3306/jrkj_java" user="root" password="123456" batchSize="-1" />
	<document name="testDoc">
		<entity name="tj_student" pk="id" query="SELECT id,nickName,isDelete FROM tj_student where isDelete = 0 and applyTeacherState = 1 and isTeacher = 1">
			<field column="id" name="id"/>
			<field column="nickName" name="nickName"/>
			<field column="isDelete" name="isDelete"/>
			<entity name="tj_course" pk="id" query="select id,courseName from tj_course where isDelete=0 and courseAuditState=1 and studentId='${tj_student.id}'">
				<field column="id" name="courseId"/>
				<field column="courseName" name="courseName"/>
 			</entity>
			<entity name="tj_userfield" pk="id" query="select fieldId,studentId from tj_userfield where isDelete = 0 and userFieldType = 1 and studentId = '${tj_student.id}'">
				<field column="fieldId" name="fieldId"/>
				<field column="studentId" name="studentId"/>
				<entity name="tj_field" pk="id" query="select fieldName,pointWord from tj_field where isDelete = 0 and id='${tj_userfield.fieldId}'">
					<field column="fieldName" name="fieldName"/>
					<field column="pointWord" name="pointWord"/>
				</entity>
			</entity>
			<entity name="tj_userIndustry" pk="id" query="select industryId from tj_userindustry where isDelete = 0 and userIndustryType = 1 and studentId = '${tj_student.id}'">
				<field column="industryId"/>
				<entity name="tj_industry" pk="id" query="select industryName from tj_industry where isDelete = 0 and id = '${tj_userIndustry.industryId}'">
					<field column="industryName" name="industryName"/>
				</entity>
			</entity>
		</entity>
  </document>

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

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.21.20:3306/jrkj_java" user="root" password="123456" batchSize="-1" />
	<document name="testDoc">
		<entity name="tj_student" pk="id" query="SELECT id,nickName,isDelete FROM tj_student where isDelete = 0 and applyTeacherState = 1 and isTeacher = 1">
			<entity name="tj_course" pk="id" query="select id,courseName from tj_course where isDelete=0 and courseAuditState=1 and studentId='${tj_student.id}'">
 			</entity>
			<entity name="tj_userfield" pk="id" query="select fieldId,studentId from tj_userfield where isDelete = 0 and userFieldType = 1 and studentId = '${tj_student.id}'">
				<entity name="tj_field" pk="id" query="select fieldName,pointWord from tj_field where isDelete = 0 and id='${tj_userfield.fieldId}'">
				</entity>
			</entity>
			<entity name="tj_userIndustry" pk="id" query="select industryId from tj_userindustry where isDelete = 0 and userIndustryType = 1 and studentId = '${tj_student.id}'">
				<entity name="tj_industry" pk="id" query="select industryName from tj_industry where isDelete = 0 and id = '${tj_userIndustry.industryId}'">
				</entity>
			</entity>
		</entity>
  </document>

        2.3 配置schema.xml

        配置如下:

        2.4 重启solr服务,测试。

        三 总结

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

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

猜你喜欢

转载自blog.csdn.net/u010942465/article/details/51339970