【问题标题】:How to keep relation of fields in a child entity如何保持子实体中的字段关系
【发布时间】:2016-12-18 06:30:52
【问题描述】:

我有两张表客户和费用,由customer.id = fees.customerId 关联 1xn。费用还有另外两列,lengthrate。我的 solr 配置是:

schema.xml

<schema name="customers" version="1.5">
    <field name="_version_" type="long" indexed="true" stored="true"/>
    <field name="id" type="long" indexed="true" stored="true"/>
    <field name="customerName" type="string" indexed="false" stored="true"/>
    <field name="length" type="int" indexed="false" stored="true" multiValued="true"/>
    <field name="rate" type="float" indexed="false" stored="true" multiValued="true"/>

    <uniqueKey>id</uniqueKey>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="string" class="solr.StrField"/>
</schema>

solr-data-config.xml

<entity name="customer" query="SELECT * FROM customers">
    <field column="id" name="id"/>
    <field column="name" name="customerName"/>

    <entity name="fees" query="SELECT length, rate FROM fees WHERE id=${customer.id}">
       <field column="length" name="length"/>
        <field column="rate" name="rate"/>
    </entity>
</entity>

问题在于,由于 Solr 的平面模式,lengthrate 最终会“分离”为两个列表。我想要一个单对列表,所以一个列表&lt;length, rate&gt;。我目前的解决方案是将数据配置更改为

<entity name="fees" query="SELECT CONCAT(length, ';', rate) AS lengthRatePair FROM fees WHERE id=${customer.id}">
   <field column="lengthRatePair" name="lengthRatePair"/>
</entity>

然后在客户端用 ; 分割字段。但我认为必须有一个更优雅的解决方案。

这样做的正确方法是什么?

【问题讨论】:

    标签: xml solr schema parent-child


    【解决方案1】:

    Solr / Lucene 将值序列保留在多值字段中,因为它们被索引,因此即使它们最终“解除关联”,length 中的第一个值将与rate 的第一个值相关 -因此您可以使用相同的索引来查找两个字段中的值。这意味着在两个字段中索引相同数量的条目非常重要。

    您的其他解决方案也可以工作 - 无论您选择什么,您都必须在查询层中处理它。

    将其作为子文档执行听起来有点矫枉过正,因为这会使几乎所有其他方面变得复杂,而不会为您提供任何回报以换取刚刚存储的值。

    第三种解决方案是将序列化的 json 结构放入一个字段中以将其附加到文档中,但我更喜欢第一种解决方案。我们向查询层添加了一个配置设置,它告诉查询层应该将查询结果中的哪些字段合并到单个条目中,以便显示/视图层接收具有{length: .., rate: ...} 哈希作为属性之一的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 2021-06-04
      • 2019-01-15
      • 2017-09-30
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多