python Azure认知搜索:

vwhgwdsa  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(168)

我最近升级了Azure认知搜索示例,使其具有语义搜索功能。
然而,当我添加query_type=semantic时,在客户机搜索中我得到了以下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.

这是我一直用来调用搜索索引的代码。

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))

这是我的Azure配置(我能够通过门户执行语义搜索:

iszxjhcz

iszxjhcz1#

从您的代码:

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

语义搜索不是参数,而是端点,不调用/search,而调用/semantic
这才是你需要的

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"))

注意:最后一行中的query_type部分

相关问题