'ElasticSearch Search-as-you-type field type field with partial search

I recently updating my ngram implementation settings to use Search-as-you-type field type. https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html

This worked great but I noticed that partial searching does not work. If I search for number 00060434 I get the desired result but I would also like to be able to search for 60434, then it should return document 3.

Is there a way todo it with the Search-as-you-type field type or can i only do this with ngrams?

PUT searchasyoutype_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "englishAnalyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "trim",
            "ascii_folding"
          ]
        }
      },
      "filter": {
        "ascii_folding": {
          "type": "asciifolding",
          "preserve_original": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "number": {
        "type": "search_as_you_type",
        "analyzer": "englishAnalyzer"
      },

      "fullName": {
        "type": "search_as_you_type",
        "analyzer": "englishAnalyzer"
      }
    }
  }
}


PUT searchasyoutype_example/_doc/1
{
  "number" : "00069794",
  "fullName": "Employee 1"
}

PUT searchasyoutype_example/_doc/2
{
  "number" : "00059840",
  "fullName": "Employee 2"
}

PUT searchasyoutype_example/_doc/3
{
  "number" : "00060434",
  "fullName": "Employee 3"
}

GET searchasyoutype_example/_search
{
  "query": {
    "multi_match": {
      "query": "00060434",
      "type": "bool_prefix",
      "fields": [
        "number",
        "number._index_prefix",
        "fullName",
        "fullName._index_prefix"
      ]
    }
  }
}


Solution 1:[1]

I think you need to query on number,number._2gram & number._3gram like below:

GET searchasyoutype_example/_search
{
  "query": {
    "multi_match": {
      "query": "00060434",
      "type": "bool_prefix",
      "fields": [
        "number",
        "number._2gram",
        "number._3gram",
      ]
    }
  }
}

search_as_you_type creates the 3 sub fields. You can check more on this article how it works:

https://ashish.one/blogs/search-as-you-type/

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