How to return only matched elements from array

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

Please format your code, logs or configuration files using </> icon as explained in this guide 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?

1 Like

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