0

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?

2 Answers 2

3

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": {}
        }
    }
}

inner_hits reference

Sign up to request clarification or add additional context in comments.

5 Comments

@RussCam & Ryan.. I've updated my original post. Running the nested query still doesn't show the desirable results.
@Dean your updated query doesn't include inner_hits: {} for the nested query
@RussCam OK..I'll update and share the results...thanks.
@RussCam thanks much..inner_hits definitely worked.
0

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" }
    }
}

2 Comments

Thanks for the reply Andreas. The problem is...can I query on nested objects? Also my client is NEST.
Yes, you can query on nested objects, have a look at elastic.co/guide/en/elasticsearch/reference/6.3/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.