首页 > 解决方案 > Apache Solr 映射自定义 JSON 无法索引嵌套文档

问题描述

我有某种问题,我无法按照文档解决它。我有这个自定义 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 ... />

我也试过 <field name="editors.given_name".... />

我正在映射的是:

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

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

谢谢。

标签: jsonsolrnested-documents

解决方案


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

  • 定义模式
  • 过帐嵌套文档
  • 查询嵌套文档

步骤1

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

  <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 不会创建用于存储的嵌套结构,而是在父级别创建所有文档。我们需要在父子文档之间建立逻辑关系,并在子文档转换器的帮助下从 solr 获取文档

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


推荐阅读