【问题标题】:How do I get all values for a nested field using elasticsearch?如何使用 elasticsearch 获取嵌套字段的所有值?
【发布时间】:2021-07-24 04:54:39
【问题描述】:

我能够在弹性搜索 6.8 中使用聚合查询获取数据库中文本字段的所有值:

{
  aggs: {
    values: {
      terms: { field: 'City.keyword', size: 200 },
    },
  },
  size: 0,
};

我正在尝试对嵌套字段做同样的事情。

以下是文本字段(城市)和嵌套字段(冷却)的示例

"City": "Fredericksburg",
"Cooling": [
    {
        "key": "Fuel",
        "value": "Electric"
    },
    {
        "key": "Central Air",
        "value": "Y"
    },
    {
        "key": "Central A/C",
        "value": "true"
    }
],

这是我一直在参考的文档: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-aggregations-bucket-terms-aggregation.html

以下是相关映射:

{
  "listings:366": {
    "mappings": {
      "_default_": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          ...
          {
            "Cooling": {
              "path_match": "Cooling.*",
              "mapping": {
                "type": "text"
              }
            }
          },
          ...
        ],
        "properties": {
          ...
          "Cooling": {
            "type": "nested"
          },
          ...
        }
      },
      "listings": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          ...
          {
            "Cooling": {
              "path_match": "Cooling.*",
              "mapping": {
                "type": "text"
              }
            }
          },
          ...
        ],
        "properties": {
          ...
          "Cooling": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "text"
              },
              "value": {
                "type": "text"
              }
            }
          },
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果Coolingnested 类型,那么您可以通过首先将terms 聚合嵌套在top-level nested aggregation 中以“深入”Cooling 嵌套文档来实现您想要的效果:

    {
      "size": 0,
      "aggs": {
        "cooling": {
          "nested": {
            "path": " Cooling"
          },
          "aggs": {
            "values": {
              "terms": {
                "field": "Cooling.key",  <--- make sure this field is mapped as keyword
                "size": 200
              }
            }
          }
        }
      }
    }
    

    更新

    您的映射需要更改为:

        "properties": {
          ...
          "Cooling": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "keyword"       <--- change this
              },
              "value": {
                "type": "text"
              }
            }
          },
        }
    

    【讨论】:

    • 谢谢,这看起来是正确的,但它仍然在 buckets 数组中返回零个桶。
    • 您能否使用索引的真实映射更新您的问题,请使用GET your-index-name
    • 我添加了相关的映射,希望这就是你要找的。​​span>
    • 好的,问题是Cooling.key应该匹配为keyword而不是text,否则聚合将不起作用
    • 请重新阅读我的评论。您的字段是text 类型,它必须是keyword 类型,否则它将不起作用。查看我的更新答案
    【解决方案2】:

    尝试使用复合聚合: 见composite Aggregation

    【讨论】:

    • 谢谢。我已经尝试过文档该部分中列出的示例,但除了空桶之外,无法让它们返回任何内容。你能提供一个使用我提供的场景的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多