Saturday, July 26, 2025

 Multitenancy on an index store of an Azure AI search requires entries to be filtered based on tenants/accounts and the operation is different from executing SQL standard query operators. The expression used to filter the entries is based exclusively on OData search syntax. Some examples might explain this:

#! /usr/bin/python

import json

import sys

import os

import requests

from azure.core.credentials import AzureKeyCredential

from azure.identity import DefaultAzureCredential

from azure.search.documents import SearchClient

from azure.search.documents.indexes import SearchIndexClient

sys.path.insert(0, os.path.abspath(".."))

from visionprocessor.vectorizer import vectorize_image

search_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")

index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")

api_version = os.getenv("AZURE_SEARCH_API_VERSION")

search_api_key = os.getenv("AZURE_SEARCH_ADMIN_KEY")

vision_api_key = os.getenv("AZURE_AI_VISION_API_KEY")

vision_api_version = os.getenv("AZURE_AI_VISION_API_VERSION")

vision_region = os.getenv("AZURE_AI_VISION_REGION")

vision_endpoint = os.getenv("AZURE_AI_VISION_ENDPOINT")

credential = AzureKeyCredential(search_api_key)

search_client = SearchClient(endpoint=search_endpoint, index_name=index_name, credential=credential)

query_text = "green street crossing mark for bicycles"

odata_filter = "id eq '008333'"

odata_filter = "search.in(title, 'aerial', ' ')"

odata_filter = "search.in(tags, 'urban design')"

odata_filter = "tags eq 'building,urban design,car,house,land vehicle,vehicle,outdoor,city,aerial,truck'"

odata_filter = "search.ismatch('urban*', 'tags')"

#only-for-collection-fields odata_filter = "tags/any(g: search.in(g, 'urban', ' '))"

# and effect of alternate jargon

results = search_client.search(

    # query_type='simple',

    # search_text=query_text,

    select='id,description',

    filter=odata_filter,

    include_total_count=True,

    top=10)

print(repr(results))

if results:

    print(f"Number of results: {results.get_count()}")

    for result in results:

         if result:

            print(f"{result['id']}")

"""

<iterator object azure.core.paging.ItemPaged at 0x1df4252ee40>

Number of results: 1

015969

<iterator object azure.core.paging.ItemPaged at 0x227467dee40>

Number of results: 7893

000688

000735

000720

000766

000741

000771

000775

000820

000824

000791

"""

Please note that id is not a filterable attribute. Additional attribute such as account id becomes necessary to do that.

No comments:

Post a Comment