Francesco Borraccino
Francesco Borraccino

Reputation: 101

How to filter a ComplexTypeCollection field in Azure Search to find records containing specific text?

I'm using Azure Search and have an index with a field of type ComplexTypeCollection called ComplexField. Each item in ComplexField has a text property called TextProperty.

I would like to know how to apply a filter on ComplexField to retrieve all records where TextProperty contains a specific text. For example, if the search text is "example," I want to get all records where at least one of the items in ComplexField has TextProperty that contains the string "example."

Here are the details of the ComplexField:

{
  "name": "ComplexField",
  "type": "Collection(Edm.ComplexType)",
  "fields": [
    {
      "name": "Id",
      "type": "Edm.Int32",
      "searchable": false,
      "filterable": false,
      "retrievable": true,
      "stored": true,
      "sortable": false,
      "facetable": false,
      "key": false,
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "analyzer": null,
      "normalizer": null,
      "dimensions": null,
      "vectorSearchProfile": null,
      "vectorEncoding": null,
      "synonymMaps": []
    },
    {
      "name": "TextProperty",
      "type": "Edm.String",
      "searchable": false,
      "filterable": true,
      "retrievable": true,
      "stored": true,
      "sortable": false,
      "facetable": false,
      "key": false,
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "analyzer": null,
      "normalizer": null,
      "dimensions": null,
      "vectorSearchProfile": null,
      "vectorEncoding": null,
      "synonymMaps": []
    }
  ]
}

Here is an example of the data structure:

{
  "records": [
    {
      "id": "1",
      "ComplexField": [
        { "TextProperty": "example text" },
        { "TextProperty": "another text" }
      ]
    },
    {
      "id": "2",
      "ComplexField": [
        { "TextProperty": "some text" }
      ]
    }
  ]
}

How can I write a query to get all records where TextProperty in ComplexField contains "example"? Do I need to change any field properties to make this work?

Thanks in advance for your help!

Upvotes: 0

Views: 96

Answers (1)

Sampath
Sampath

Reputation: 3639

To filter ComplexTypeCollection fields in Azure Cognitive Search based on the contents of a nested TextProperty, you can use OData filtering syntax, since the TextProperty in your ComplexField collection is set as filterable.

Below is the sample example :

  { 
  "name": "FieldName", 
  "type": "Edm.ComplexType",
  "fields": [
    { 
      "name": "SubFieldName1", 
      "type": "Edm.String", 
      "filterable": false, 
      "sortable": false, 
      "facetable": false, 
      "searchable": true 
    },
    { 
      "name": "SubFieldName2", 
      "type": "Edm.String", 
      "searchable": true, 
      "filterable": true, 
      "sortable": true, 
      "facetable": true 
    }
  ]
}


Update your index and filter the collection by using any to filter on a specific condition within the collection.

The following filter query retrieves records where any item in ComplexField has a TextProperty containing the exact phrase, as shown below:

$filter=ComplexField/any(c: contains(c/TextProperty, 'example'))

I followed this link to update an index, using this sample example of a complex structure from this documentation1.

enter image description here

I have added documents to Azure AI Search index using this guide, based on the above complex type and following the example from docment1.

enter image description here

I followed this link for text query filters in Azure AI Search, using the OData filter.

Output :

enter image description here

Upvotes: 0

Related Questions