# Filtering Aggregations

**URL:** <https://discuss.elastic.co/t/filtering-aggregations/64444>\
**Category:** Elasticsearch\
**Created:** [October 31, 2016, 12:16pm UTC](https://discuss.elastic.co/t/filtering-aggregations/64444 "2016-10-31T12:16:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![graham.roberts](https://avatars.discourse-cdn.com/v4/letter/g/ebca7d/32.png) [@graham.roberts](https://discuss.elastic.co/u/graham.roberts)\
**Post date:** [October 31, 2016, 12:16pm UTC](https://discuss.elastic.co/t/filtering-aggregations/64444/1 "2016-10-31T12:16:26Z")

</div>

Hi,

First post here, so hello, nice to meet you all.

I am playing with aggregations and getting some great results but I have one particular use case where I don't have an optimal result yet and I was hoping for a steer.

Each document has a locality which is an array structured like this to support hierarchical searching:  
[  
0/United Kingdom,  
1/United Kingdom/Berkshire  
2/United Kingdom/Berkshire/Reading  
]

The numeric prefix is a depth indicator that is used when filtering.

I am creating an auto suggest of available localities, when some types in "rea" I only want to return this result in the aggregation bucket:  
2/United Kingdom/Berkshire/Reading

However, in order make the autocomplete suggestions work I am using a pattern tokenizer so I can search only the lowest fragment in the hierarcy:  
'tokenizer' =\> array(  
'descendent' =\> array(  
'type' =\> 'pattern',  
'pattern' =\> '([^/]+$)', // Get everything after the last slash  
'group' =\> 1  
),  
),

I am using a edge n-gram filter so I can do a "starts with" like search:  
'edge\_ngram\_filter' =\> array(  
'type' =\> 'edge\_ngram',  
'min\_gram' =\> 2,  
'max\_gram' =\> 20,  
),

And then I put that together in my analyzer:  
'edge\_ngram\_analyzer\_descendent' =\> array(  
'type' =\> 'custom',  
'tokenizer' =\> 'descendent',  
'filter' =\> array(  
'lowercase',  
'edge\_ngram\_filter'  
),  
),

But the difficulty is that I need to return a value from a different field in my aggregation, the untouched version. If I setup my aggregation like this:

{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"match": {  
"field\_item\_locality.edge\_ngram\_analyzer\_descendent": "rea"  
}  
}  
}  
},  
"aggs": {  
"field\_item\_locality": {  
"terms": {  
"field": "field\_item\_locality.untouched",  
"size": 0  
}  
}  
}  
}

Then I will receive all items from the locality array in the document, not just the one that matches.

And I can't use an include on the aggregation like:  
"include": "rea.\*"  
because if I do then I have to do this on one of my analyzed fields to account for the lowercasing and extracting the lowest descendent.

What direction should I be looking in to solve this?

Many thanks in advance

Graham

---

<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:** [July 5, 2017, 10:08pm UTC](https://discuss.elastic.co/t/filtering-aggregations/64444/2 "2017-07-05T22:08:18Z")

</div>

Closed on July 5, 2017, 10:08pm UTC


