Filip
Filip

Reputation: 123

Azure Search: Group By or Distinct in OData context / Query?

Using Azure Search service I need to be able to group by or use distinct by a field in the query.

Use case: My business model has the concept of "resources" which have >=1 revisions. 1 revision is 1 document in an Azure index. I need to simulate something like "select the most recently changed resources from the index while also allowing pagination", therefore I need something like an ability to group the documents from the index into resources and search by them

Upvotes: 5

Views: 2824

Answers (1)

Bruce Johnston
Bruce Johnston

Reputation: 8634

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.

Upvotes: 3

Related Questions