Search code examples
azure-cognitive-search

Azure Cognitive Search prefix searching as single token


I'm trying to create an Azure Search index with a searchable Name field that should not be tokenized and be treated as a single string.

So if I have two values:

  • "Total Insurance"
  • "Invoice Total"

With a search term like this: search=Total*, then only "Total Insurance" should be returned because it starts with "Total".

My assumption was that the 'keyword' analyzer is to be used for this type of search

https://learn.microsoft.com/en-us/azure/search/index-add-custom-analyzers#built-in-analyzers

But it doesn't seem to work like that, it doesn't return any results with search=Total*.

Is there a different setup for this type of search?


Solution

  • Something like this is required:

    {   
        "name":"myIndex",
        "fields": [
            { 
                "name":"Name", 
                "type":"Edm.String", 
                "searchable":true, 
                "filterable": true, 
                "retrievable": true, 
                "sortable": true, 
                "searchAnalyzer":"keyword", 
                "indexAnalyzer":"prefixAnalyzer" 
            }   
        ],
        "analyzers": [
            {
                "name":"prefixAnalyzer",
                "@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer",
                "tokenizer":"keyword_v2",
                "tokenFilters":[ "lowercase", "my_edgeNGram" ]
            }
        ],
        "tokenFilters": [
            {
                "name":"my_edgeNGram",
                "@odata.type":"#Microsoft.Azure.Search.EdgeNGramTokenFilterV2",
                "minGram":3,
                "maxGram":7
            }
        ]
    }