Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting on has_child query

In some of our types, we have a parent child setup and we want to search on parent fields and also on the child fields (and return parent) and we do query like below. When there is a has_child match is there any way to get highlighting information from the child match even though the parent is being returned. As an example, if we have mapping like the following:

PUT nested2
{
  "mappings":{
    "discussion":{
        "properties" : {
            "title":{
                "type":"string"
            }
        }
    },
    "discussionPost":{
        "_parent":{
            "type" : "discussion"
        },
        "properties" : {
            "post" : {
                "type" : "string"
            }
        }
    }
  }
}

And we issue a query like below, highlight information is returned if there is a match on parent field but not if the parent is being returned due to a has_child match:

POST nested2/discussion/_search
{
  "query": {
    "bool": {
        "should": [
            {
                "prefix": {
                    "_all" : "cat"
                }    
            },
            {   
                "has_child" : {
                    "type" : "discussionPost",
                    "score_mode" : "sum",
                    "query" : {
                        "prefix": {
                            "_all" : "cat"
                        }  
                    }
                }
            }
        ],
        "minimum_should_match": 1
    }
  },
  "highlight":{
    "fields":{
      "*":{}
    }
  }
}

Is it possible to get highlight information on what matched in the child when has_child query is being issued on the parent?

Regards LT

like image 769
user1452215 Avatar asked Nov 23 '22 04:11

user1452215


1 Answers

It is possible to do this using inner_hits inside the has_child query clause:

{
  "query": {
    "bool": {
        "should": [
            {   
                "has_child" : {
                    "inner_hits": {
                        "_source": false,
                        "highlight":{
                            "order": "score",
                            "fields": {"*":{}}
                        }
                    },
                    "type" : "discussionPost",
                    "score_mode" : "sum",
                    "query" : {
                        "prefix": {
                            "_all" : "cat"
                        }  
                    }
                }
            }
        ],
        "minimum_should_match": 1
    }
  }
}
like image 120
Philip Bergström Avatar answered Nov 24 '22 17:11

Philip Bergström