# Highlight does not work correctly?

**URL:** <https://discuss.elastic.co/t/highlight-does-not-work-correctly/336109>\
**Category:** Elasticsearch\
**Created:** [June 15, 2023, 2:27pm UTC](https://discuss.elastic.co/t/highlight-does-not-work-correctly/336109 "2023-06-15T14:27:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nurm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nurm/32/120844_2.png) [@Nurm](https://discuss.elastic.co/u/Nurm)\
**Post date:** [June 15, 2023, 2:27pm UTC](https://discuss.elastic.co/t/highlight-does-not-work-correctly/336109/1 "2023-06-15T14:27:27Z")

</div>

query = 'Ivan Atamanchuk Ivanovich'

highlighter is not working correctly when I put text above, it is just highlighting "_ **Ivan** _ _ **Atamanchuk** _ _ **Ivan** _ovich"  
ignoring last "ovich", I approximately know why it is not highlighting, but I am struggling with realization

CODE:

```auto
body['query']['bool']['should'] = [
                    {'match': {'fullname.ngrams': {'query': query, 'operator': 'AND'}}}
                ]

            if body['query']['bool'].get('should'):
                body['query']['bool']['minimum_should_match'] = 1
                body['highlight'] = {
                    "fields": [
                        {
                            "fullname.ngrams": {}
                        },

```

SETTINGS:

```auto
index_settings = {
    'settings': {
        'number_of_shards': 1,
        'number_of_replicas': 0,
        'analysis': {
            'tokenizer': {
                'custom_edge_ngram_tokenizer': {
                    'type': 'edge_ngram',
                    'min_gram': '1',
                    'max_gram': '100',
                    'token_chars': [
                        'letter',
                        'digit',
                        'custom'
                    ],
                    "custom_token_chars": "+-_"
                }
            },
            'analyzer': {
                'custom_edge_ngram_analyzer': {
                    'tokenizer': 'custom_edge_ngram_tokenizer',
                    'filter': [
                        'lowercase'
                    ]
                }
            }
        }
    },
    'mappings': {
        'properties': {
            'fullname': {
                'type': 'text',
                'term_vector': 'with_positions_offsets',
                'fields': {
                    'ngrams': {
                        'type': 'text',
                        'search_analyzer': 'standard',
                        'analyzer': 'custom_edge_ngram_analyzer',
                        'term_vector': 'with_positions_offsets'
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 15, 2023, 2:47pm UTC](https://discuss.elastic.co/t/highlight-does-not-work-correctly/336109/2 "2023-06-15T14:47:45Z")

</div>

Hi @Nurm

I ran your mapping and the query below. The highlight worked but I don't know if it's exactly what you expect. Does it make sense to just match inside the bool-should? Maybe you missed something.

```auto

GET idx_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "fullname.ngrams": {
              "query": "Ivan Atamanchuk Ivanovich",
              "operator": "and"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "fullname.ngrams": {}
    }
  }
}

```

Response

```auto
{
        "_index": "idx_test",
        "_id": "lMaAv4gBSQo3DdXvtNG4",
        "_score": 0.970927,
        "_source": {
          "fullname": "Ivan Atamanchuk Ivanovich"
        },
        "highlight": {
          "fullname.ngrams": [
            "<em>Ivan</em> <em>Atamanchuk</em> <em>Ivanovich</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 13, 2023, 2:48pm UTC](https://discuss.elastic.co/t/highlight-does-not-work-correctly/336109/3 "2023-07-13T14:48:31Z")

</div>

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