如何保持子实体中字段的关系
How to keep relation of fields in a child entity
我有两个表 customers 和 fees,按 customer.id = fees.customerId
关联 1xn。 fees 还有另外两列,length
和 rate
。我的 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-数据-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 的平面架构,length
和 rate
最终彼此 'dissociated',作为两个列表。我想要一个单对列表,所以列表 <length, rate>
。我目前的解决方案是将数据配置更改为
<entity name="fees" query="SELECT CONCAT(length, ';', rate) AS lengthRatePair FROM fees WHERE id=${customer.id}">
<field column="lengthRatePair" name="lengthRatePair"/>
</entity>
然后在客户端用 ; 分割字段。但我认为一定有更优雅的解决方案。
这样做的正确方法是什么?
Solr / Lucene 将在索引时将值的序列保留在多值字段中,因此即使它们最终为 "disassociated",length
中的第一个值将与第一个相关rate
的值 - 因此您可以使用相同的索引来查找两个字段中的值。这意味着在两个字段中索引相同数量的条目很重要。
您的其他解决方案也可以工作 - 无论您选择什么,您都必须在查询层中进行处理。
将它作为子文档来做听起来有点矫枉过正,因为这会使几乎所有其他方面变得复杂,而不会在 return 中为您提供任何刚刚存储的值。
第三种解决方案是将序列化的 json 结构放入字段中以将其也附加到文档中,但我更喜欢第一种解决方案。我们向查询层添加了一个配置设置,它告诉查询层应该将查询结果中的哪些字段合并到一个条目中,以便显示/视图层接收一个具有 {length: .., rate: ...}
哈希值的对象作为属性。
我有两个表 customers 和 fees,按 customer.id = fees.customerId
关联 1xn。 fees 还有另外两列,length
和 rate
。我的 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-数据-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 的平面架构,length
和 rate
最终彼此 'dissociated',作为两个列表。我想要一个单对列表,所以列表 <length, rate>
。我目前的解决方案是将数据配置更改为
<entity name="fees" query="SELECT CONCAT(length, ';', rate) AS lengthRatePair FROM fees WHERE id=${customer.id}">
<field column="lengthRatePair" name="lengthRatePair"/>
</entity>
然后在客户端用 ; 分割字段。但我认为一定有更优雅的解决方案。
这样做的正确方法是什么?
Solr / Lucene 将在索引时将值的序列保留在多值字段中,因此即使它们最终为 "disassociated",length
中的第一个值将与第一个相关rate
的值 - 因此您可以使用相同的索引来查找两个字段中的值。这意味着在两个字段中索引相同数量的条目很重要。
您的其他解决方案也可以工作 - 无论您选择什么,您都必须在查询层中进行处理。
将它作为子文档来做听起来有点矫枉过正,因为这会使几乎所有其他方面变得复杂,而不会在 return 中为您提供任何刚刚存储的值。
第三种解决方案是将序列化的 json 结构放入字段中以将其也附加到文档中,但我更喜欢第一种解决方案。我们向查询层添加了一个配置设置,它告诉查询层应该将查询结果中的哪些字段合并到一个条目中,以便显示/视图层接收一个具有 {length: .., rate: ...}
哈希值的对象作为属性。