# Configure highlighted part

**URL:** <https://discuss.elastic.co/t/configure-highlighted-part/295164>\
**Category:** Elasticsearch\
**Created:** [January 23, 2022, 9:11pm UTC](https://discuss.elastic.co/t/configure-highlighted-part/295164 "2022-01-23T21:11:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavel\_Maltsev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pavel_maltsev/32/100748_2.png) [@Pavel\_Maltsev](https://discuss.elastic.co/u/Pavel_Maltsev)\
**Post date:** [January 23, 2022, 9:11pm UTC](https://discuss.elastic.co/t/configure-highlighted-part/295164/1 "2022-01-23T21:11:16Z")

</div>

**Main question**  
The user is looking for a name and enters the part of it, let's say `au`, and the document with the text `paul` is found.  
I would like to have the doc highlighted like `p<em>au</em>l`.  
How can I achieve it if I have a complex search query (combination of match, prefix, wildcard to rule relevance)?

**Sub question**  
When do highlight settings from [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html#highlighting-settings) for `type`, `boundary_scanner` and `boundary_chars` come into play? As per my tests described below, these settings don't change highlighted part.

**Try 1: Wildcard query with default analyzer**

```auto
PUT myindex
{
    "mappings": {
        "properties": {
            "name": {
                "type": "text",
                "term_vector": "with_positions_offsets"
            }
        }
    }
}
POST myindex/_doc/1
{
    "name": "paul"
}
GET myindex/_search
{
    "query": {
        "wildcard": {"name": "*au*"}
    },
    "highlight": {
        "fields": { 
            "name": {}
        },
        "type": "fvh",
        "boundary_scanner": "chars",
        "boundary_chars": "abcdefghijklmnopqrstuvwxyz.,!? \t\n"
    }
}

```

This kind of search returns highlight `<em>paul</em>` but I need to get `p<em>au</em>l`.

**Try 2: Match query with NGRAM analyzer**  
This one works as described in SO question: [autocomplete - Highlighting part of word in elasticsearch - Stack Overflow](https://stackoverflow.com/questions/21310610/highlighting-part-of-word-in-elasticsearch)

```auto
PUT myindexngram
{
    "settings": {
        "analysis": {
            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "nGram",
                    "min_gram": "2",
                    "max_gram": "3",
                    "token_chars": [
                        "letter",
                        "digit"
                    ]
                }
            },
            "analyzer": {
                "index_ngram_analyzer": {
                    "type": "custom",
                    "tokenizer": "ngram_tokenizer",
                    "filter": [
                        "lowercase"
                    ]
                },
                "search_term_analyzer": {
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": "lowercase"
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "name": {
                "type": "text",
                "analyzer": "index_ngram_analyzer",
                "term_vector": "with_positions_offsets"
            }
        }
    }
}
POST myindexngram/_doc/1
{
    "name": "paul"
}
GET myindexngram/_search
{
    "query": {
        "match": {"name": "au"}
    },
    "highlight": {
        "fields": { 
            "name": {}
        }
    }
}

```

This highlights `p<em>au</em>l` as desired but:

1. Highlighting depends on the query type, so combining `match` and `wildcard` into `bool.should` will again result in `<em>paul</em>`.
2. Highlighting is not affected at all on `type`, `boundary_scanner` and `boundary_chars` settings.

Elastic version 7.13.4

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [January 27, 2022, 10:14pm UTC](https://discuss.elastic.co/t/configure-highlighted-part/295164/2 "2022-01-27T22:14:01Z")

</div>

A highlighter works on terms, so only full terms can be highlighted - whatever are the terms in your index. In your second example, `au` could be highlighted, because it it a term in the index, which is not the case for your first example.  
There is also an option to define your own `highlight_query` that could be different from the main `query`, but this could lead to unpredictable highlights.

---

<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:** [February 24, 2022, 10:14pm UTC](https://discuss.elastic.co/t/configure-highlighted-part/295164/3 "2022-02-24T22:14:30Z")

</div>

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