简体   繁体   English

Elasticsearch 过滤器/计数嵌套字段

[英]Elasticsearch filter/count nested fields

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"
...

The mapping for the nested field looks like this:嵌套字段的映射如下所示:

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

Before I switched to elasticsearch 2 I had a query with aggs that counted the documents that had no results.在我切换到 elasticsearch 2 之前,我有一个 aggs 查询,它计算没有结果的文档。 Here's the aggregation part of the query:这是查询的聚合部分:

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

Now that I switched to elasticserach 2 it just counts all documents.现在我切换到 elasticserach 2 它只计算所有文档。 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"
    }
  }
}

Is always 0总是 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": {}
        }
      }
    }
  }
}

the number count will be in top_reserve_nested doc_count数量计数将在 top_reserve_nested doc_count 中

"field": "ip" “字段”:“IP”

should be "field": "id"应该是“字段”:“id”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM