内嵌子文档

Solr 内嵌子文档在索引时使用 “Block Join” 来作为一种建模包含其它文档的文档的方式。 如博客内容作为父文档而其评论作为子文档,或者产品作为父文档而尺寸、颜色或其它变量作为子文档。 在查询时, BlockJoinQueryParsers 可用于在这些关系上进行搜索。 在性能上,索引文档之间的关系可能比试图在查询时进行 join 更加高效, 因为关系已经存储于索引中且不需要重新计算。

内嵌文档可以通过 XML 或 JSON 数据语法(或使用 SolrJ )来索引。 但不管语法如何,你可以包含一个字段来表示父文档为父; 它可以是任何适合该目的的字段,且它将用作 BlockJoinQueryParsers 的输入。

XML 示例

如,下面有两个文档及其子文档:

  1. <add>
  2. <doc>
  3. <field name="id">1</field>
  4. <field name="title">Solr adds block join support</field>
  5. <field name="content_type">parentDocument</field>
  6. <doc>
  7. <field name="id">2</field>
  8. <field name="comments">SolrCloud supports it too!</field>
  9. </doc>
  10. </doc>
  11. <doc>
  12. <field name="id">3</field>
  13. <field name="title">New Lucene and Solr release is out</field>
  14. <field name="content_type">parentDocument</field>
  15. <doc>
  16. <field name="id">4</field>
  17. <field name="comments">Lots of new features</field>
  18. </doc>
  19. </doc>
  20. </add>

这里,我们有被索引的父文档,其具有字段 content_type,其值为 “parentDocument”。 我们也可以使用一个布尔字段,如 isParent,值为 true,或其它类似的方式。

JSON 示例

本例等价于上面的 XML 示例,注意需要特殊的 _childDocuments_ 键来指示 JSON 中的内嵌文档。

  1. [
  2. {
  3. "id": "1",
  4. "title": "Solr adds block join support",
  5. "content_type": "parentDocument",
  6. "_childDocuments_": [
  7. {
  8. "id": "2",
  9. "comments": "SolrCloud supports it too!"
  10. }
  11. ]
  12. },
  13. {
  14. "id": "3",
  15. "title": "New Lucene and Solr release is out",
  16. "content_type": "parentDocument",
  17. "_childDocuments_": [
  18. {
  19. "id": "4",
  20. "comments": "Lots of new features"
  21. }
  22. ]
  23. }
  24. ]

注:一个索引内嵌文档的限制是父子文档的整个块在任何需要变更的时候需要被一起更新。 或者说,即使只有单个子文档或单个父文档有变更,整个父子文档块需要一起更新。