我有两个 MySQL 表 book
和 author
,它们具有 多对多
关系,通过 book_author_mapper< 完成
其行包含列 book_id
/author_id
。
在 Solr 中,我有一个获取图书列表的查询,对于每本书,我需要获取该书的 author_id
数组。
目前,我正在考虑使用多值字段来存储图书 ID。
我的问题是:
- 字段怎么定义,DIH里面的SQL怎么写,好像需要多条SQL吧?谢谢。
- 如果我不仅要获取
author_id
列表,还要获取每个author_id
的author_name
,这可能吗?<
最佳答案
在查看文档和谷歌搜索后,我已经很好地解决了这个问题。
表格
- 书
- 作者
- book_author_map(这是多对多关系的中间表)
DIH配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull" user="root"
password="123456" />
<document>
<entity name="book" pk="id"
query="SELECT * FROM book where status = 0 limit 200000;"
deltaImportQuery="SELECT * FROM book where status = 0 and id='${dih.delta.id}' limit 200000;"
deltaQuery="select id from book where status = 0 and CONVERT_TZ(`update_date`, @@session.time_zone, '+00:00') > '${dih.last_index_time}'"
>
<entity name="author"
query="SELECT au.cn_name as author_cn_name FROM author AS au JOIN book_author_map AS bam ON au.id = bam.author_id WHERE bam.book_id = ${book.id} limit 10;"
>
<field name="authors" column="author_cn_name" />
</entity>
</entity>
</document>
</dataConfig>
字段定义
<field name="cn_name" type="textComplex" indexed="true" stored="true" />
<field name="en_name" type="textComplex" indexed="true" stored="true" />
<field name="status" type="int" indexed="true" stored="true" />
<field name="authors" type="textComplex" indexed="true" stored="true" multiValued="true" />
待办事项
parentDeltaQuery
它获取父实体的 pk,但是当它被调用时,做什么呢?有必要吗?deltaQuery
和parentDeltaQuery
在子实体中是否必要?
关于mysql - Solr - DIH 定义和导入多对多字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263896/