【问题标题】:Solr documents with child elements?带有子元素的 Solr 文档?
【发布时间】:2011-07-31 21:19:39
【问题描述】:

是否有可能创建一个包含子元素的 solr 文档?

例如,我将如何表示这样的事情:

<person first="Bob" last="Smith">
   <children>
      <child first="Little" last="Smith" />
      <child first="Junior" last="Smith" />
   </children>
</person>

解决这个问题的常用方法是什么?

【问题讨论】:

    标签: solr nested document structure


    【解决方案1】:

    从 Solr 4.7 和 4.8 开始,Solr 支持嵌套文档:

    {
    "id": "chapter1",
    "title" : "Indexing Child Documents in JSON",
    "content_type": "chapter",
    "_childDocuments_": [
      {
        "id": "1-1",
        "content_type": "page",
        "text": "ho hum... this is page 1 of chapter 1"
      },
      {
        "id": "1-2",
        "content_type": "page",
        "text": "more text... this is page 2 of chapter 1"
      }
    ]
    }
    

    请参阅Solr release notes 了解更多信息。

    【讨论】:

    • 如何将这个结构与haystack一起使用?
    • @whomer : 你确定 SOLR 在 4.7 版本中支持子文档吗??
    【解决方案2】:

    您可以根据您的搜索/分面需求以不同的方式对此进行建模。通常您会使用多值或动态字段。在接下来的示例中,我将省略字段类型、索引和存储标志:

    <field name="first"/>
    <field name="last"/>
    <field name="child_first" multiValued="true"/>
    <field name="child_last" multiValued="true"/>
    

    您可以将孩子的名字和姓氏关联起来。或者您可以将两者都放在一个字段中:

    <field name="first"/>
    <field name="last"/>
    <field name="child_first_and_last" multiValued="true"/>
    

    另一个:

    <field name="first"/>
    <field name="last"/>
    <dynamicField name="child_first_*"/>
    <dynamicField name="child_last_*"/>
    

    在这里,您将存储字段“child_first_1”、“child_last_1”、“child_first_2”、“child_last_2”等。再次由您决定关联值,但至少您有一个索引。使用一些代码,您可以使其变得透明。

    底线:正如Solr wiki 所说:“Solr 提供一个表。在索引中存储一组数据库表通常需要对某些表进行非规范化。避免非规范化的尝试通常会失败。”您可以根据搜索需求对数据进行非规范化处理。

    更新:自 4.5 版左右以来,Solr 直接支持嵌套文档:https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-BlockJoinQueryParsers

    【讨论】:

    • 谢谢毛里西奥。选项 #1 和 #2 并不是真正有用,因为这样就无法提取单个字段,特别是如果有两个以上的字段。您的第三个建议可能会起作用,使用动态字段。我将使用 DataImportHandler 中的什么机制来生成这些动态字段?
    • @user332523:如果您仅限于使用 DataImportHandler,这可能是不可能的......但如果您在自己的编码过程中导入,这很容易做到。
    • 您好毛里西奥,感谢您的回复。您是指使用 Solr API 将文档添加到索引的自定义数据导入器吗?我确实在 Solr DIH 文档中读到了一些可能能够创建动态字段的内容 [wiki.apache.org/solr/DataImportHandler#TemplateTransformer]
    • @user332523:是的,通常编写自己的数据导入器比 DIH 灵活得多。 DIH 提供的是简单性和零编码,可以快速上手,但如果还不够,我会毫不犹豫地放弃它。
    • 感谢您的信息。猜猜定制 DIH 是我们必须去的地方......干杯。
    【解决方案3】:

    为子项设置单独的字段会导致误报匹配。连接字段在某种意义上起作用,但它确实是有限的方法。我们在http://blog.griddynamics.com/2011/06/solr-experience-search-parent-child.htmlhttp://blog.griddynamics.com/2011/06/solr-experience-search-parent-child.html博客上的类似任务有很多经验

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 2014-09-26
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多