# How to find the percentage for any query success?

**URL:** <https://discuss.elastic.co/t/how-to-find-the-percentage-for-any-query-success/317194>\
**Category:** Elasticsearch\
**Created:** [October 21, 2022, 11:28am UTC](https://discuss.elastic.co/t/how-to-find-the-percentage-for-any-query-success/317194 "2022-10-21T11:28:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HemabhRavee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hemabhravee/32/111878_2.png) [@HemabhRavee](https://discuss.elastic.co/u/HemabhRavee)\
**Post date:** [October 21, 2022, 11:28am UTC](https://discuss.elastic.co/t/how-to-find-the-percentage-for-any-query-success/317194/1 "2022-10-21T11:28:55Z")

</div>

I want to write an aggregation which returns the percentage of hits for any query  
Let say I have a query Q which returns me 20 docs out of the 100 present in the target index.  
Then that aggregation should return me 20% when I add the query to it.

I should clarify that I am using a span\_near query which works fine, but I want to know how many hits it gets in percentage and it really shouldn't be this hard to figure out.

I found [this question](https://discuss.elastic.co/t/query-to-calculate-successrate/98886/2) which discusses a similar use-case which is getting the percentage of success. However, they have used `term` filter and I need `span_near` which is not available under filters but only under query.

I also came across [this question](https://discuss.elastic.co/t/how-to-find-percentage-using-doc-count-in-elastic-query/221394), the solution which describes my use case exactly. Unfortunately it does not have an answer.

here is my working span\_near query -

```auto
GET /transform_test6_4/_search?size=0
{
  "query": {
    "span_near": {
      "clauses": [
        {
          "span_term": {
            "eventFlow": "click_input"
          }
        },
        {
          "span_term": {
            "eventFlow": "idle"
          }
        }
      ],
      "slop": 120,
      "in_order": true
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 24, 2022, 12:38pm UTC](https://discuss.elastic.co/t/how-to-find-the-percentage-for-any-query-success/317194/2 "2022-10-24T12:38:21Z")

</div>

You can wrap your query in a [`filter` aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html). You can then use a [`bucket_script` aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html) to access the total number of matching documents via [`_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#_special_paths).

Wrap all of this inside a [`filters` aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html) that uses a `match_all` query to get the total number of documents, again via `_count`.

You can now calculate the percentage of matching documents by dividing the two counts and multiplying by 100. Something like this should work:

```auto
GET transform_test6_4/_search
{
  "size": 0,
  "aggs": {
    "all_docs_filter": {
      "filters": {
        "filters": {
          "all_docs": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "matching_docs": {
          "filter": {
            "span_near": {
              "clauses": [
                {
                  "span_term": {
                    "eventFlow": "click_input"
                  }
                },
                {
                  "span_term": {
                    "eventFlow": "idle"
                  }
                }
              ],
              "slop": 120,
              "in_order": true
            }
          }
        },
        "match_percentage": {
          "bucket_script": {
            "buckets_path": {
              "matching_doc_count": "matching_docs._count",
              "all_doc_count": "_count"
            },
            "script": "params.matching_doc_count / params.all_doc_count * 100"
          }
        }
      }
    }
  }
}

```

I notice you've set `size` to `0`, so I'm assuming you don't care about the actual hits. If you do, you can add a `top_hits` aggregation to the `matching_docs` `filter` agg.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 21, 2022, 12:38pm UTC](https://discuss.elastic.co/t/how-to-find-the-percentage-for-any-query-success/317194/3 "2022-11-21T12:38:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
