'How to connect Elasticsearch analyzer with search query (typeahead search, combination search)?

I have an issue with the mapping analyzer to the field.

My priority is based on search (i want to autocomplete search based on the given text) 1 must match from the left side (1 priority), 2 any word in the given text (2 priority), 3 any character like the combination in the given text (3 priority)

**1. I created field mapping with the custom analyzer.**
    `
    "properties" => [
                "name" => [
                    "type"              => "search_as_you_type",
                    "analyzer"          => "my_tokenizer",
                ],
        ];
    `
**2. created a custom analyzer**
    
    {
        "settings": { 
          "analysis": {
              "tokenizer": { 
                   "my_tokenizer": {
                        "type": "edge_ngram", 
                        "min_gram": 1, 
                         "max_gram": 10, 
                         "token_chars": ["letter", "digit", "symbol"]
                    }
                },
               "analyzer": { 
                "my_analyzer":{
                       "tokenizer": "my_tokenizer"
                }
               }
             }
          }
       }
    
**3. Search query**
  
    'query' => [
               'query_string' => [
                      'default_field' => "name",
                      'query' => "test",
                      'default_operator' => "AND",
                    ],
         ],

**The result I am getting:**

TEST
ABC TEST
NAKT TZY TEST
TEST POST
TEST VN

**What I want :**
TEST
TEST VN
TEST POST
ABC TEST
NAKT TZY TEST


Solution 1:[1]

I don't understand why you use an analyzer for a search_as_you_type field that you want to use as an autocomplete. The search_as_you_type already creates the subfields with ngram.

I mapped like this:

"mappings": {
     "properties": {
       "name": {
         "type": "search_as_you_type"
       }
     }
   }

And the query below ran as you want.

{
  "query": {
    "multi_match": {
      "query": "test",
      "type": "bool_prefix",
      "fields": [
        "name",
        "name._2gram",
        "name._3gram"
      ]
    }
  }
}

See more: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andrecoelho.rabbitbr