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の検索手法実装まとめ(なるべくシンプルに)

Posted at

Azure AI Searchには、フルテキスト検索やベクトル検索といったいくつかの検索手法が用意されていますが、それらの切り替えは単にオプションの値を変えれば良いというものではなく、事前に準備しなければいけないものがあったりします。そういったことは資料としてまとまってはおらず、自分としても混乱したため備忘録としてもここに残しておきます。

まず前提として以下のようなインデックスがAI Searchに定義されているとします。
スクリーンショット 2025-05-05 14.34.40.png

その上で以下の下準備を行います。

import os
from dotenv import load_dotenv
from azure.search.documents import SearchClient
from azure.search.documents.models import VectorizedQuery
from azure.core.credentials import AzureKeyCredential
from openai import AzureOpenAI

dotenv_path = '.env'
load_dotenv(dotenv_path=dotenv_path)

credential = AzureKeyCredential(os.getenv("SEARCH_API_KEY"))
search_client = SearchClient(endpoint=os.getenv("SEARCH_ENDPOINT"),
                      index_name=index_name,
                      credential=credential)

index_name = "portfolio" # 検索したいインデックス
TOP=3 # 取得するドキュメント数

# 検索したいクエリ
query_text = "四条河原町住みで、3年以上の開発経験があり、コミュニケーションがしっかり取れる。"

# フルテキスト検索で検索したいフィールド
search_fields=["name", "address","academic_background","certification","career"]
# ベクトル検索で検索したいフィールド
vector_search_fileds=["self_introduce"], 
# 検索結果で取得したいフィールド
output_field = ["name", "address","academic_background","certification","career"]

フルテキスト検索

results = search_client.search(
    search_text=query_text, # フルテキスト検索したいクエリ
    search_fields=search_fields, # 検索対象のフィールド
    top=TOP, # 取得したいドキュメント数
    select=output_field,  # 出力結果に含めるフィールド
)

ベクトル検索

まずは検索クエリのベクトル化を行う。
ここのベクトル化は、インデックスに格納されているドキュメントのベクトルフィールドをベクトル化したものと同じものを使うことに注意する。

# 埋め込みの例
client = AzureOpenAI(
  api_key = os.getenv("AZURE_OPENAI_API_KEY"),  
  api_version = "2024-02-01",
  azure_endpoint =os.getenv("AZURE_OPENAI_ENDPOINT") 
)

response = client.embeddings.create(
    input = query_text,
    model= "text-embedding-3-large"
)

vector = response.data[0].embedding

ベクトル化した検索クエリで検索をかける。

results = search_client.search(
    search_text=None,# フルテキスト検索の検索クエリは、ベクトル検索時にはNoneにする
    vector_queries = [VectorizedQuery(vector=vector, k_nearest_neighbors=TOP, fields=vector_search_fileds)] if vector else None, # ベクトル検索したいクエリ
    select=output_field,  # 出力結果に含めるフィールド
)

ハイブリッド検索

vectorはベクトル検索の時と同様に、すでにベクトル化したものを想定している。

results = search_client.search(
    search_text=query_text, # フルテキスト検索したいクエリ
    search_fields=search_fields, # 検索対象のフィールド
    top=TOP, # 取得したいドキュメント数
    vector_queries = [VectorizedQuery(vector=vector, k_nearest_neighbors=TOP, fields="vector")] if vector else None, # ベクトル検索したいクエリ
    select=output_field,  # 出力結果に含めるフィールド
)

セマンティックハイブリッド検索

セマンティックハイブリッド検索を行うには、AI Searchの費用とは別に費用のかかるセマンティックランカーを使う必要があることに注意。
なお、セマンティックランカーはAI Searchのfreeプランでは使うことができない。

vectorはベクトル検索の時と同様に、すでにベクトル化したものを想定している。

results = search_client.search(
    query_type="semantic",# セマンティックランカーを使うことを宣言
    search_text=query_text, # フルテキスト検索したいクエリ
    top=TOP, # 取得したいドキュメント数
    vector_queries= [VectorizedQuery(vector=vector, k_nearest_neighbors=TOP, fields="vector")] if vector else None, # ベクトル検索したいクエリ
    select=output_field,  # 出力結果に含めるフィールド
)
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?