Search code examples
elasticsearchsearchprefix

Type of field for prefix search in Elastic Search


I'm confused on what index type I should apply for my field for prefix search, many show search_as_you_type but I think auto complete is not what I'm going for.

I have a UUID field:

id: 34y72ca1-3739-41ff-bbec-f6d17479384c

The following terms should return the doc above:

3

34

34y72ca1

34y72ca1-3739

34y72ca1-3739-41ff-bbec-f6d17479384c

Using 3739 should not return it as it doesn't start with 3739. Initially this is what I was going for but then the wildcard field is not supported by Amazon AWS, so I compromise for prefix search instead of partial search.

I tried search_as_you_type field but it doesn't return the result when I use the whole ID. Actually, my use case is when user click enter, the results will be shown, instead of real-live when they type, so if speed is compromised its OK, just that I hope for something that will be good for many rows of data.

Thanks


Solution

  • If you have not explicitly defined any index mapping, then you need to use id.keyword field instead of the id field for the prefix query to show the appropriate results. This uses the keyword analyzer instead of the standard analyzer

    {
      "query": {
        "prefix": {
          "id.keyword": {
            "value": "34y72ca1"
          }
        }
      }
    }
    

    Otherwise, you can modify your index mapping, by adding multi fields for id field