问题
I've two MySQL tables book
and author
, they have many-to-many
relationship, done via book_author_mapper
whose row contain columns book_id
/ author_id
.
In Solr, I have a query to get book list, for each book I need to get an array of author_id
for the book.
Currently, I am thinking about to use a multi-valued field to store book ids.
My question is:
- How to define the field, and how to write the SQL in DIH, it seems need multiple SQL, right? Thx.
- If I want to get not just the
author_id
list, but as well asauthor_name
for eachauthor_id
, is that possible?
回答1:
After viewing doc & googling, I have kind solved the problem.
Tables
- book
- author
- book_author_map (this is the middle table for many-to-many relationship)
DIH config file
<?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 definition
<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" />
TODOs
parentDeltaQuery
It get pk of parent entity, but when it is called, and what is do? Is that necessary?- Does
deltaQuery
andparentDeltaQuery
necessary in sub entity?
来源:https://stackoverflow.com/questions/32263896/solr-dih-define-import-many-to-many-field