0
votes

I can't get this to highlight for the life of me. It seems like elastic search is broken with regards to highlighting on fields that are mentioned in a filtered clause. The below query just does not highlight. Is this an issue with Elastic? What can I do to make this work?

    {
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      },
      "filter": {
        "and": [
          {
            "query": {
              "query_string": {
                "fields": [
                  "standard_analyzed_name",
                  "standard_analyzed_message"
                ],
                "query": "Arnold AND Schwarz"
              }
            }
          }
        ]
      }
    }
  },
  "size": 20,
  "sort": [
    {
      "total_interactions": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "standard_analyzed_name": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      },
      "standard_analyzed_message": {
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ],
        "fragment_size": 500,
        "number_of_fragments": 1
      }
    }
  }
}

UPDATE

The above query does highlight in elastic search 2.0.0 - Yayyy

1
Can you share your mapping and some documents that might be helpful to reproduce your case?Val
Thanks, what about your mapping? curl -XGET localhost:9200/facebook_posts/_mapping/documentVal

1 Answers

1
votes

Well, simply put, you have your query and filter parts swapped. Your query_string should go inside the query part and the term should go inside the filter part, then all will be well and you'll see the highlighted fields.

{
   "query": {
      "filtered": {
         "query": {
            "query_string": {
               "fields": [
                  "standard_analyzed_name",
                  "standard_analyzed_message"
               ],
               "query": "arnold"
            }
         },
         "filter": {
            "term": {
               "language": "austrian"
            }
         }
      }
   },
   "size": 20,
   "sort": [
      {
         "total_interactions": {
            "order": "desc"
         }
      }
   ],
   "highlight": {
      "fields": {
         "standard_analyzed_name": {
            "pre_tags": [
               "<strong>"
            ],
            "post_tags": [
               "</strong>"
            ],
            "fragment_size": 500,
            "number_of_fragments": 3
         },
         "standard_analyzed_message": {
            "pre_tags": [
               "<strong>"
            ],
            "post_tags": [
               "</strong>"
            ],
            "fragment_size": 500,
            "number_of_fragments": 3
         }
      }
   }
}