老鬼的博客 来都来啦,那就随便看看吧~
solr篇六:对db_product进行增量更新索引
发布于: 2019-12-07 更新于: 2023-11-08 分类于:  阅读次数: 

一:相关参考文档

二:介绍

1
2
前几篇介绍的同步数据都是进行full-import(全量更新),本篇将进行增量更新数据,
新创建了一个db_product的core进行演示。

三:创建db_product

1
关于如何创建新的db_product,该篇不做阐述,不懂得可以参考其他文章

四:配置增量更新

  • db-data-config.xml
1
2
db-data-config.xml的基本配置和db_core的配置一样,唯一不同的就是要使用deltaImportQuery和deltaQuery,
具体的配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dataConfig>
<!-- 定义datasource,driver是驱动,url是数据库的连接地址,user是用户名,password是密码 -->
<dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:51216;DatabaseName=db_solr" user="sa" password="EIKYJnnifZgBRMa0" />
<document>

<!-- 定义实体类,这里的定义可以像java定义entity一样 -->
<!-- query是做full-import数据导入使用,根据需求定义的query sql -->
<!-- column是数据库的字段名,name是solr中定义的对应的名称,我这里定义的是和java entity一致的 -->

<!-- Product实体 -->
<!-- '${dataimporter.delta.id}'是取到deltaQuery查询得到的id数据然后,deltaImportQuery。根据id匹配的数据执行增量导入 -->
<entity name="Product" pk="id" transformer="RegexTransformer"
query="select * from solr_product;"
deltaImportQuery="SELECT * from solr_product where id = '${dataimporter.delta.id}'"
deltaQuery="SELECT id from solr_product where update_time > '${dataimporter.last_index_time}';">
<field column="id" name="id" />
<field column="product_name" name="productName" />
<field column="product_desc" name="productName" />
<field column="insert_time" name="insertTime" />
<field column="update_time" name="updateTime" />
</entity>
</document>
</dataConfig>
  • managed-schema
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" useSmart="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" useSmart="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

<!-- 添加索引字段 (renjie) -->
<uniqueKey>id</uniqueKey>
<field name="productName" type="string" indexed="true" stored="true" />
<field name="productDesc" type="string" indexed="true" stored="true" />
<field name="insertTime" type="pdate" indexed="true" stored="true" />
<field name="updateTime" type="pdate" indexed="true" stored="true" />
<!-- 索引复制,联合索引 -->
<field name="keyword" type="text_ik" indexed="true" stored="true" omitNorms="true" multiValued="true"/>
<copyField source="id" dest="keyword" maxChars="30000"/>
<copyField source="productName" dest="keyword" maxChars="30000"/>
<copyField source="productDesc" dest="keyword" maxChars="30000"/>
<!-- end -->
  • 备注
1
2
3
db_product和db_core的配置基本一样,区别有两点:
1.db_core用的表是solr_person,db_product用的表是solr_product
2.db-data-config.xml配置不一样,db_product增加了deltaImportQuery和deltaQuery的配置

五:admin ui

  • 使用full-import做全量初始化
  • 数据库新增数据
  • 使用delta-import做增量更新
*************感谢您的阅读*************