【AWS re:Invent 2025】OpenSearchのセマンティック検索(意味検索)を試してみた

【AWS re:Invent 2025】OpenSearchのセマンティック検索(意味検索)を試してみた

2026.01.06

目次

  1. この記事でやること / やらないこと
  2. セマンティック検索(意味検索)とは
  3. 手を動かす:Dev Toolsでセットアップと検索
  4. レキシカル検索との比較
  5. セマンティック検索の注意点
  6. まとめ

この記事でやること / やらないこと

この記事は、AWS re:Invent 2025 のワークショップ(Lab 2: ML-powered search / Semantic search)で学んだ内容を、自分の環境で再現して理解を深めた記録です。

前回の記事では、レキシカル検索(キーワード検索)の限界を体感しました。今回は、その弱点を補なう手法として挙げられる セマンティック検索(ベクトル検索) を試してみます。

やったこと

  1. Bedrock(Cohere Embed English)の embedding model を使うための connector_id を取得
  2. embedding model を登録&デプロイ(model_id を取得)
  3. ingest pipeline を作成し、投入時に product_description を embedding
  4. demostore-search-index を k-NN 対応で作り直す
  5. Dev Tools から neural query(semantic search)を実行
  6. レキシカル検索との結果を比較

省略したこと

  • 環境構築・ログイン手順の詳細
  • VPC 内配置などの本番向け設定

補足:検証目的のため、パブリックアクセス可能な構成で実施しました。実運用では セキュリティの観点からVPC 内配置 を基本に考えてください。


セマンティック検索(意味検索)とは

レキシカル検索の限界(前回のおさらい)

前回の記事で、レキシカル検索には以下の限界があることを体感しました。

クエリ 期待 実際の結果
warm winter outerwear 冬用ジャケット 冬用ブーツ

原因:outerwearjacket, coat という同義語を理解できない

セマンティック検索の仕組み

通常の検索(レキシカル検索)は「キーワードが一致するか」で探しますが、セマンティック検索は「意味が近いか」で探します。
例えば「車」で検索すると、「自動車」「クルマ」「カー」なども見つかります。

仕組み(ざっくり)

  1. データ登録時:テキストをembedding モデルで「ベクトル」に変換して保存
  2. 検索時:検索ワード(クエリ)も同じように変換し、k-NN で近いベクトルを持つテキストを探す

OpenSearchでは「Neural Search」という機能でこれを実現します。


手を動かす:Dev Toolsでセットアップと検索

Step 0: MLコネクターを作成

MLコネクターは、OpenSearchからBedrockのembedding modelを呼び出すための設定です。

事前準備:IAMロールを作成

OpenSearchがBedrockを呼び出すためのIAMロールを作成します。

1. 許可ポリシー(BedrockInvokeModelOnly)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "bedrock:InvokeModel",
            "Resource": "*"
        }
    ]
}

2. 信頼ポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "es.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

CloudShellでコネクターを作成

1. awscurlをインストール

pip install awscurl

2. コネクターを作成

以下の箇所を自分の環境に置き換えてください

  • <OpenSearchのドメインエンドポイント>
  • <作成したIAMロールのARN>
awscurl --service es \
  --region ap-northeast-1 \
  -X POST \
  "https://<OpenSearchのドメインエンドポイント>/_plugins/_ml/connectors/_create" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "BEDROCK_TEXT_COHERE:EMBEDDING",
    "description": "Cohere Embedding via Amazon Bedrock",
    "version": "1",
    "protocol": "aws_sigv4",
    "credential": {
      "roleArn": "<作成したIAMロールのARN>"
    },
    "parameters": {
      "region": "us-east-1",
      "service_name": "bedrock"
    },
    "actions": [
      {
        "action_type": "predict",
        "method": "POST",
        "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/cohere.embed-english-v3/invoke",
        "headers": {
          "Content-Type": "application/json"
        },
        "request_body": "{ \"texts\": ${parameters.texts}, \"input_type\": \"search_document\" }",
        "pre_process_function": "connector.pre_process.cohere.embedding",
        "post_process_function": "connector.post_process.cohere.embedding"
      }
    ]
  }'

