Search code examples
azure-cognitive-search

Azure Search how to get one specific item in a complex type collection


I have an index for songs and each song have a collection of customers. If I filter songs that contains a specific customer works great, but I get in the result all the customers for the song. I would need to get the song with only the customer I'm filtering in the collection.

My index is something like:

{
   SongId: 1,
   Title: "My song",
   Artist: "Artist",
   Customers: [
   {
      CustomerId: 1,
      ...more customer data
   }
   {
      CustomerId: 2,
      ...more customer data
   }
   ]
}

I need to get the song filtering by title and only get the customer's 1 data or no customer if the customer 1 is not in the collection for that song. Is that possible?


Solution

  • This is not possible today. You'd have to filter out the complex collection elements on the client-side.

    Also, you didn't mention this in your question, but it seems the cardinality of the relationship from songs to customers could be quite high. If that's the case, you should consider a different data model, because there is a hard limit on the number of elements you can have in complex collections per document. Even without that limit, there are practical limits to complex collections, for example around document size and the inability to incrementally update them during indexing.

    If your model will have at most a few hundred customers per song, you're probably fine, but if it's in the thousands or higher, you should reconsider your design.