How could I get distinct values in Azure Search query results

Debashis Jena 31 Reputation points

How can I get distinct values for a field using Azure search query. Is Distinct keyword available for search query or any other way to get distinct values in Azure search?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
4,913 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 10,166 Reputation points

    Hi @Debashis Jena ,

    Thanks for the question. I found a similar question on StackOverflow which has a response from a member of the engineering team. Azure Search does not support distinct values. The below response still holds true today.

    "Azure Search does not support operators like distinct or group-by directly in the query language. However, there are potentially other ways to achieve what you want.

    One way would be to make each document in your index a resource instead of a revision of a resource. Then you could have a complex collection field to represent the revisions of each resource. There are a few potential caveats to this approach:
    It doesn't scale well if there are many (i.e. -- thousands) of revisions per resource. In fact, there is a limit of 3000 complex objects across all collections in a document.

    To add a new revision, you'd have to read-modify-write the entire collection of revisions, since Azure Search doesn't support intra-collection merges.

    If the primary unit of querying is really revisions and not resources, then modeling revisions as documents is more natural. However, you can always have more than one index depending on the query patterns you need.

    Another approach would be to add a Boolean field like IsLatestVersion, but then you'd need to set the flag to false on the previous revision whenever you add a new revision to your index. The approach above using complex types would probably be more straightforward."

    Hope that helps. Please let us know if you have further questions



    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments