Kevin Pluck
Kevin Pluck

Reputation: 681

Multi match query in elastic not matching initial character

Hello I am trying to setup a single search box that will do partial searches on certain fields and standard searches on others. I am nearly there but am failing to get past the following hurdle:

This is my index:

PUT /my_index
{
    "mappings": {
        "blogpost": {
            "properties": {
                "firstname": {
                    "fields": {
                        "autocomplete": {
                            "index_analyzer": "autocomplete",
                            "type": "string"
                        },
                        "firstname": {
                            "index_analyzer": "standard",
                            "type": "string"
                        }
                    },
                    "type": "string"
                }
            }
        }
    },
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "autocomplete": {
                        "tokenizer": "ngram_tokenizer",
                        "type": "custom"
                    },
                    "standard": {
                        "type": "standard"
                    }
                },
                "tokenizer": {
                    "ngram_tokenizer": {
                        "max_gram": "20",
                        "min_gram": "2",
                        "type": "nGram"
                    }
                }
            },
            "creation_date": "1431690991641",
            "number_of_replicas": "0",
            "number_of_shards": "3",
            "uuid": "W4Ug6IadS9mYuN5_Pqlhow",
            "version": {
                "created": "1040499"
            }
        }
    }
}

Index 1 document:

PUT /my_index/blogpost/1
{"firstname" : "Albert"}

Simple query:

/_search?q=Albert

returns Albert. All good.

Multi_match query:

{
  "query": {
    "multi_match": {
      "query": "Albert",
      "fields": [
        "firstname",
        "firstname.autocomplete"
      ]
    }
  }
}

Also returns Albert. All good.

If I replace Albert with bert it returns Albert. All good.

But "Al" or "al" or "Alber" or "alber" does not! Any search where beginning letter is included fails.

Yet

/my_index/_search?firstname.autocomplete:Al

Is all good.

Please help.

Upvotes: 1

Views: 1834

Answers (1)

keety
keety

Reputation: 17441

The search analyzer for field.autocomplete is the default which is usually standard.

So when you search for Al you are actually looking for "al" even for "Al" you end up searching for the lowercase version.

However while indexing using autocomplete analyzer you are not normalizing the data to lowercase so it has only the term "Al" in the index.

You can use the analyze api to check how the data has been analyzed

GET /my_index/_analyze?field=firstname.autocomplete&text=Albert"

Adding lowercase token filter to "autocomplete" analyzer should fix this issue :

   "autocomplete": {
                        "tokenizer": "ngram_tokenizer",
                        "type": "custom",
                        "filter" :[
                            "lowercase"
                        ]
                    },

Upvotes: 4

Related Questions