akuiper
akuiper

Reputation: 215117

prefix autocomplete suggestion elasticsearch

I am trying to implement a prefix auto complete feature using ElasticSearch, here is my mapping for the suggest field:

PUT vdpinfo
{
    "mappings": {
        "details" : {
            "properties" : {
                "suggest" : {
                    "type" : "completion"
                },
                "title": {
                  "type": "keyword"
                }
            }
        }
    }
}

And I indexed some data with both single word and double words(bigram), such as:

{"suggest": "leather"}

And also:

{"suggest": "leather seats"}
{"suggest": "2 leather"}

And my search query is like this:

GET /vdpinfo/details/_search
{
        "suggest": {
            "feature-suggest": {
                "prefix": "leather",
                "completion": {
                    "field": "suggest"
                }
            }
        }
    }

But the result returns both {"suggest": "leather"} and {"suggest": "2 leather"}, and more importantly, {"suggest": "2 leather"} is ranked higher than leather.

My question is why the 2 leather gets returned, why doesn't it just do prefix autocomplete as in the query. prefix: leather?

Upvotes: 0

Views: 784

Answers (1)

Val
Val

Reputation: 217564

This is because the default analyzer that is used for analyzing your data is the simple analyzer, which simply breaks text into terms whenever it encounters a character which is not a letter, so 2 leather is actually indexed as leather, hence why that result is showing (and also why it is showing first).

The reason they are using the simple analyzer by default instead of the standard one is to not provide suggestion based on stop words (explanation here).

So if you use the standard analyzer instead, you won't get any suggestion for 2 leather

PUT vdpinfo
{
    "mappings": {
        "details" : {
            "properties" : {
                "suggest" : {
                    "type" : "completion",
                    "analyzer" : "standard"
                },
                "title": {
                  "type": "keyword"
                }
            }
        }
    }
}

Upvotes: 2

Related Questions