データ オブジェクトのコレクションのクエリ

Query API の目的は、フィルタを使用してコレクションからデータ オブジェクトを取得することです。これは、データベース テーブルをクエリして SQL WHERE 句を使用するのと同様です。集計を使用して、フィルタに一致するデータ オブジェクトの数を取得することもできます。

フィルタ式の言語

Vector Search 2.0 は、KNN / ANN 検索機能に加えて、カスタムクエリ言語を使用した汎用性の高いクエリ機能を提供します。クエリ言語については、次の表をご覧ください。

フィルタ 説明 サポートされるタイプ
$eq フィールド値が指定された値と等しいデータ オブジェクトを照合します。 数値、文字列、ブール値 {"genre": {"$eq": "documentary"}}
$ne フィールド値が指定された値と等しくないデータ オブジェクトを照合します。 数値、文字列、ブール値 {"genre": {"$ne": "drama"}}
$gt フィールド値が指定された値より大きいデータ オブジェクトを照合します。 数値 {"year": {"$gt": 2019}}
$gte 指定した値以上のフィールド値を持つデータ オブジェクトを照合します。 数値 {"year": {"$gte": 2020}}
$lt フィールド値が指定した値より小さいデータ オブジェクトを照合します。 数値 {"year": {"$lt": 2020}}
$lte フィールド値が指定された値以下であるデータ オブジェクトを照合します。 数値 {"year": {"$lte": 2020}}
$in 指定された配列に含まれるフィールド値を持つデータ オブジェクトを照合します。 文字列 {"genre": {"$in": ["comedy", "documentary"]}}
$nin 指定された配列に含まれないフィールド値を持つデータ オブジェクトを照合します。 文字列 {"genre": {"$nin": ["comedy", "documentary"]}}
$and クエリ句を論理 AND で結合します。 - {"$and": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]}
$or クエリ句を論理 OR で結合します。 - {"$or": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]}
$all フィールドの配列値に指定された値がすべて含まれているドキュメントを選択します。 - {"colors": {"$all": ["red", "blue"]}}

コレクションのクエリ

次の例は、フィルタを使用して ID が COLLECTION_ID のコレクション内のデータ オブジェクトをクエリする方法を示しています。

REST

リクエストのデータを使用する前に、 次のように置き換えます。

  • COLLECTION_ID: コレクションの ID。
  • LOCATION: Agent Platform を使用しているリージョン。
  • PROJECT_ID: 実際の Google Cloud プロジェクト ID

HTTP メソッドと URL:

POST https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:query

リクエストの本文(JSON):

{
  "page_size": 10,
  "page_token": "",
  "filter": {
    "$or": [
      {
        "director": {
          "$eq": "Akira Kurosawa"
        }
      },
      {
        "$and": [
          {
            "director": {
              "$eq": "David Fincher"
            }
          },
          {
            "genre": {
              "$ne": "Thriller"
            }
          }
        ]
      }
    ]
  },
  "output_fields": {
    "data_fields": "*",
    "vector_fields": "*",
    "metadata_fields": "*"
  }
}

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

{
  "dataObjects": [
    {
      "name": "projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/1",
      "createTime": "2026-02-04T14:35:29Z",
      "updateTime": "2026-02-04T14:37:29Z",
      "data": {
        "title": "Seven Samurai",
        "director": "Akira Kurosawa",
        "genre": "Action",
        "year": 1954
      },
      "vectors": {
        "genre_embedding": {
          "dense": {
            "values": [
              0.3863801,
              0.73934346,
              0.16189057,
              0.5271367
            ]
          }
        },
        "sparse_embedding": {
          "sparse": {
            "values": [
              1,
              6,
              3,
              2,
              8,
              5,
              2
            ],
            "indices": [
              4065,
              13326,
              17377,
              25918,
              28105,
              32683,
              42998
            ]
          }
        },
        "plot_embedding": {
          "dense": {
            "values": [
              1,
              1,
              1
            ]
          }
        },
        "soundtrack_embedding": {
          "dense": {
            "values": [
              0.5920452,
              0.08301644,
              0.12647335,
              0.619643,
              0.49258286
            ]
          }
        }
      }
    },
    {
      "name": "projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/2",
      "createTime": "2026-02-04T15:35:29Z",
      "updateTime": "2026-02-04T15:37:29Z",
      "data": {
        "title": "The Social Network",
        "director": "David Fincher",
        "genre": "Drama",
        "year": 2010
      },
      "vectors": {
        "genre_embedding": {
          "dense": {
            "values": [
              0.1,
              0.2,
              0.3,
              0.4
            ]
          }
        },
        "sparse_embedding": {
          "sparse": {
            "values": [
              1
            ],
            "indices": [
              1000
            ]
          }
        },
        "plot_embedding": {
          "dense": {
            "values": [
              0.1,
              0.1,
              0.1
            ]
          }
        },
        "soundtrack_embedding": {
          "dense": {
            "values": [
              0.1,
              0.2,
              0.3,
              0.4,
              0.5
            ]
          }
        }
      }
    }
  ]
}

gcloud

後述のコマンドデータを使用する前に、 次のように置き換えます。

  • COLLECTION_ID: コレクションの ID。
  • LOCATION: Agent Platform を使用しているリージョン。
  • PROJECT_ID: 実際の Google Cloud プロジェクト ID

次のコマンドを実行します。

Linux、macOS、Cloud Shell

