【问题标题】:Solr - DIH define & import many-to-many fieldSolr - DIH 定义和导入多对多字段
【发布时间】:2015-08-28 05:28:12
【问题描述】:

我有两个 MySQL 表 bookauthor,它们有 many-to-many 关系,通过 book_author_mapper 完成,其行包含列 book_id / author_id

在 Solr 中,我有一个查询来获取图书列表,对于每本书,我需要为这本书获取一个 author_id 数组。

目前,我正在考虑使用多值字段来存储书籍 ID。

我的问题是:

  • 字段怎么定义,DIH怎么写SQL,好像需要多条SQL吧?谢谢。
  • 如果我不仅想获得author_id 列表,还想获得每个author_idauthor_name,这可能吗?

【问题讨论】:

  • 到目前为止你尝试过什么?你的架构怎么样,你的 data-import.xml 怎么样?这不是给我代码的地方:)
  • 你检查过full sample in the Solr wiki吗?他们还在那里解决了 n:m 关系。表item_category 是它们的关系表。
  • @cheffe 我会先查看 solr wiki & doc,谢谢。
  • @cheffe 我查过文档,可能找到了解决办法,并给出了答案,希望是正确的,你能看看,或者给出一个答案来纠正它。

标签: mysql solr lucene dih


【解决方案1】:

查看文档和谷歌搜索后,我已经解决了问题。

表格

  • 作者
  • 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&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 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,但是什么时候调用,怎么办?有必要吗?
  • 子实体中是否需要deltaQueryparentDeltaQuery

【讨论】:

  • 这对我来说看起来不错。我唯一注意到的是,您的 dih-config 中只有一个 &lt;field .. /&gt; 映射,但您确实在架构中总共声明了 4 个字段。但它可以按预期工作吗?
  • 是的,它按预期工作,sql结果的列名与name属性同名,所以可以忽略它们,但正如你所说,我认为将所有 放在适当的 中是一个好习惯。
猜你喜欢
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
相关资源
最近更新 更多