3. 成功するとconnector_idが返ってきます

{
  "connector_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

Step 1: connector_id を取得

上記のconnector_idをDev Tools上でも確認してみます。

POST /_plugins/_ml/connectors/_search
{
  "size": 1,
  "query": {
    "match_phrase": { "name": "BEDROCK_TEXT_COHERE:EMBEDDING" }
  }
}

1

返ってきた _id ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")を控えます。

Step 2: embedding model を登録

以下の箇所を自分の環境に置き換えてください

  • <connector_id>
POST _plugins/_ml/models/_register?deploy=true
{
  "name": "Bedrock Text Embedding",
  "function_name": "remote",
  "connector_id": "<connector_id>"
}

2

返ってきた model_id("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") を控えます。

Step 3: ingest pipeline(データを保存する前に、自動でベクトル化する設定) を作成

以下の箇所を自分の環境に置き換えてください

  • <model_id>
PUT _ingest/pipeline/ml_ingest_pipeline
{
  "description": "text embedding ingest pipeline",
  "processors": [
    {
      "text_embedding": {
        "model_id": "<model_id>",
        "field_map": {
          "product_description": "product_description_vector"
        }
      }
    }
  ]
}

3

パラメータ 意味
ml_ingest_pipeline パイプラインの名前
text_embedding テキストをベクトルに変換する処理
model_id 使用するモデル(さっき登録したもの)
field_map どのフィールドを変換するか

Step 4: k-NN index(セマンティック検索用のデータベース) を作成

DELETE demostore-search-index
PUT demostore-search-index
{
  "settings": {
    "index.knn": true,
    "default_pipeline": "ml_ingest_pipeline"
  },
  "mappings": {
    "properties": {
      "product_description": { "type": "text" },
      "product_description_vector": {
        "type": "knn_vector",
        "dimension": 1024,
        "method": {
          "engine": "faiss",
          "space_type": "l2",
          "name": "hnsw"
        }
      },
      "category": { "type": "keyword" },
      "gender": { "type": "keyword" }
    }
  }
}

4

パラメータ 意味
demostore-search-index インデックス(データベース)の名前
index.knn k-NN検索を有効にする設定
default_pipeline データ登録時に自動で実行するパイプライン
product_description 商品説明(テキスト形式で保存)
product_description_vector 商品説明をベクトル化したもの
knn_vector ベクトル検索用のフィールド型
dimension: 1024 ベクトルの次元数(Cohereモデルに合わせる)
engine: faiss ベクトル検索エンジン(高速)
space_type: l2 距離の計算方法(ユークリッド距離)
name: hnsw 検索アルゴリズム(高速な近似検索)
category カテゴリ(完全一致検索用)
gender 性別(完全一致検索用)

Step 5: データを再投入

前回(レキシカル検索で使用したもの)と同じ50件のデータを投入します。パイプラインにより自動で ベクトル(embedding)が生成されます。

POST demostore-search-index/_bulk
{"index":{}}
{"product_id":"1","product_name":"Classic Black Leather Jacket","product_description":"Premium black leather jacket for men. Perfect for casual and formal occasions.","category":"jackets","price":199.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"2","product_name":"Men's Black Running Shoes","product_description":"Lightweight black running shoes for men. Breathable mesh upper.","category":"shoes","price":89.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"3","product_name":"Women's Red Backpack","product_description":"Stylish red backpack for women. Multiple compartments for travel.","category":"bags","price":59.99,"color":"red","gender":"women"}
{"index":{}}
{"product_id":"4","product_name":"Black Denim Jacket","product_description":"Trendy black denim jacket for men. Classic fit with button closure.","category":"jackets","price":79.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"5","product_name":"Silver Bracelet Set","product_description":"Elegant silver bracelets for men. Set of 3 designs.","category":"accessories","price":45.99,"color":"silver","gender":"men"}
{"index":{}}
{"product_id":"6","product_name":"Women's Black Tote Bag","product_description":"Spacious black tote bag for women. Faux leather with gold accents.","category":"bags","price":69.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"7","product_name":"Men's Black Wool Coat","product_description":"Elegant black wool coat for men. Perfect for winter formal occasions.","category":"jackets","price":299.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"8","product_name":"Black Hiking Boots","product_description":"Durable black hiking boots for men. Waterproof with ankle support.","category":"shoes","price":149.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"9","product_name":"Women's Black Leather Jacket","product_description":"Stylish black leather jacket for women. Slim fit with zipper.","category":"jackets","price":189.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"10","product_name":"Black Canvas Sneakers","product_description":"Classic black canvas sneakers for men. Comfortable everyday black shoes.","category":"shoes","price":49.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"11","product_name":"Men's Navy Blazer","product_description":"Professional navy blazer jacket for men. Perfect for business meetings.","category":"jackets","price":179.99,"color":"navy","gender":"men"}
{"index":{}}
{"product_id":"12","product_name":"Black Oxford Shoes","product_description":"Formal black oxford shoes for men. Genuine leather, perfect for suits.","category":"shoes","price":129.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"13","product_name":"Women's Denim Jacket","product_description":"Classic blue denim jacket for women. Vintage wash with brass buttons.","category":"jackets","price":69.99,"color":"blue","gender":"women"}
{"index":{}}
{"product_id":"14","product_name":"Black Sports Watch","product_description":"Digital black sports watch for men. Water resistant with multiple functions.","category":"accessories","price":79.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"15","product_name":"Men's Black Puffer Jacket","product_description":"Warm black puffer jacket for men. Lightweight down filling for cold weather.","category":"jackets","price":159.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"16","product_name":"Black Running Trainers","product_description":"High performance black running trainers for men. Extra cushioning for long runs.","category":"shoes","price":119.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"17","product_name":"Women's Red Heels","product_description":"Elegant red high heels for women. Perfect for parties and special occasions.","category":"shoes","price":89.99,"color":"red","gender":"women"}
{"index":{}}
{"product_id":"18","product_name":"Black Messenger Bag","product_description":"Professional black messenger bag for men. Multiple compartments for laptop.","category":"bags","price":79.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"19","product_name":"Men's Suede Jacket","product_description":"Premium brown suede jacket for men. Soft texture with classic design.","category":"jackets","price":219.99,"color":"brown","gender":"men"}
{"index":{}}
{"product_id":"20","product_name":"Black Loafers","product_description":"Comfortable black loafers for men. Slip-on design for easy wear.","category":"shoes","price":99.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"21","product_name":"Women's White Blouse","product_description":"Elegant white blouse for women. Perfect for office and casual wear.","category":"tops","price":49.99,"color":"white","gender":"women"}
{"index":{}}
{"product_id":"22","product_name":"Black Leather Wallet","product_description":"Genuine black leather wallet for men. Multiple card slots and coin pocket.","category":"accessories","price":45.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"23","product_name":"Men's Windbreaker Jacket","product_description":"Lightweight black windbreaker jacket for men. Perfect for outdoor activities.","category":"jackets","price":89.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"24","product_name":"Black Basketball Shoes","product_description":"Professional black basketball shoes for men. High top design with ankle support.","category":"shoes","price":139.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"25","product_name":"Women's Crossbody Bag","product_description":"Compact black crossbody bag for women. Adjustable strap with gold hardware.","category":"bags","price":59.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"26","product_name":"Men's Quilted Jacket","product_description":"Stylish black quilted jacket for men. Diamond pattern with snap buttons.","category":"jackets","price":129.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"27","product_name":"Black Dress Shoes","product_description":"Classic black dress shoes for men. Perfect for weddings and formal events.","category":"shoes","price":109.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"28","product_name":"Women's Pink Sneakers","product_description":"Trendy pink sneakers for women. Lightweight with memory foam insole.","category":"shoes","price":69.99,"color":"pink","gender":"women"}
{"index":{}}
{"product_id":"29","product_name":"Black Sunglasses","product_description":"Stylish black sunglasses for men. UV protection with polarized lenses.","category":"accessories","price":59.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"30","product_name":"Men's Motorcycle Jacket","product_description":"Rugged black motorcycle jacket for men. Heavy duty leather with armor pockets.","category":"jackets","price":279.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"31","product_name":"Black Slip-On Shoes","product_description":"Casual black slip-on shoes for men. Elastic sides for easy on and off.","category":"shoes","price":59.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"32","product_name":"Women's Trench Coat","product_description":"Classic beige trench coat for women. Double breasted with belt.","category":"jackets","price":199.99,"color":"beige","gender":"women"}
{"index":{}}
{"product_id":"33","product_name":"Black Gym Bag","product_description":"Spacious black gym bag for men. Separate shoe compartment included.","category":"bags","price":49.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"34","product_name":"Men's Varsity Jacket","product_description":"Retro style black varsity jacket for men. Wool body with leather sleeves.","category":"jackets","price":149.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"35","product_name":"Black Chelsea Boots","product_description":"Sleek black chelsea boots for men. Elastic side panels with pull tab.","category":"shoes","price":159.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"36","product_name":"Women's Yoga Pants","product_description":"Comfortable black yoga pants for women. High waist with stretch fabric.","category":"bottoms","price":39.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"37","product_name":"Black Tie","product_description":"Silk black tie for men. Perfect for formal occasions and business.","category":"accessories","price":29.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"38","product_name":"Men's Fleece Jacket","product_description":"Cozy black fleece jacket for men. Full zip with side pockets.","category":"jackets","price":79.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"39","product_name":"Black Tennis Shoes","product_description":"Professional black tennis shoes for men. Non-marking sole for court use.","category":"shoes","price":89.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"40","product_name":"Women's Shoulder Bag","product_description":"Elegant black shoulder bag for women. Quilted design with chain strap.","category":"bags","price":129.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"41","product_name":"Men's Track Jacket","product_description":"Athletic black track jacket for men. Moisture wicking with reflective details.","category":"jackets","price":69.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"42","product_name":"Black Boat Shoes","product_description":"Classic black boat shoes for men. Leather upper with non-slip sole.","category":"shoes","price":79.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"43","product_name":"Women's Winter Boots","product_description":"Warm black winter boots for women. Faux fur lining with waterproof exterior.","category":"shoes","price":119.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"44","product_name":"Black Belt","product_description":"Classic black leather belt for men. Silver buckle with adjustable size.","category":"accessories","price":35.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"45","product_name":"Men's Safari Jacket","product_description":"Adventure style khaki safari jacket for men. Multiple pockets for travel.","category":"jackets","price":139.99,"color":"khaki","gender":"men"}
{"index":{}}
{"product_id":"46","product_name":"Black Skate Shoes","product_description":"Durable black skate shoes for men. Reinforced toe cap with grip sole.","category":"shoes","price":74.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"47","product_name":"Women's Clutch Bag","product_description":"Glamorous black clutch bag for women. Perfect for evening events.","category":"bags","price":49.99,"color":"black","gender":"women"}
{"index":{}}
{"product_id":"48","product_name":"Men's Rain Jacket","product_description":"Waterproof black rain jacket for men. Packable design with hood.","category":"jackets","price":99.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"49","product_name":"Black Work Boots","product_description":"Heavy duty black work boots for men. Steel toe with oil resistant sole.","category":"shoes","price":129.99,"color":"black","gender":"men"}
{"index":{}}
{"product_id":"50","product_name":"Women's Cardigan","product_description":"Soft gray cardigan for women. Button front with ribbed cuffs.","category":"tops","price":59.99,"color":"gray","gender":"women"}

5

Step 6: セマンティック検索を実行

以下の箇所を自分の環境に置き換えてください

  • <model_id>
GET demostore-search-index/_search
{
  "_source": ["product_name"],
  "query": {
    "neural": {
      "product_description_vector": {
        "query_text": "warm winter outerwear",
        "model_id": "<model_id>",
        "k": 5
      }
    }
  }
}

6

パラメータ 意味
_source 検索結果に含めるフィールド(ここでは商品名のみ表示)
neural セマンティック検索(ベクトル検索)を行うクエリ
product_description_vector 検索対象のベクトルフィールド
query_text 検索したいテキスト(「暖かい冬のアウター」)
model_id テキストをベクトル化するモデル(Step 1で登録したもの)
k 返す件数(意味が近い上位5件を取得)

レキシカル検索との比較

結果比較

レキシカル検索の結果(前回)

順位 スコア(BM25) 商品名 カテゴリ
1位 2.64 Women's Winter Boots shoes ⚠️
2位 1.37 Men's Black Wool Coat jackets ✅
3位 1.32 Men's Black Puffer Jacket jackets ✅

セマンティック検索の結果

順位 スコア(cos類似度) 商品名 カテゴリ
1位 0.518 Women's Winter Boots shoes ⚠️
2位 0.513 Men's Black Puffer Jacket jackets ✅
3位 0.500 Men's Black Wool Coat jackets ✅
4位 0.478 Men's Fleece Jacket jackets ✅
5位 0.465 Women's Cardigan tops

なぜ両方とも Winter Boots が1位なのか?

Women's Winter Boots の商品説明:

"Warm black winter boots for women. Faux fur lining with waterproof exterior."

  • warmwinter の両方を含んでいる
  • セマンティック検索でも「暖かい冬用アイテム」として高い類似度を持つ

→ この商品説明がクエリ「warm winter outerwear」に対して、両方の検索方式で高評価を受けるのは妥当

違い:候補の幅

検索方式 上位に含まれる商品
レキシカル warm または winter を含む商品のみ
セマンティック キーワードを含まなくても「暖かいアウター」の意味を持つ商品も含む

セマンティック検索では、Fleece Jacket(フリース)や Cardigan(カーディガン)など、warm winter を含まない商品も「暖かい衣類」として上位に入っています

filter を使えば意図通りの結果に

jackets カテゴリに絞るクエリ例

GET demostore-search-index/_search
{
  "_source": ["product_name"],
  "query": {
    "neural": {
      "product_description_vector": {
        "query_text": "warm winter outerwear",
        "model_id": "<model_id>",
        "k": 5,
        "filter": {
          "term": { "category": "jackets" }
        }
      }
    }
  }
}

7

順位 スコア 商品名 カテゴリ
1位 0.513 Men's Black Puffer Jacket jackets ✅
2位 0.500 Men's Black Wool Coat jackets ✅
3位 0.478 Men's Fleece Jacket jackets ✅
4位 0.452 Men's Suede Jacket jackets ✅
5位 0.451 Men's Windbreaker Jacket jackets ✅

ポイント

  • Winter Boots が除外された(category が shoes のため)
  • Puffer Jacket が1位に(「暖かい冬用アウター」の意味に最も近い)
  • filter を使うことで、意図通りの結果を得られた

セマンティック検索の注意点

注意点①:属性フィルタリングには filter を併用

セマンティック検索は「意味の近さ」で検索するため、属性(gender, category など)の区別が曖昧になることがあります。

例:「men's black jacket」で検索した場合

検索方式 結果
レキシカル 「men」を含む商品が自然と上位に
セマンティック 「黒いジャケット」の意味が近ければ、women 向けも上位に混ざる可能性

解決策:filter を併用

注意点②:コストがかかる

コスト種別 内容
API呼び出し 投入時・検索時に embedding 生成の API 呼び出しが発生
ストレージ ベクトル(1024次元 × ドキュメント数)の保存領域が必要

まとめ

特性 レキシカル検索 セマンティック検索
検索方法 単語の一致 意味の近さ
同義語 理解できない 理解できる
検索意図 理解できない ある程度理解
属性フィルタ キーワード一致で自然に絞られる 明示的な filter が必要
コスト 低い 高い

今回の検証結果

  • warm winter outerwear」の検索では、両方とも Winter Boots が1位
  • ただしセマンティック検索は候補の幅が広い(キーワードを含まない商品も上位に)
  • 単独では完璧でないが、filter との組み合わせで真価を発揮

次のステップとして、次回は、Rerankを導入して検索精度がどう変わるか検証してみたいと思います。

項目 説明
Rerankとは 検索結果を「並べ替え直す」専用モデル
仕組み 上位N件をクエリとの関連度で再スコアリング
効果 「暖かい冬のアウター」に本当に関連する商品を上位に

この記事をシェアする

FacebookHatena blogX

関連記事