Home > other >  Search for parents only (with joins)
Search for parents only (with joins)

Time:11-04

I'm relatively new to elasticsearch and I'm trying to follow this documentation page:

https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html

It is mentioned here that a plain match_all query will return everything, both the "parents" and the "children" - https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html#_searching_with_parent_join .

What isn't mentioned there (maybe because it's basic knowledge) is how do you get the parents only? I simply want to get all the parents, without the children.

How would a query like that look like?

CodePudding user response:

I believe that you have already known the relationship of your join when you do the mappings. Let's use the example from the documentation, you know that the parent will be question, and the answer will be children. You simply need to query your join field to be question only:

{
  "query": {
    "term": {
            "my_join_field": "question"
        }
  },
  "sort": ["my_id"]
}

If you use this query, only the documents with my_join_field of question will be returned (and they are your parent documents). If you want only the children, you could do the same (my_join_field will be answer).

  • Related