【问题标题】:Elasticsearch aggregation with reverse_nested path parameter具有 reverse_nested 路径参数的 Elasticsearch 聚合
【发布时间】:2017-10-08 11:06:37
【问题描述】:

谁能详细说明elasticsearch聚合中reverse_nested标签中的“路径”参数?我正在尝试使用不同嵌套级别的键来聚合嵌套桶。以下是详细信息:

使用以下映射创建索引

PUT agg
{
  "mappings": {
    "sample": {
      "properties": {
        "product": {
          "type": "object",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "category": {
              "type": "keyword"
            }
          }
        },
        "features": {
          "type": "nested",
          "properties": {
            "color": {
              "type": "keyword"
            },
            "details": {
              "type": "text"
            },
            "finish": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

在“agg”索引中索引一些文档:

POST _bulk
{ "index" : { "_index" : "agg", "_type" : "sample", "_id" : "1" } }
{"product":{"name":"tv","category":"electronics"},"features":[{"color":"black","details":"jet black in color"},{"finish":"matte"}]}
{ "index" : { "_index" : "agg", "_type" : "sample", "_id" : "2" } }
{"product":{"name":"tv","category":"electronics"},"features":[{"color":"black","details":"jet black in color"},{"finish":"glossy"}]}
{ "index" : { "_index" : "agg", "_type" : "sample", "_id" : "3" } }
{"product":{"name":"tv","category":"electronics"},"features":[{"color":"red","details":"apple red in color"},{"finish":"matte"}]}
{ "index" : { "_index" : "agg", "_type" : "sample", "_id" : "4" } }
{"product":{"name":"tv","category":"electronics"},"features":[{"color":"red","details":"blood red in color"},{"finish":"matte"}]}

以下聚合按预期工作:(颜色桶包含完成桶):

GET agg/_search
{
  "size": 0,
  "aggs": {
    "root": {
      "nested": {
        "path": "features"
      },
      "aggs": {
        "colors": {
          "terms": {
            "field": "features.color",
            "size": 10
          },
          "aggs": {
            "colorToFinish": {
              "reverse_nested": {},
              "aggs": {
                "root": {
                  "nested": {
                    "path": "features"
                  },
                  "aggs": {
                    "finishes": {
                      "terms": {
                        "field": "features.finish",
                        "size": 10
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

但是,以下内容似乎无法按预期工作:

GET agg/_search
{
  "size": 0,
  "aggs": {
    "root": {
      "nested": {
        "path": "features"
      },
      "aggs": {
        "colors": {
          "terms": {
            "field": "features.color",
            "size": 10
          },
          "aggs": {
            "colorToFinish": {
              "reverse_nested": {
                "path": "features"
              },
              "aggs": {
                "finishes": {
                  "terms": {
                    "field": "features.finish",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

在非工作 DSL 中,我试图摆脱对“功能”的嵌套,并再次深入以完成任务。这似乎没有为“完成”收集桶。

但是,我们从根文档级别和从第一个原则获取字段的方法似乎有效。因此,似乎我没有正确使用 reverse_nested 中的“路径”参数,并且可能没有登陆正确的嵌套。有人知道为什么第二个查询不起作用吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-nested


    【解决方案1】:

    nested 查询/聚合可让您查询/聚合嵌套对象。您必须指定查询/聚合进入的path。另一方面,reverse_nested 让您跳出当前的嵌套查询/聚合。当使用reverse_nested 时,它已经知道应该跳出哪个嵌套对象,因此reverse_nested 内部不需要path

    因此,在您的第一个查询中,当出现reverse_nested 时,以下聚合将位于顶级对象,例如“产品”和“功能”。现在您要在嵌套的 features.finish 字段上进行聚合,因此您必须通过使用 path 提供 nested 术语再次进入嵌套对象。然后你在嵌套的 features.finish 字段上做普通的 agg。

    第二个查询不起作用有两个原因: 1. reverse_nested 不支持 path 并且没有必要。 2.在reverse_nested term之后,在嵌套字段上查询/聚合时仍然需要nested

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 2020-07-21
      • 2018-08-07
      • 1970-01-01
      • 2020-12-09
      • 2020-08-08
      相关资源
      最近更新 更多