# Annotated text highlighter identifies incorrect phrase hit when wrapped in function\_score query

**URL:** <https://discuss.elastic.co/t/annotated-text-highlighter-identifies-incorrect-phrase-hit-when-wrapped-in-function-score-query/277073>\
**Category:** Elasticsearch\
**Created:** [June 25, 2021, 2:15pm UTC](https://discuss.elastic.co/t/annotated-text-highlighter-identifies-incorrect-phrase-hit-when-wrapped-in-function-score-query/277073 "2021-06-25T14:15:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robmartin11](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robmartin11/32/99177_2.png) [@robmartin11](https://discuss.elastic.co/u/robmartin11)\
**Post date:** [June 25, 2021, 2:15pm UTC](https://discuss.elastic.co/t/annotated-text-highlighter-identifies-incorrect-phrase-hit-when-wrapped-in-function-score-query/277073/1 "2021-06-25T14:15:52Z")

</div>

Wondering if the following could be a bug or just user error?

Given I create a new index with annotated text fields:

```auto
PUT test_index
{
    "mappings": {
        "properties": {
            "a": {
                "type": "annotated_text"
            },
            "b": {
                "type": "annotated_text"
            }
        }
    }
}

```

And I index a document

```auto
POST test_index/_doc/1
{
    "a": "some test data",
    "b": "some more test stuff"
}

```

When I run the following query (containing function score)

```auto
GET test_index/_search
{
  "query": {
    "function_score": {
      "query": {
        "query_string": {
          "query": "\"test data\""
        }
      },
      "functions": [
        {
          "filter": {
            "query_string": {
                "query": "stuff"
            }
          },
          "weight": 5.0
        }
      ],
      "score_mode": "multiply"
    }
  },
  "highlight": {
    "type": "annotated",
    "fields": {
      "a": {},
      "b": {}
    }
  }
}

```

Then I expect to get 1 matching highlight fragments, but I get 2:

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.8768208,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 2.8768208,
                "_source": {
                    "a": "some test data",
                    "b": "some more test stuff"
                },
                "highlight": {
                    "a": [
                        "some [test](_hit_term=test) [data](_hit_term=data)"
                    ],
                    "b": [
                        "some more [test](_hit_term=test) stuff"
                    ]
                }
            }
        ]
    }
}

```

When I run the same query not wrapped in function\_score

```auto
GET test_index/_search
{
    "query": {
        "query_string": {
            "query": "\"test data\""
        }
    },
    "highlight": {
        "type": "annotated",
        "fields": {
            "a": {},
            "b": {}
        }
    }
}

```

Then I get the correct result

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "a": "some test data",
                    "b": "some more test stuff"
                },
                "highlight": {
                    "a": [
                        "some [test](_hit_term=test) [data](_hit_term=data)"
                    ]
                }
            }
        ]
    }
}

```

When I use the plain highlighter with function\_score

```auto
GET test_index/_search

{
  "query": {
    "function_score": {
      "query": {
        "query_string": {
          "query": "\"test data\""
        }
      },
      "functions": [
        {
          "filter": {
            "query_string": {
                "query": "stuff"
            }
          },
          "weight": 5.0
        }
      ],
      "score_mode": "multiply"
    }
  },
  "highlight": {
    "type": "plain",
    "fields": {
      "a": {},
      "b": {}
    }
  }
}

```

Then I get the correct result

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.8768208,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 2.8768208,
                "_source": {
                    "a": "some test data",
                    "b": "some more test stuff"
                },
                "highlight": {
                    "a": [
                        "some <em>test</em> <em>data</em>"
                    ]
                }
            }
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 23, 2021, 2:16pm UTC](https://discuss.elastic.co/t/annotated-text-highlighter-identifies-incorrect-phrase-hit-when-wrapped-in-function-score-query/277073/2 "2021-07-23T14:16:12Z")

</div>

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