当前位置: 动力学知识库 > 问答 > 编程问答 >

Document count aggregation via query in Elasticsearch (like facet.query in solr)

问题描述:

I have a main query and i need the number of matches for a couple of sub-queries.

In solr words I need a facet.query. What I am missing is a simple doc_count aggregation like the value_count aggregation.

Any suggestions?

I found two possible solutions which I do not like:

  1. Use filter aggregation with value_count metric on _id:

example:

GET _search

{

"query": {

"match_main": {}

},

"aggs": {

"facetvalue1": {

"filter": {

"bool": {

"should": [

{"match": { "name": "fred" }},

{"term": { "lastname": "krueger" }}

]

}

},

"aggs": {

"count": {

"value_count": {

"field": "_id"

}

}

}

},

"facetvalue2": {

"filter": {

"term": { "name": "freddy" }

},

"aggs": {

"count": {

"value_count": {

"field": "_id"

}

}

}

}

}

}

  1. Use Multi Search API

example:

GET _msearch

{"index":"myindex"}

{"query":{"match_main": {}}}

{"index":"myindex"}

{"size": 0, "query":{"match_main": {}}, "filter": {"bool": {"should":[{"match": { "name": "fred" }},{"term": { "lastname": "krueger" }}]}}}

{"index":"myindex"}

{"size": 0, "query":{"match_main": {}},"filter": {"term": { "name": "freddy" }}}

I see that solution 2 is faster but imagine match_main as complex query!

So I would prefer solution 1 if there would be an doc_count:{} instead of value_count:{"field":"_id"}.

But back to my basic question: what is the counterpart of the solr facet.query in elasticsearch?

网友答案:

You can use a filters aggregation for this. Note the additional s, that is different from the filter aggregation you already mentioned.

{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "values": {
      "filters": {
        "filters": {
          "value1": {
            "bool": {
              "should": [
                {
                  "match": {
                    "name": "fred"
                  }
                },
                {
                  "term": {
                    "lastname": "krueger"
                  }
                }
              ]
            }
          },
          "value2": {
            "term": {
              "name": "freddy"
            }
          }
        }
      }
    }
  }
}

This will return something like

"aggregations": {
  "values": {
    "buckets": {
      "value1": {
        "doc_count": 4
      },
      "value2": {
        "doc_count": 1
      }
    }
  }
}

Edit: As a general note, you don't have to use a metric aggregation on your bucket aggregations. If you don't provide any subaggregations, you will just get the document count. In this case, filters will provide the buckets, but multiple filter aggregations should work as well.

分享给朋友:
您可能感兴趣的文章:
随机阅读: