add highlight does not work with has_child query in Elasticsearch 2.3.3

前端 未结 1 1571
灰色年华
灰色年华 2021-01-14 18:10

When I use hasChildQuery,everything works OK.But when I add addHighlightedField() method,it does not work.The following is my code:

TermsLookupQ         


        
相关标签:
1条回答
  • 2021-01-14 18:26

    This is related to the bug specified in the git issue here . The workaround as mentioned in the thread is to specify it in highlight_query

    Example :

    PUT test
    {
       "mappings": {
          "my_parent": {
             "_all": {
                "store": true
             }
          },
          "my_child": {
             "_parent": {
                "type": "my_parent"
             }
          }
       }
    }
    
    PUT test/my_parent/1 
    {
      "text": "This is a parent document"
    }
    
    PUT test/my_child/2?parent=1 
    {
      "text": "This is a child document"
    }
    
    POST test/my_parent/_search
    {
       "query": {
          "bool": {
             "must": [
                {
                   "has_child": {
                      "type": "my_child",
                      "query": {
                         "match": {
                            "text": "child document"
                         }
                      }
                   }
                },
                {
                   "match": {
                      "_all": "parent"
                   }
                }
             ]
          }
       },
       "highlight": {
          "fields": {
             "_all": {}
          },
          "highlight_query": {
             "match": {
                "_all": "parent"
             }
          }
       }
    }
    

    Results :

      {
                "_index": "test",
                "_type": "my_parent",
                "_id": "1",
                "_score": 1.016466,
                "_source": {
                   "text": "This is a parent document"
                },
                "highlight": {
                   "_all": [
                      "This is a <em>parent</em> document "
                   ]
                }
             }
    

    In Java Client you should be able to achieve it via this api

    0 讨论(0)
提交回复
热议问题