首页 > 解决方案 > 查询嵌套字段时 Elasticsearch 返回错误

问题描述

我在弹性搜索中有一些索引数据,我正在尝试使用邮递员使用以下请求获取数据。

{
"_source": ["_id"],
"query":  {
    "nested" : {
        "path" : "data",
        "query" : {
            "bool" : {
                "must" : [
                { "match" : {"data.id": "3456"} }
                ]
            }
        },
        "score_mode" : "avg"
    }
}

}

但我得到了例外

[nested] nested object under path [data] is not of nested type

我的映射定义就像

    {
  "property": {
    "mappings": {
      "property": {
        "properties": {
          "data": {
            "properties": {
              "id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "point": {
            "properties": {
              "lat": {
                "type": "float"
              },
              "lon": {
                "type": "float"
              }
            }
          },
          "popId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

知道为什么会这样吗?

标签: elasticsearch

解决方案


Fielddata是一个对象,所以它应该是nested类型。

嵌套类型

请尝试以下映射配置:

PUT property
{
  "mappings": {
    "property": {
      "properties": {
        "point": {
          "properties": {
            "lat": {
              "type": "float"
            },
            "lon": {
              "type": "float"
            }
          }
        },
        "popId": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "data": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
}

您必须删除索引并使用新的映射配置重新创建它。

如果您不想删除数据,这可能会有所帮助

希望能帮助到你


推荐阅读