Highlight does not work correctly?

query = 'Ivan Atamanchuk Ivanovich'

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

CODE:

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:

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'
                    }
                }
            }
        }
    }
}

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.


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

Response

{
        "_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>"
          ]
        }
      }

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