Search for a parent and all it's children in one query in elastic search


(Rishu K) #1

Referring to the examples mentioned in the official documentation of building a parent-child relationship - https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html

The link has provided question-answer join relationship where question is a parent and answer is a child type.

If you need to search for all parents matching some text along with their children matching some text specific to children in one query, how would you do that?

Let's say they have key-value pairs in the json document as below and we search for parents matching a text in the question and children matching a value in answer-text.

Parent --> question --> "question-text" : "Geography: Where is Mt. Everest?"
Child --> answer --> "answer-text" : "Nepal?"
We do not want just parents or just children in the result but all parents with their associated children which matched the query.

I know inner hits is an option but could not figure out the usage - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html

I hope the question is clear, can add more details if needed.

I also tried the example mentioned here for parent-child inner hits:

Example:

    POST labelling_index/_search
    {
          "query": {
              "has_child": {
                   "type": "child",
                         "query": {
                              "match": {
                                  "number": 2
                              }
                        },
              "inner_hits": {}
           }
       }
    }

This gives me the matching children of all parents. How do I add a filter to the parent and query only for specific parents in the same query (along with the child filter)? Is it possible to filter parent records on a specific field in the same query?

Something like this -

POST test_index/_search
    {
      "query": {
        --match parents first on number:1 and then match the children below--
        "has_child": {
          "type": "child",
          "query": {
            "match": {
              "number": 2
            }
          },
          "inner_hits": {}    
        }
      }
    }

(Rishu K) #2

Would be great if an expert could comment on this.

This is working when I pass only one field in the second match. Match query does not support multiple fields? Any other way to pass multiple fields such that we want only the best match with AND operator

POST test_index/_search
{
	"query": {
		"bool": {
			"must": [{
				"has_child": {
					"type": "childname",
					"query": {
						"match": {
							"properties.name": "hello"
						}
					},
					 "inner_hits": {}    
				}
			},
			{
				"match": {
					"account.name": "random",
					"version": {"2.0"}
				}
			},{
				"match": {
					"account.name": "random",
					"version": {"2.0"}
				}
			]
		}
	}
}

This worked for me


(system) #3

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