【问题标题】:Apache Solr mapping custom JSON can't index nested documentsApache Solr 映射自定义 JSON 无法索引嵌套文档
【发布时间】:2020-08-08 23:49:45
【问题描述】:

我有一些问题,我无法按照文档解决它。我有这个自定义 JSON,我无法将其展平,因为它来自外部源。我省略了部分代码以使其易于阅读和理解。这是我要索引的文档:

{
"status": "ok",
"message": {
    ... some other fields ...
    "title": "The good and the bad",
    "editor": [
        {"given": "Bianca", "family": "Racioppe", "sequence": "additional", "affiliation": [] },
        {"given": "Virginia", "family": "C\u00e1neva", "sequence": "additional", "affiliation": []}],
    ... other fields ...
}}

这是我的架构:

<field name="editors" type="string" multiValued="true" indexed="true" stored="true">
    <field name="given_name" type="string" indexed="true" stored="true"/>
    <field name="family_name" type="string" indexed="true" stored="true"/>
    <field name="affiliation" type="string" multiValued="true" indexed="true" stored="true"/>
    <field name="orcid" type="string" indexed="true" stored="true"/>
</field>
<field name="title" type="string" multiValued="false" indexed="true" stored="true"/>
<field... other fields ... />

我也试过

我正在映射的是:

curl -X POST "http://localhost:8983/solr/papers/update/json/docs?
split=/message|/message/autor|/message/editor
&f=editors:/message/editor
&f=editors.given_name:/message/editor/given
&f=editors.family_name:/message/editor/family
&f=editors.affiliation:/message/editor/affiliation/name
&f=title:/message/title
&f=.... other fields....
&commit=true" -H 'Content-type:application/json' --data-binary @file.json

索引对除“编辑器”字段之外的所有字段都正常工作,没有任何反应!我做错了什么或错过了什么?

谢谢。

【问题讨论】:

    标签: json solr nested-documents


    【解决方案1】:

    要在 Solr 中索引嵌套文档,您需要了解三个基本步骤

    • 定义架构
    • 发布嵌套文档
    • 查询嵌套文档

    第 1 步

    您只需要像对这样的 parents 字段定义相同的架构。您可以根据您的业务需求更改属性,即类型、存储、多值等

      <field name="editor" type="text_general"/>
      <field name="editors" type="string" multiValued="false"/>
      <field name="family" type="string" indexed="true" stored="true"/>
      <field name="given" type="string" indexed="true" stored="true"/>
      <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
      <field name="sequence" type="string" indexed="true" stored="true"/>
    

    第 2 步

    现在对于索引,您只需将所需的 json 发布到 solr。在此示例中,我仅出于示例目的而包含了较少的字段

    curl --location --request POST 'http://localhost:8983/solr/stackoverflow_core/update?commit=true&overwrite=true&wt=json' \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json' \
    --data-raw '[{
      "title": "The good and the bad",
      "editor": [
        {
          "given": "Bianca",
          "family": "Racioppe",
          "sequence": "additional"
        },
        {
          "given": "Virginia",
          "family": "Cu00e1neva",
          "sequence": "additional"
        }
      ]
    }]'
    

    第 3 步

    现在要在 solr 中查询嵌套文档,您需要使用块连接查询解析器和子文档转换器。您还需要知道 solr 不会创建用于存储的嵌套结构,而是在父级别创建所有文档。我们需要在child doc transformer

    的帮助下创建父子文档之间的逻辑关系并从solr获取文档

    https://lucene.apache.org/solr/guide/8_0/searching-nested-documents.html

    【讨论】:

    • "" 我不明白为什么这个字段存在。
    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多