首页 > 解决方案 > Elasticsearch 将嵌套的 json 视为纯字符串:无法在 start_object 上获取文本

问题描述

我正在尝试添加一些弹性的 json 文档,它们具有深层嵌套的 json 结构。

我想将其中一些嵌套字段视为普通的未索引text而不是 jsonskip,或者它们是不可能的。

例如,我有这样的event文件:

{
    "cluster": "production1",
    "header": {
        "os": "ios",
        "experiments": {
            "e1": {
                "field1": "value1",
                "field2": "value2"
                "array": ["id0"]
            },
            "e2": {
                "field3" : "value3",
                "array": ["id1", "id2", "id3"]
            }
        }
    },
}

我希望header.experiments字段下的所有内容都被视为一个纯文本。
我试过这个映射,但它对我不起作用,因为experimentsfield 是 object 而不是 string: error: can't get text on a start_object

"mappings": {
    "event": {
      "properties": {
        "cluster": {
          "type": "keyword",
          "index": true
        },
        "header": {
          "os": {
            "type": "keyword",
            "index": true
          },
          "experiments": {
            "type": "text",  // This does not work 
            "index": false
          }
        }
      }
    }
}

如何让 ElasticSearch 将子字段视为纯文本?

标签: jsonelasticsearchmapping

解决方案


要完全跳过该字段,您可以在该字段的索引映射中设置enabled为:falseexperiments

"mappings": {
  "event": {
    "properties": {
      "cluster": {
        "type": "keyword",
        "index": true
      },
      "header": {
        "os": {
          "type": "keyword",
          "index": true
        },
        "experiments": {
          "enabled": false
        }
      }
    }
  }
}

enabled设置只能应用于映射类型和object字段,导致 Elasticsearch 完全跳过对字段内容的解析。JSON 仍然可以从该_source字段中检索,但不可搜索或以任何其他方式存储:

有关详细信息,请参阅:
https ://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html


推荐阅读