# Terms aggregation - Sort on the relevancy of the terms

**URL:** <https://discuss.elastic.co/t/terms-aggregation-sort-on-the-relevancy-of-the-terms/85627>\
**Category:** Elasticsearch\
**Created:** [May 12, 2017, 9:59pm UTC](https://discuss.elastic.co/t/terms-aggregation-sort-on-the-relevancy-of-the-terms/85627 "2017-05-12T21:59:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_mp](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@s\_mp](https://discuss.elastic.co/u/s_mp)\
**Post date:** [May 12, 2017, 9:59pm UTC](https://discuss.elastic.co/t/terms-aggregation-sort-on-the-relevancy-of-the-terms/85627/1 "2017-05-12T21:59:32Z")

</div>

Hi,  
We have this usecase, where we are doing terms aggregation on a multi-valued (array) field. The documents are initially filtered using a regex match on this field, and the terms aggregation is done on the same field.  
The terms aggregation is returning the terms sorted by doc\_count as expected.  
We would like the sorting to be based on the relevancy of the term with respect to the initial regex filter. Is this possible ?

Refer to my example below for more clarity.

```
PUT someindex
{
    "settings": {
        "index": {
            "number_of_replicas": 0,
            "number_of_shards": 1,
            "search.slowlog.threshold.query.debug": "1ms"
        }
    },
    "mappings": {
        "my_type": {
            "dynamic_templates": [
                {
                    "strings": {
                        "mapping": {
                            "type": "keyword"
                        },
                        "match_mapping_type": "string"
                    }
                }
            ]
        }
    }
}
PUT someindex/my_type/1
{
  "title": "document 001",
  "tags": ["lucene", "search"]
}

PUT someindex/my_type/2
{
  "title": "document 002",
  "tags": ["lucene is a search library", "lucene", "elastic", "search"]
}

GET someindex/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "regexp": {
            "tags": {
              "value": ".*lucene.*|.*search.*"
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "requestedDimension": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

```

Response :

```
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "requestedDimension": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "lucene",
          "doc_count": 2
        },
        {
          "key": "search",
          "doc_count": 2
        },
        {
          "key": "elastic",
          "doc_count": 1
        },
        {
          "key": "lucene is a search library",
          "doc_count": 1
        }
      ]
    }
  }
}

```

Looking for a query to get the below response, as "lucene is a search library" is more relevant to my filter.

```
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "requestedDimension": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "lucene is a search library",
          "doc_count": 1
        },
        {
          "key": "lucene",
          "doc_count": 2
        },
        {
          "key": "search",
          "doc_count": 2
        },
        {
          "key": "elastic",
          "doc_count": 1
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![lwintergerst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lwintergerst/32/18164_2.png) [@lwintergerst](https://discuss.elastic.co/u/lwintergerst)\
**Post date:** [May 18, 2017, 8:42pm UTC](https://discuss.elastic.co/t/terms-aggregation-sort-on-the-relevancy-of-the-terms/85627/2 "2017-05-18T20:42:24Z")

</div>

Hello Srini,  
unfortunately that is not possible as far as I know. I will double check with my colleagues and get back to you.

Scoring is only done as part of they query phase and can't be applied to aggregations.

Thank you for providing the exact query and aggregation. This makes it much easier for us to help you.

---

<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:** [June 15, 2017, 8:43pm UTC](https://discuss.elastic.co/t/terms-aggregation-sort-on-the-relevancy-of-the-terms/85627/3 "2017-06-15T20:43:00Z")

</div>

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