# Aggregating on highlight

**URL:** https://discuss.elastic.co/t/aggregating-on-highlight/140175
**Category:** Elasticsearch
**Created:** [July 16, 2018, 3:13pm UTC](https://discuss.elastic.co/t/aggregating-on-highlight/140175 "2018-07-16T15:13:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sofiane\_Chenna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sofiane_chenna/32/33382_2.png) [@Sofiane\_Chenna](https://discuss.elastic.co/u/Sofiane_Chenna)
#### Post date: [July 16, 2018, 3:13pm UTC](https://discuss.elastic.co/t/aggregating-on-highlight/140175/1 "2018-07-16T15:13:42Z")

</div>

Hi ,  
I have a query that returns a highlight fields , i wanna aggregate the fields returned by the query ( see code )

```
      {
            "query" : {
                "query_string" : {
                    "query" : "*so*",
                    "fields": ["MemberFname", "TopicLabel"] 
                }
            },
            "highlight": {           
             	 "pre_tags" : ["", ""],
            "post_tags" : ["", ""],
                "fields" : {
                    "MemberFname" : {},          
                     "TopicLabel": {}
                }
            }

    }

```

i want to aggregate the fields "MemberFname" and "TopicLabel" to return unique values of these fields  
any idea how to do it ?  
thanks

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [July 17, 2018, 11:03am UTC](https://discuss.elastic.co/t/aggregating-on-highlight/140175/2 "2018-07-17T11:03:32Z")

</div>

Aggregations work on the raw values of documents (or values mutated by a script) and not the outputs of a highlighter.  
If these fields are single-value fields then a straight-forward `terms` aggregation on the field value would work otherwise you'd need to use a `terms` agg with script to filter the multiple values in matching docs to just those values that contain `so` as in your query.

---

<div class="post-metadata">

### Author: ![Sofiane\_Chenna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sofiane_chenna/32/33382_2.png) [@Sofiane\_Chenna](https://discuss.elastic.co/u/Sofiane_Chenna)
#### Post date: [July 18, 2018, 7:59am UTC](https://discuss.elastic.co/t/aggregating-on-highlight/140175/3 "2018-07-18T07:59:13Z")

</div>

Thanks for your answer , but my problem is that when a "MemberFname" is matched with the query , the query returns the both fields (MemberFname and TopicLabel ) in terms aggregation , and i want to return only the field that matched the query.

Thanks

---

<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: [August 15, 2018, 7:59am UTC](https://discuss.elastic.co/t/aggregating-on-highlight/140175/4 "2018-08-15T07:59:21Z")

</div>

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