# Filter items of array field with query while aggregating values

**URL:** https://discuss.elastic.co/t/filter-items-of-array-field-with-query-while-aggregating-values/211287
**Category:** Elasticsearch
**Created:** [December 10, 2019, 11:29am UTC](https://discuss.elastic.co/t/filter-items-of-array-field-with-query-while-aggregating-values/211287 "2019-12-10T11:29:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![phoenix616](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@phoenix616](https://discuss.elastic.co/u/phoenix616)
#### Post date: [December 10, 2019, 11:29am UTC](https://discuss.elastic.co/t/filter-items-of-array-field-with-query-while-aggregating-values/211287/1 "2019-12-10T11:29:10Z")

</div>

Hi!

I ran into a problem for which I can not find an acceptable solution:

Input:  
I have index of documents

> ```
> {
> "profile": {
> "mappings": {
> "properties": {
> ...
> "skills": {
> "type": "text",
> "fields": {
> "keyword": {
> "type": "keyword",
> "ignore_above": 256
> }
> }
> },
> ...
> }
> }
> }
> 
> ```

Where, in fact, "skills" field is array of strings

> ```
> {
> ...
> "skills": ["skill1", "skill2"]
> ...
> }
> 
> ```

I need:

1. Aggregate all unique value of this field
2. Amount of possible values may be big, so I need paging
3. Filter aggregated values with match query.

Now I have smth like this:

> ```
> {
> "aggs": {
> "skills": {
> "composite": {
> "size": 10,
> "sources": [{
> "skills_sub": {
> "terms": {
> "field": "skills.keyword"
> }
> }
> }
> ]
> }
> }
> },
> "query": {
> "bool": {
> "should": [{
> "match": {
> "skills": {
> "query": "value"
> }
> }
> }
> ]
> }
> },
> "size": 0
> }
> 
> ```

It almost matches my requirements:

1. It aggregates unique values
2. I have "After" and "Size"
3. It returns values, that match query. BUT it returns ALL values from array if any matches

So, if doc has field "skills" with values: "value1", "value2". And I request for "value1", I'll got both of them.

So, what I need to do, to override this behavior?

---

<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: [January 7, 2020, 11:29am UTC](https://discuss.elastic.co/t/filter-items-of-array-field-with-query-while-aggregating-values/211287/2 "2020-01-07T11:29:11Z")

</div>

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