Share via

How to implement contains-like search logic and order results alphabetically in Azure Search

Thitiwut Harnphatcharapharnukorn 40 Reputation points
2026-01-23T06:16:40.0166667+00:00

We are using Azure Cognitive Search via the REST API and need clarification on how to

implement infix (contains-like) search logic and proper alphabetical ordering of results.

Currently, I am able to perform prefix-based searches, but I would like to support

infix matching as well. For example, searching for "pli" should return results such as:

  • "application"
  • "supplier"
  • "multiplier"

(not only values that start with the search term).

Additionally, We are ordering results using the $orderby parameter, but the sorting

appears to be based on ASCII order rather than true alphabetical order. For example, uppercase values are grouped separately from lowercase values, which is not the desired behavior.

Our goals are:

  • Implement infix / contains-like search using the Azure Cognitive Search REST API
  • Sort results alphabetically in a case-insensitive way

We would like clarification on:

  1. The recommended approach for infix or contains-style matching (e.g., analyzers, wildcard search, n-grams, or other techniques)
  2. How to configure fields or analyzers to achieve proper alphabetical ordering
  3. Any limitations or best practices when using infix search together with sorting

If possible, REST API examples or references to relevant Azure Cognitive Search documentation would be greatly appreciated.

Azure AI Search
Azure AI Search

An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.


Answer accepted by question author
  1. Golla Venkata Pavani 3,920 Reputation points Microsoft External Staff Moderator
    2026-01-23T07:30:13.79+00:00

    Hi @Thitiwut Harnphatcharapharnukorn ,

    Thank you for reaching us regarding the ways to implement contains-like search logic and achieve proper alphabetical ordering of your results in Azure Cognitive Search.

    Your approach with prefix searches is effective, and implementing true infix or contains matching (such as searching for "pli" to find "application," "supplier," or "multiplier") along with case-insensitive alphabetical sorting is a common requirement. Below is the recommended method for achieving both functionalities using Azure AI Search (formerly known as Azure Cognitive Search).

    For infix/contains-like search :
    The recommended approach for performant substring matching anywhere in the term is a custom analyzer that uses the NGramTokenFilterV2. This pre-generates overlapping substrings (n-grams) at index time, so a simple search=pli query matches naturally — no wildcards or regex needed at query time.

    This beats wildcard (pli) or regex (/.pli./) queries in speed, especially on larger indexes, though it does increase index size a bit (tune minGram/maxGram to balance recall and storge 3 to 8 or 10 works well for most cases).

    Example index field and analyzer config (add this when creating/updating your index):

    {
      "fields": [
        {
          "name": "name",
          "type": "Edm.String",
          "searchable": true,
          "analyzer": "ngram_analyzer",
          "sortable": true,
          "normalizer": "lowercase"  // for case-insensitive sort — see below
        }
      ],
      "analyzers": [
        {
          "name": "ngram_analyzer",
          "@odata.type": "#Microsoft.Azure.Search.CustomAnalyzer",
          "tokenizer": "keyword_v2",  // keeps the full string as one token
          "tokenFilters": [ "lowercase", "ngram_filter" ]
        }
      ],
      "tokenFilters": [
        {
          "name": "ngram_filter",
          "@odata.type": "#Microsoft.Azure.Search.NGramTokenFilterV2",
          "minGram": 3,
          "maxGram": 8
        }
      ]
    }
    

    If you prioritize simplicity over index size and are able to accommodate slightly slower query performance, particularly when using leading wildcards, consider utilizing a keyword analyzer field along with Lucene's full syntax options:

    • search=pli&queryType=full (wildcard)
    • search=/.pli./&queryType=full (regex)

    For case-insensitive alphabetical sorting:
    By default, string sorting is case-sensitive and follows ASCII order, placing uppercase letters before lowercase ones. To address this, please include "normalizer": "lowercase" in your field configuration, along with "sortable": true. This ensures values are preprocessed for sorting, filtering, and faceting, while search tokenization remains unaffected.

    The $orderby query then works naturally:

    GET ... ?search=...&$orderby=name asc
    

    Implementation Best Practices and Considerations:

    • When modifying analyzers or normalizers, it is necessary to drop and recreate the index, or add new fields. Please plan accordingly for reindexing.
    • Use the Analyze API to test tokenization and verify expected behavior.
    • Implementing n-gram can increase index size; begin with conservative gram ranges and monitor storage and performance impacts.
    • You can apply both features to the same field, use an analyzer for search and a normalizer for sorting as they function independently.

    Reference:
    https://learn.microsoft.com/en-us/azure/search/search-query-partial-matching
    https://learn.microsoft.com/en-us/azure/search/index-add-custom-analyzers
    https://learn.microsoft.com/en-us/azure/search/search-normalizers

    Kindly let us know if the above helps or you need further assistance on this issue.

    Please "accept" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-01-23T06:17:57.24+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To implement infix (contains-like) search logic and achieve proper alphabetical ordering in Azure Cognitive Search, you can follow these guidelines:

    1. Infix / Contains-like Matching:
      • Azure Cognitive Search does not natively support infix matching directly. However, you can achieve this using wildcard searches or regular expressions. For example, to search for terms containing "pli", you can use a wildcard query like search=*pli*. This approach allows you to match any term that contains the specified substring.
      • Alternatively, consider using n-grams for tokenization. N-grams can create tokens for sequences of characters in a term, which can enhance the search capabilities for infix matching. This requires configuring your index with an appropriate analyzer that supports n-gram tokenization.
    2. Alphabetical Ordering:
      • To sort results alphabetically in a case-insensitive manner, you should ensure that the fields used for sorting are indexed as sortable and utilize a text normalizer. The text normalizer can preprocess the text to ensure that uppercase and lowercase letters are treated equally during sorting. For example, you can assign a lowercase tokenizer to the field, but remember that sorting is done on a non-analyzed copy, so you may need to handle this at the application level or use a custom scoring profile to adjust the sorting behavior.
      • Use the $orderby parameter in your search query to specify the field you want to sort by. Ensure that the field is marked as sortable in your index definition.
    3. Limitations and Best Practices:
      • Be aware that wildcard searches can be slower, especially on large datasets. If performance is a concern, consider optimizing your index with n-gram tokenization, which may improve query execution times.
      • When using infix searches, ensure that your queries are well-formed and consider the impact of using wildcards on the overall performance of your search queries.
      • Regular expressions can also be used for more complex matching patterns, but they may introduce additional complexity and performance considerations.

    REST API Example:

    To perform a wildcard search and order results, your REST API call might look like this:

    GET https://<your-service-name>.search.windows.net/indexes/<your-index-name>/docs?search=*pli*&$orderby=yourField asc&api-version=2021-04-30-Preview
    

    Replace <your-service-name>, <your-index-name>, and yourField with your actual service name, index name, and the field you want to sort by.

    For more detailed information, you can refer to the Azure documentation on wildcard searches and sorting results.


    References:

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.