Azure Cognitive Search:

Question:

I have recently upgraded my Azure Cognitive Search instance so it has semantic search.

However, when I add query_type=semantic, in the client search I get the following stacktrace…

Traceback (most recent call last):
  File "call_semantic_search.py", line 34, in <module>
    c, r = main(search_text='what is a ')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "call_semantic_search.py", line 28, in main
    count: float = search_results.get_count()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/search/documents/_paging.py", line 82, in get_count
    return self._first_iterator_instance().get_count()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/search/documents/_paging.py", line 91, in wrapper
    self._response = self._get_next(self.continuation_token)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/search/documents/_paging.py", line 115, in _get_next_cb
    return self._client.documents.search_post(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/search/documents/_generated/operations/_documents_operations.py", line 312, in search_post
    raise HttpResponseError(response=response, model=error)
azure.core.exceptions.HttpResponseError: () The request is invalid. Details: parameters : Requested value 'semantic' was not found.
Code: 
Message: The request is invalid. Details: parameters : Requested value 'semantic' was not found.

This is the code that I have been using to call the search index.

import logging
from typing import Dict, Iterable, Tuple

import settings as settings
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from search import SearchableItem

TOP = 10
SKIP = 0


def main(search_text: str) -> Tuple[float, Iterable[Dict]]:
    client = SearchClient(
        api_version="2021-04-30-Preview",
        endpoint=settings.SEARCH_SERVICE_ENDPOINT,
        index_name=settings.SOCIAL_IDX_NAME,
        credential=AzureKeyCredential(key=settings.SEARCH_SERVICE_KEY)
    )
    logging.info(f"Calling: /search?top={TOP}&skip={SKIP}&q={search_text}")
    search_results = client.search(
        search_text=search_text,
        top=TOP,
        skip=SKIP,
        query_type="semantic",
        include_total_count=True,
    )
    count: float = search_results.get_count()
    results = SearchableItem.from_result_as_dict(search_results)
    return count, results


if __name__ == "__main__":
    count, results = main(search_text='what is a ')
    print(count, list(results))

And here is my Azure configuration (I’m able to perform Semantic searches via the portal:

enter image description here

EDITS

Taking @Thiago Custodio’s advice;

I enabled logging with:

import sys


logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

# ...
    search_results = client.search(
        search_text=search_text,
        top=TOP,
        skip=SKIP,
        query_type="semantic",
        include_total_count=True,
        logging_enable=True
    )
# ...

And I got the following:

DEBUG:azure.core.pipeline.policies._universal:Request URL: 'https://search.windows.net//indexes('idx-name')/docs/search.post.search?api-version=2020-06-30'
Request method: 'POST'
Request headers:
    'Content-Type': 'application/json'
    'Accept': 'application/json;odata.metadata=none'
    'Content-Length': '86'
    'x-ms-client-request-id': 'fbaafc9e-qwww-11ed-9117-a69cwa6c72e'
    'api-key': '***'
    'User-Agent': 'azsdk-python-search-documents/11.3.0 Python/3.11.1 (macOS-13.0-x86_64-i386-64bit)'

So this shows the request URL going out is pinned to api-version=2020-06-30 – in the Azure Portal, if I change the search version to the same, semantic search is unavailable.

I seem to have an outdated version of the search library even though I installed via:

pip install azure-search-documents

The most notable difference is that in my local azure/search/documents/_generated/operations/_documents_operations.py – the api_version seems to be hardcoded to 2020-06-30 see:

enter image description here

Looking at the source, I actually need the api_version to be dynamically set, so at the caller I can pass it in the search client. This is something thats already implemented within there main branch of the source, see: Source, but for some reason, my local version is different

Asked By: Bob

||

Answers:

from your code:

search_results = client.search(
        search_text=search_text,
        top=TOP,
        skip=SKIP,
        query_type="semantic",
        include_total_count=True,
    )

Semantic search is not a parameter, but an endpoint. Rather than calling /search, you should call /semantic

that’s what you need:

def semantic_ranking():
    # [START semantic_ranking]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
    index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
    api_key = os.getenv("AZURE_SEARCH_API_KEY")

    credential = AzureKeyCredential(api_key)
    client = SearchClient(endpoint=endpoint,
                          index_name=index_name,
                          credential=credential)
    results = list(client.search(search_text="luxury", query_type="semantic", query_language="en-us"))

note: query_type part in the last line

Answered By: Thiago Custodio

Fixed with:

azure-search-documents==11.4.0b3
Answered By: Bob
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.