3

I use elastic search 7.10 and like to find documents by wildcard search on analyzed fields and highlight those in text. But this doesn't work.

The document could contain the following example: "The color of the car is black." I expect a result in which car and black is marked.

I have the following mapping:

 "text": {
            "type": "text",
            "store": true,
            "term_vector": "with_positions_offsets",
            "analyzer": "my_analyzer",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 8000
                },
                "wc" :{
                    "type": "wildcard"
                }
            }
        },

I use the following query:

{
    "query": {
        "bool": {
            "should": [ 
                {
                   "match": {"text": "car"}
                },
                {
                   "wildcard": { "text.wc": { "value": "bl*" } }
                }
            ]
        }
    },
    "fields": ["text", "text.wc"],
    "highlight": {
        "pre_tags": [
            "<span class='marker'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "type": "fvh",
        "fields": {
            "*": {
                "pre_tags": [
                    "<em>"
                ],
                "post_tags": [
                    "</em>"
                ]
            }
        },
        "require_field_match": true
    }
}

The query resultsets only contains highlight for the text - field, but not for the text.wc field. I also tried an separate wildcard-field, which is not an subfield of text but this also does not work. I also notice, that _source- field set to enabled is needed, even if all the fields are set to store otherwise I get an Unable to retrieve the requested [fields] message.

Question: How can I get highlighted text for a wildcrad searchterm?

The Bndr
  • 13,204
  • 16
  • 68
  • 107

1 Answers1

1

I found a solution and would like to answer my question myself in case anyone is facing the same problem.

The answer is, that wildcard and highlight and text-analysis (like stemming) are not working with match and wildcard- query like this one above.

But: Instead of match and wildcard you can use query_string. This is part of the of the elastic Query DSL, but unfortunately it is not listed here: https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl.html

This in my eyes very important feature is located 2 essential clicks/levels deeper here: https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl-query-string-query.html

query_string allows you to do all the search-and-highlight stuff on analyzed fields as the people from solr do in there query.

An example looks like that:

"query": {
    "bool": {
        "should": [
            {
                "query_string": {
                    "fields": [
                        "text"
                    ],
                    "query": "car and bl*"
                }
            ]
        }
    }
    "highlight": {
        "pre_tags": [
            "<span class='marker'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "type": "fvh",
        "fields": {
            "*": {
                "pre_tags": [
                    "<em>"
                ],
                "post_tags": [
                    "</em>"
                ]
            }
        },
        "require_field_match": true
    }
}
The Bndr
  • 13,204
  • 16
  • 68
  • 107