0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

背景

Azure AI Search で Index に何が格納されているか?を確認する方法が欲しかったので用意した記録

サンプルコード

AZURE_SEARCH_SERVICE の対象とする index_name 内のドキュメントを一覧表示します

大量にある場合は、ループを途中で止めるなどの処理は追加必要

preview documents in index
import os
from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from dotenv import load_dotenv
from icecream import ic

dotenv_path = os.path.join(".azure", "{azd env name}", ".env")
load_dotenv(dotenv_path)

AZURE_SEARCH_SERVICE = os.getenv("AZURE_SEARCH_SERVICE")
service_endpoint = f"https://{AZURE_SEARCH_SERVICE}.search.windows.net"
index_name = "index"

credential = DefaultAzureCredential()
search_client = SearchClient(endpoint=service_endpoint, index_name=index_name, credential=credential)

def get_documents(search_client, top=None):
    if top:
        results = search_client.search(search_text="*", top=top)
    else:
        results = search_client.search(search_text="*")
    documents = [result for result in results]
    return documents

number_of_documents = None  # Set to None to retrieve all documents
documents = get_documents(search_client, number_of_documents)
if len(documents) == 0:
    print("No documents found.")
else:
    for doc in documents:
        doc_copy = doc.copy()
        doc_copy = {k: v for k, v in doc_copy.items() if v is not None} # None 要素は非表示

        if "embedding" in doc_copy: # embedding は大量の配列なので省略表示
            doc_copy["embedding"] = doc_copy["embedding"][:1]  # embeddingの最初の要素だけ残す
        ic(doc_copy)
    print(f"Retrieved {len(documents)} documents.")

あとがき

新しい機能がバンバン入ってくるわけで・・skillset とか試したいところ :sweat:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?