Reputation: 887
Good day:
Is it possible to filter out unrelated nested objects? The use case is that you have a single parent but, the parent has multiple children. Say for example for a particular parent...there's 100 children; I need a way to filter out the particular children so that out of a 100, I get a subset of that result.
UPDATED
Ran the following query
GET /dev/doc/_search?typed_keys=true
{"_source":{"includes":["reviews"]},"query":{"nested":{"query":{"bool":{"filter":[{"term":{"reviews.userId":{"value":"339c8add-4278-4acd-905e-64b9acabc71a"}}}]}},"path":"reviews"}}}
However, I'm getting back the following results:
"reviews": [
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-03T02:00:34.8735726-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "56a4bac2-85d0-4ccf-aba2-fd9ff74fb3a5",
"message": "blah blah blah",
"userId": "339c8add-4278-4acd-905e-64b9acabc71a",
"cleanliness": 4
},
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-04T12:01:22.228658-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "f2f1b84e-bc1d-4e9c-b6d5-bdc578cb1b5f",
"message": "blah blah blah",
"userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8",
"cleanliness": 4
}
]
As you can see the last object from this list has "userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8" which does mot match however, the filter is still returning the values. Any ideas why this is the case?
Upvotes: 0
Views: 1482
Reputation: 8513
It sounds like you are asking how to filter which inner nested objects get returned. Andreas's answer will help if you are asking how to chose which columns to return without filtering the data in those columns.
To return only the nested objects that match your query you can use inner_hits
. Basically just add it to your query clause and you should get the matching inner objects. There are some more configuration options for paging, etc that you can optionally add.
GET index/_search
{
"query": {
"nested": {
"path": "field_name",
"query": { ... },
"inner_hits": {}
}
}
}
Upvotes: 3
Reputation: 3105
What client are you using?
You probably want something like this (java): https://stackoverflow.com/a/30476650/5335131
Otherwise there is this: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
You can specify fields to include and exclude like so:
GET /_search
{
"_source": {
"includes": [ "obj1.*", "obj2.*" ],
"excludes": [ "*.description" ]
},
"query" : {
"term" : { "user" : "kimchy" }
}
}
Upvotes: 0