查詢資料物件的集合

Query API 的用途是使用篩選器從集合擷取資料物件。這與查詢資料庫資料表並使用 SQL WHERE 子句類似。您也可以使用匯總功能,取得符合篩選條件的資料物件計數。

篩選運算式語言

除了 KNN/ANN 搜尋功能,Vector Search 2.0 也提供多種查詢功能,可使用自訂查詢語言。下表說明查詢語言。

篩選器 說明 支援的類型 範例
$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": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]}
$or 以邏輯 OR 聯結查詢子句。 - {"$or": [{"genre": {"$eq": "drama"}}, {"year": {"$gte": 2020}}]}
$all 選取欄位陣列值包含所有指定值的文件。 - {"colors": {"$all": ["red", "blue"]}}

查詢集合

以下範例說明如何使用篩選條件,在 ID 為 COLLECTION_ID 的集合中查詢資料物件。

REST

# Query Data Objects
curl -X POST \
'https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:query' \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H 'Content-Type: application/json' \
  -d '{
    "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": "*"
    }
  }'

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

curl -X POST \
'https://vectorsearch.googleapis.com/v1beta/projects/PROJECT_ID/locations/LOCATION/collections/COLLECTION_ID/dataObjects:aggregate' \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H 'Content-Type: application/json' \
  -d '{
    "aggregate": "count"
  }'

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)

後續步驟