gcloud beta vector-search collections data-objects query \
  --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' \
  --output-data-fields='*' \
  --output-vector-fields='*' \
  --output-metadata-fields='*' \
  --collection=COLLECTION_ID \
  --location=LOCATION \
  --project=PROJECT_ID

Windows(PowerShell)

gcloud beta vector-search collections data-objects query `
  --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' `
  --output-data-fields='*' `
  --output-vector-fields='*' `
  --output-metadata-fields='*' `
  --collection=COLLECTION_ID `
  --location=LOCATION `
  --project=PROJECT_ID

Windows(cmd.exe)

gcloud beta vector-search collections data-objects query ^
  --json-filter='{"$or": [{"director": {"$eq": "Akira Kurosawa"}},{"$and": [{"director": {"$eq": "David Fincher"}},{"genre": {"$ne": "Thriller"}}]}]}' ^
  --output-data-fields='*' ^
  --output-vector-fields='*' ^
  --output-metadata-fields='*' ^
  --collection=COLLECTION_ID ^
  --location=LOCATION ^
  --project=PROJECT_ID

次のようなレスポンスが返されます。

---
createTime: '2026-02-04T14:35:29Z'
data:
  director: Akira Kurosawa
  genre: Action
  title: Seven Samurai
  year: 1954
name: projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/1
updateTime: '2026-02-04T14:37:29Z'
vectors:
  genre_embedding:
    dense:
      values:
      - 0.38638
      - 0.739343
      - 0.161891
      - 0.527137
  plot_embedding:
    dense:
      values:
      - 1.0
      - 1.0
      - 1.0
  soundtrack_embedding:
    dense:
      values:
      - 0.592045
      - 0.0830164
      - 0.126473
      - 0.619643
      - 0.492583
  sparse_embedding:
    sparse:
      indices:
      - 4065
      - 13326
      - 17377
      - 25918
      - 28105
      - 32683
      - 42998
      values:
      - 1.0
      - 6.0
      - 3.0
      - 2.0
      - 8.0
      - 5.0
      - 2.0
---
createTime: '2026-02-04T15:35:29Z'
data:
  director: David Fincher
  genre: Drama
  title: The Social Network
  year: 2010
name: projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects/2
updateTime: '2026-02-04T15:37:29Z'
vectors:
  genre_embedding:
    dense:
      values:
      - 0.1
      - 0.2
      - 0.3
      - 0.4
  plot_embedding:
    dense:
      values:
      - 0.1
      - 0.1
      - 0.1
  soundtrack_embedding:
    dense:
      values:
      - 0.1
      - 0.2
      - 0.3
      - 0.4
      - 0.5
  sparse_embedding:
    sparse:
      indices:
      - 1000
      values:
      - 1.0

Python

from google.cloud import vectorsearch_v1beta

# Create the client
data_object_search_service_client = vectorsearch_v1beta.DataObjectSearchServiceClient()

# Initialize request
request = vectorsearch_v1beta.QueryDataObjectsRequest(
    parent="projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID",
    filter={
        "$or": [
            {"director": {"$eq": "Akira Kurosawa"}},
            {
                "$and": [
                    {"director": {"$eq": "David Fincher"}},
                    {"genre": {"$ne": "Thriller"}},
                ]
            },
        ]
    },
)

# Make the request
page_result = data_object_search_service_client.query_data_objects(request=request)

# Handle the response
for response in page_result:
    print(response)

集計を行うには、aggregate エンドポイントを使用し、リクエストの本文で集計のタイプを指定します。

次の例は、ID が COLLECTION_ID のコレクション内のすべてのデータ オブジェクトをカウントする方法を示しています。

REST

リクエストのデータを使用する前に、 次のように置き換えます。

  • COLLECTION_ID: コレクションの ID。
  • LOCATION: Agent Platform を使用しているリージョン。
  • PROJECT_ID: 実際の Google Cloud プロジェクト ID

HTTP メソッドと URL:

POST https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:aggregate

リクエストの本文(JSON):

{
  "aggregate": "count"
}

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

{
  "aggregateResults": [
    {
      "count": 1000
    }
  ]
}

gcloud

後述のコマンドデータを使用する前に、 次のように置き換えます。

  • COLLECTION_ID: コレクションの ID。
  • LOCATION: Agent Platform を使用しているリージョン。
  • PROJECT_ID: 実際の Google Cloud プロジェクト ID

次のコマンドを実行します。

Linux、macOS、Cloud Shell

gcloud beta vector-search collections data-objects aggregate \
  --aggregation-method=count \
  --collection=COLLECTION_ID \
  --location=LOCATION \
  --project=PROJECT_ID

Windows(PowerShell)

gcloud beta vector-search collections data-objects aggregate `
  --aggregation-method=count `
  --collection=COLLECTION_ID `
  --location=LOCATION `
  --project=PROJECT_ID

Windows(cmd.exe)

gcloud beta vector-search collections data-objects aggregate ^
  --aggregation-method=count ^
  --collection=COLLECTION_ID ^
  --location=LOCATION ^
  --project=PROJECT_ID

次のようなレスポンスが返されます。

aggregateResults:
- count: 1000

Python

from google.cloud import vectorsearch_v1beta

# Create the client
data_object_search_service_client = vectorsearch_v1beta.DataObjectSearchServiceClient()

# Initialize request
request = vectorsearch_v1beta.AggregateDataObjectsRequest(
    parent="projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID",
    aggregate="COUNT",
)

# Make the request
response = data_object_search_service_client.aggregate_data_objects(request=request)

# Handle the response
print(response)

次のステップ