How could I get distinct values in Azure Search query results

Debashis Jena 71 Reputation points
2022-10-27T11:29:35.793+00:00

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.
7,664 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 17,886 Reputation points
    2022-10-27T20:48:40.673+00:00

    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

    Thanks,
    Grace

    ------------------------------------------------------------------------------------------------------------------------------

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

    0 comments No comments

Your answer

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