简体   繁体   中英

Solr - DIH define & import many-to-many field

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 as author_name for each author_id , is that possible?

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&amp;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')  &gt; '${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 and parentDeltaQuery necessary in sub entity?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM