
Amazon DynamoDB ベクトル検索のインラインフィルターが範囲比較・IN・属性の有無・OR / NOT に対応したので試してみた
はじめに
2026年11月20日、Amazon DynamoDB ベクトル検索のインラインフィルターが範囲比較、値のリスト、属性の有無、論理演算子をサポートしました。
インラインフィルターは、SearchVectors の検索条件式(SearchConditionExpression)に書いた属性の条件でアイテムを絞り込む機能です。DynamoDB が検索中にフィルターを適用します。この記事では、追加された演算子を合成データで試し、検索条件の書き方と返った結果を紹介します。
検証環境
- リージョン:東京(ap-northeast-1)
- AWS CLI:2.42.1
- テーブル:InlineFilterProducts(オンデマンド)
- テーブルのキー:ProductId(String, HASH)
- ベクトルインデックス:ProductFilterIndex(4 次元、COSINE)
- インデックスのパーティションキー(SearchSchema の HASH):Category
- データ:合成データ 36 件(Category が Electronics 24 件、Apparel 12 件)
- Discount を持つアイテム:36 件中 12 件(Electronics では 24 件中 8 件)
- 検索対象:Category が Electronics のアイテム
テーブルとインデックスの作成
フィルターに使う属性は、インデックス作成時に SearchSchema の INLINE_FILTER として宣言します。インデックス定義を次のファイルに保存します。
[
{
"IndexName": "ProductFilterIndex",
"VectorAttribute": {"AttributeName": "Embedding"},
"SearchSchema": [
{"AttributeName": "Category", "SearchSchemaElementType": "HASH"},
{"AttributeName": "Brand", "SearchSchemaElementType": "INLINE_FILTER"},
{"AttributeName": "Genre", "SearchSchemaElementType": "INLINE_FILTER"},
{"AttributeName": "Price", "SearchSchemaElementType": "INLINE_FILTER"},
{"AttributeName": "Rating", "SearchSchemaElementType": "INLINE_FILTER"},
{"AttributeName": "PublishedDate", "SearchSchemaElementType": "INLINE_FILTER"},
{"AttributeName": "Discount", "SearchSchemaElementType": "INLINE_FILTER"}
],
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 4,
"DistanceFunction": "COSINE"
}
]
SearchSchema に書いた属性は AttributeDefinitions にも宣言します。次のコマンドでは、テーブルのキーである ProductId に加えて、SearchSchema の 7 属性を宣言しています。
aws dynamodb create-table \
--table-name InlineFilterProducts \
--attribute-definitions \
AttributeName=ProductId,AttributeType=S \
AttributeName=Category,AttributeType=S \
AttributeName=Brand,AttributeType=S \
AttributeName=Genre,AttributeType=S \
AttributeName=Price,AttributeType=N \
AttributeName=Rating,AttributeType=N \
AttributeName=PublishedDate,AttributeType=N \
AttributeName=Discount,AttributeType=N \
--key-schema AttributeName=ProductId,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--vector-indexes file://vector-indexes.json
アイテムの書き込み形式
Embedding は、数値(N)のリスト(L)として書き込みます。次は、batch-write-item に渡すファイルの PutRequest のうち、Discount を持つ P003 の 1 件です。
{
"PutRequest": {
"Item": {
"ProductId": {"S": "P003"},
"Category": {"S": "Electronics"},
"Title": {"S": "Sample product 003"},
"Brand": {"S": "CirqueFit"},
"Genre": {"S": "camera"},
"Price": {"N": "152.62"},
"Rating": {"N": "4.8"},
"PublishedDate": {"N": "20260201"},
"Discount": {"N": "10"},
"Embedding": {
"L": [
{"N": "-0.7027"},
{"N": "-0.5389"},
{"N": "-0.5799"},
{"N": "-0.9951"}
]
}
}
}
}
検索は、インデックスが ACTIVE になったことを確認してから始めます。今回は、作成開始から 21 秒後の確認で ACTIVE になっていました。
検索の基本形
クエリベクトルは次のファイルに保存します。
[{"N": "0.3"}, {"N": "-0.2"}, {"N": "0.8"}, {"N": "0.1"}]
Rating が 4.5 以上のアイテムを検索するコマンドは次のとおりです。
aws dynamodb search-vectors \
--table-name InlineFilterProducts \
--index-name ProductFilterIndex \
--search-vector file://query-vector.json \
--top-k 24 \
--search-condition-expression "Category = :c AND Rating >= :v" \
--expression-attribute-values '{":c":{"S":"Electronics"},":v":{"N":"4.5"}}' \
--projection-expression "ProductId, Brand, Genre, Price, Rating, PublishedDate, Discount"
SearchSchema で HASH に指定した Category の条件は、= で式の最上位に置き、AND でほかの条件とつなぎます。
このコマンドでは 5 件が返りました。応答の SearchResults には、型付きの Item と Score を持つ要素が並びます。先頭の 1 件を示します。
{
"Item": {
"Brand": {
"S": "AcmeRun"
},
"Discount": {
"N": "10"
},
"Genre": {
"S": "camera"
},
"Price": {
"N": "127.22"
},
"ProductId": {
"S": "P015"
},
"PublishedDate": {
"N": "20250119"
},
"Rating": {
"N": "4.6"
}
},
"Score": 0.797802209854126
}
演算子ごとの検索
検索は Category = :c AND <条件> の条件部分を変えて実行し、表には条件部分だけを示します。TopK は Electronics の全件数である 24 にしました。返った件数と ID は、いずれの条件でも手元のデータから求めた該当アイテムと一致しました。
範囲比較
範囲比較には、不等号の演算子と BETWEEN を使います。日付(PublishedDate)は yyyymmdd 形式の Number で格納しました。
| 条件 | 値 | 返った件数 | 備考 |
|---|---|---|---|
Price < :v |
50 | 6 | |
Price <= :v |
48.68 | 6 | 境界値 48.68 の P023 も含まれる |
Price > :v |
150 | 4 | |
Rating >= :v |
4.5 | 5 | 境界値 4.5 の P007 も含まれる |
Rating BETWEEN :lo AND :hi |
4.0 〜 4.5 | 7 | 両端の値を持つ P020・P011・P022(4.0)と P007(4.5)も含まれる |
PublishedDate >= :v |
20260101 | 12 |
値のリスト
IN の括弧には、値のプレースホルダーを並べます。Brand が AcmeRun または BravoStep のアイテムを検索したときの式と値は次のとおりです。8 件が返りました。
--search-condition-expression "Category = :c AND Brand IN (:b1, :b2)" \
--expression-attribute-values '{":c":{"S":"Electronics"},":b1":{"S":"AcmeRun"},":b2":{"S":"BravoStep"}}'
属性の有無
Electronics 24 件のうち Discount を持つのは 8 件で、そのうち 4 件の値が 10 です。attribute_exists / attribute_not_exists と <> を、同じデータで比べました。
| 条件 | 返った件数 | 備考 |
|---|---|---|
attribute_exists(Discount) |
8 | Discount を持つ全件 |
attribute_not_exists(Discount) |
16 | 持たない全件 |
Discount <> :d(:d は 10) |
20 | 持たない 16 件と、値が 10 以外の 4 件 |
attribute_exists(Discount) AND Discount <> :d(:d は 10) |
4 | 値が 10 以外の 4 件 |
<> は Discount を持たないアイテムにも一致したため、attribute_exists を AND で加えて除外しました。
条件の組み合わせ
OR は括弧で囲み、Category の条件と AND でつなぎました。表の最後の 2 行は、発表の小売の例(価格帯とブランド群)と RAG の例(カテゴリ群と公開日の条件)に当たる式です。RAG の例では、HASH に指定した Category には IN を使えないため、Category の代わりに Genre で絞り込みました。
| 条件 | 値 | 返った件数 | 備考 |
|---|---|---|---|
(Brand = :b1 OR Brand = :b2) |
AcmeRun, DeltaGear | 11 | |
NOT Brand = :b |
CirqueFit | 14 | |
(Rating >= :r OR Price < :p) |
4.5 / 20 | 8 | |
Price BETWEEN :lo AND :hi AND Brand IN (:b1, :b2, :b3) |
50 〜 130 / AcmeRun, DeltaGear, CirqueFit | 11 | 発表の小売の例 |
Genre IN (:g1, :g2) AND PublishedDate >= :d |
audio, camera / 20260101 | 7 | 発表の RAG の例 |
制約と上限
演算子ごとに使える属性の型、HASH に指定した属性の条件、インラインフィルター属性の上限は、SearchConditionExpression のリファレンスとクォータのページに記載されています。
まとめ
DynamoDB ベクトル検索のインラインフィルターでは、従来の等価(=)による絞り込みに加えて、範囲比較、IN、属性の有無、OR / NOT を SearchConditionExpression で使えることを確認できました。
DynamoDB ベクトル検索の結果をアプリケーション側でフィルターしている場合は、今回追加された演算子を使ったインラインフィルターをお試しください。








