Search code examples
azure-cognitive-search

Selecting nested objects which satisfy a predicate


In Azure search, is it possible to select objects in an array (Collection(Edm.ComplexType) field) which satisfy a predicate?

Using the any operator specified at https://learn.microsoft.com/en-us/azure/search/search-query-understand-collection-filters#correlated-versus-uncorrelated-search returns the entire root object if any of the objects in the array satisfies the predicate.

Example, given the object below in Azure search:

{
  "arrayOfObjects": [
    {
      "id": 1,
      "foo": "a"
    },
    {
      "id": 2,
      "foo": "b"
    },
    {
      "id": 3,
      "foo": "b"
    }
  ]
}

Is it possible to select only the nested objects where foo equals “b”, so that the search response looks like this:

{
  "arrayOfObjects": [
    {
      "id": 2,
      "foo": "b"
    },
    {
      "id": 3,
      "foo": "b"
    }
  ]
}

Solution

  • No, this is not possible. Queries in Azure Search operate at the granularity of documents, not objects within documents. A possible workaround would be to model your index such that the individual objects become top-level documents.