处理“nested”类型的嵌套对象的正确方法

问题描述 投票:0回答:1

我有一个类型为“嵌套”的数组字段。在这个领域我有多个对象。这是 JSON 示例:

{
  "users" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

我希望索引后有相同的结构。这是我的映射的一部分:

},
"users": {
    "dynamic": false,
    "type": "nested",
    "properties": {
        "first": {
            "type": "text"
         },
         "last": {
             "type": "text"
        },
    }
}

但是在索引之后我得到了这个结构:

    
users
    
[
  {
    "first": [
      "John"
    ],
    "last": [
      "Smith"
    ],
  },
  {
    "first": [
      "Alice"
    ],
    "last": [
      "White"
    ],
  },]

我做错了什么?这种行为是预期的吗? 我如何处理嵌套对象?

我只需要文本字段,而不需要数组中的文本字段。搜索工作正常,但映射后的数据格式令人困惑。

删除索引、更改映射、刷新索引。 设置动态:true 和 false。更改字段类型。

elasticsearch kibana elasticsearch-mapping
1个回答
0
投票

它按我的预期工作。我正在使用 ES v8。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "users": {
        "dynamic": false,
        "type": "nested",
        "properties": {
          "first": {
            "type": "text"
          },
          "last": {
            "type": "text"
          }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "users" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "users",
      "query": {
        "bool": {
          "must": [
            { "match": { "users.first": "Alice" }},
            { "match": { "users.last":  "White" }} 
          ]
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.