# How to return only matched elements from array

**URL:** <https://discuss.elastic.co/t/how-to-return-only-matched-elements-from-array/122885>\
**Category:** Elasticsearch\
**Created:** [March 7, 2018, 1:00pm UTC](https://discuss.elastic.co/t/how-to-return-only-matched-elements-from-array/122885 "2018-03-07T13:00:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PiotrUfel](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@PiotrUfel](https://discuss.elastic.co/u/PiotrUfel)\
**Post date:** [March 7, 2018, 1:00pm UTC](https://discuss.elastic.co/t/how-to-return-only-matched-elements-from-array/122885/1 "2018-03-07T13:00:30Z")

</div>

Hi, everyone  
I'm trying to retrieve only matching elements from array like this:

{  
"speed": 2.24,  
"incident": "RIGHT\_TURN",  
"timestamp": "2018-03-06 15:38:19.454",  
"geoposition": {  
"coordinates": [  
19.145480869745988,  
50.27426389026141  
],  
"type": "point"  
}  
},  
{  
"speed": 2.97,  
"incident": "LEFT\_TURN",  
"timestamp": "2018-03-06 15:38:17.456",  
"geoposition": {  
"coordinates": [  
19.145461604945485,  
50.2742377011437  
],  
"type": "point"  
}  
},  
{  
"speed": 3.73,  
"incident": "LEFT\_TURN",  
"timestamp": "2018-03-06 15:38:16.457",  
"geoposition": {  
"coordinates": [  
19.145437295613323,  
50.27423723030056  
],  
"type": "point"  
}

elements are mapped to nested.  
I want to search only those which have for example incident == "LEFT\_TURN" is there any chance to do that and if it is how can I do that in NEST

I'm trying to do this like that:

{  
"\_source": "incidents.incident",  
"query": {  
"bool": {  
"must": {  
"match\_all": {}  
},  
"filter": {  
"nested": {  
"path": "incidents",  
"query": {  
"bool": {  
"must": {  
"match": { "incidents.incident" : "LEFT\_TURN" }  
}  
}  
}  
}  
}  
}  
}  
}

but the result is:

{  
"took": 1,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "sltracks1",  
"\_type": "elastictrack",  
"\_id": "2",  
"\_score": 1,  
"\_source": {  
"incidents": [  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "RIGHT\_TURN"  
},  
{  
"incident": "RIGHT\_TURN"  
},  
{  
"incident": "RIGHT\_TURN"  
},  
{  
"incident": "RIGHT\_TURN"  
},  
{  
"incident": "RIGHT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
},  
{  
"incident": "LEFT\_TURN"  
}  
]  
}  
}  
]  
}  
}

best regards  
Piotr

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 7, 2018, 4:12pm UTC](https://discuss.elastic.co/t/how-to-return-only-matched-elements-from-array/122885/2 "2018-03-07T16:12:25Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.  
Please update your post.

That said, may be it's more a question on how you are indexing your data?  
Like instead of indexing an array of objects, just index individual objects as this is what you are searching for?

---

<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:** [April 4, 2018, 4:12pm UTC](https://discuss.elastic.co/t/how-to-return-only-matched-elements-from-array/122885/3 "2018-04-04T16:12:34Z")

</div>

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