Elasticsearch过滤器/计数嵌套字段 [英] Elasticsearch filter/count nested fields

查看:114
本文介绍了Elasticsearch过滤器/计数嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档带有如下所示的嵌套字段:

I have documents with a nested field that looks like this:

...
"results": [
  {
    "id": "1234",
    "name": "asdf"
  },
  {
    "id": "5678",
    "name": "jklö"
  }
],
"ip": "1.2.3.4"
...

嵌套字段的映射如下:

"results": {
  "type": "nested",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    }
  }
}

在我切换到Elasticsearch 2之前,我有一个带有aggs的查询,该查询计算了没有结果的文档。这是查询的汇总部分:

Before I switched to elasticsearch 2 I had a query with aggs that counted the documents that had no results. Here's the aggregation part of the query:

"aggs": {
  "no_result": {
    "filter": {
      "missing": {
        "field": "results"
      }
    },
    "aggs": {
      "count": {
        "value_count": {
          "field": "ip"
        }
      }
    }
  }
}

现在我切换到elasticserach 2,它只计算所有文档。我已经尝试过其他方法,例如计算所有文档和计算结果,以便可以减去结果,但是

Now that I switched to elasticserach 2 it just counts all documents. I already tried different things like counting all documents and counting results so that I can subtract the results but

"aggs": {
  "results_count": {
    "value_count": {
      "field": "results"
    }
  }
}

总是0

如何正确过滤/计数嵌套字段?

How can I correctly filter/count my nested fields?

推荐答案

如果您要计算具有结果的文档数,则可以执行此操作。

if you want to count the number of documents which have results you can do this.

{
  "size": 0,
  "aggs": {
    "count": {
      "nested": {
        "path": "results"
      },
      "aggs": {
        "top_reverse_nested": {
          "reverse_nested": {}
        }
      }
    }
  }
}

数字计数将在top_reserve_nested doc_count

the number count will be in top_reserve_nested doc_count

这篇关于Elasticsearch过滤器/计数嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