使用reverse_nested路径参数进行弹性搜索聚合

时间:2017-05-10 00:05:18

标签: elasticsearch elasticsearch-aggregation elasticsearch-nested

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

使用以下映射创建索引

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中的“path”参数,并且可能没有在正确的嵌套中着陆。有人知道为什么第二个查询不起作用吗?

1 个答案:

答案 0 :(得分:0)

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

因此,在您的第一个查询中,当显示reverse_nested时,以下聚合将位于顶级对象上,例如“product”和“features”。现在,您希望在嵌套的features.finish字段上进行聚合,因此您必须通过nested术语path再次进入嵌套对象。然后你在嵌套的features.finish字段上做正常的聚合。

第二个查询不起作用有两个原因:1。reverse_nested不支持path,这是不必要的。 2. reverse_nested一词后,在嵌套字段上查询/聚合时仍需要nested