Search code examples
azuremessageazure-cognitive-search

Personalized search in Azure Search


I am looking at Azure Search for a message service. Very simplified, we have an Azure App Service (running .NET) and two tables on SQL Azure:

  • Message: Id, Subject, Body, [...]
  • MessageRecipient: Id, MessageId, UserId, [...]

One message has 1:N recipients.

I want an index to search the Subject and Body of Message, but the results must be filtered on the current user being a recipient of the message. I can't seem to find a good way to implement this so it scales well to high volumes. Returning all search results from the index and then filtering on UserId in my application would generate a lot of unnecessary overhead. And indexing each message duplicated for each UserId is even worse.

How would you best implement a recipient-filtered message search?

Thanks, TGM


Solution

  • In similar situations I have utilized the Collection(Edm.String) data type in my search index. So an example document from your index would look something like this:

    {
        messageId : 1,
        subject : "Why Azure Search is cool",
        body: "Just because",
        recipientIds : ["123", "456", "789"]
    }
    

    The recipientIds field is defined as type Collection(Edm.String) in the index schema.

    Then you can use the "any" and "all" OData expressions to filter your searches. So, if you wanted to find messages sent to recipient id 123 your filter expression would look like this:

    $filter=recipientIds/any(r: r eq '123')
    

    See the Azure Search OData expression docs for more details.