構造化データまたはメタデータを含む非構造化データを使用する検索アプリがある場合は、メタデータを使用して検索クエリをフィルタできます。このページでは、メタデータ フィールドを使用して、検索を特定のドキュメント セットに制限する方法について説明します。
始める前に
アプリを作成して、構造化データまたはメタデータを含む非構造化データを取り込んだことを確認します。詳細については、検索アプリを作成するをご覧ください。
メタデータの例
4 つの PDF ファイル(document_1.pdf
、document_2.pdf
、document_3.pdf
、document_4.pdf
)のメタデータの例を確認します。このメタデータは、PDF ファイルとともに Cloud Storage バケット内の JSON ファイルに格納されます。このページを読む際にこの例を参照できます。
{"id": "1", "structData": {"title": "Policy on accepting corrected claims", "category": ["persona_A"]}, "content": {"mimeType": "application/pdf", "uri": "gs://bucketname_87654321/data/document_1.pdf"}}
{"id": "2", "structData": {"title": "Claims documentation and reporting guidelines for commercial members", "category": ["persona_A", "persona_B"]}, "content": {"mimeType": "application/pdf", "uri": "gs://bucketname_87654321/data/document_2.pdf"}}
{"id": "3", "structData": {"title": "Claims guidelines for bundled services and supplies for commercial members", "category": ["persona_B", "persona_C"]}, "content": {"mimeType": "application/pdf", "uri": "gs://bucketname_87654321/data/document_3.pdf"}}
{"id": "4", "structData": {"title": "Advantage claims submission guidelines", "category": ["persona_A", "persona_C"]}, "content": {"mimeType": "application/pdf", "uri": "gs://bucketname_87654321/data/document_4.pdf"}}
フィルタ式の構文
検索フィルタの定義に使用するフィルタ式の構文を理解しておきます。フィルタ式の構文は、次の Extended Backus-Naur form に要約できます。
# A single expression or multiple expressions that are joined by "AND" or "OR". filter = expression, { " AND " | "OR", expression }; # Expressions can be prefixed with "-" or "NOT" to express a negation. expression = [ "-" | "NOT " ], # A parenthetical expression. | "(", expression, ")" # A simple expression applying to a text field. # Function "ANY" returns true if the field exactly matches any of the literals. ( text_field, ":", "ANY", "(", literal, { ",", literal }, ")" # A simple expression applying to a numerical field. Function "IN" returns true # if a field value is within the range. By default, lower_bound is inclusive and # upper_bound is exclusive. | numerical_field, ":", "IN", "(", lower_bound, ",", upper_bound, ")" # A simple expression that applies to a numerical field and compares with a double value. | numerical_field, comparison, double # An expression that applies to a geolocation field with text/street/postal address. | geolocation_field, ":", "GEO_DISTANCE(", literal, ",", distance_in_meters, ")" # An expression that applies to a geolocation field with latitude and longitude. | geolocation_field, ":", "GEO_DISTANCE(", latitude_double, ",", longitude_double, ",", distance_in_meters, ")" # Datetime field | datetime_field, comparison, literal_iso_8601_datetime_format); # A lower_bound is either a double or "*", which represents negative infinity. # Explicitly specify inclusive bound with the character 'i' or exclusive bound # with the character 'e'. lower_bound = ( double, [ "e" | "i" ] ) | "*"; # An upper_bound is either a double or "*", which represents infinity. # Explicitly specify inclusive bound with the character 'i' or exclusive bound # with the character 'e'. upper_bound = ( double, [ "e" | "i" ] ) | "*"; # Supported comparison operators. comparison = "<=" | "<" | ">=" | ">" | "="; # A literal is any double quoted string. You must escape backslash (\) and # quote (") characters. literal = double quoted string; text_field = text field - for example, category; numerical_field = numerical field - for example, score; geolocation_field = field of geolocation data type - for example home_address, location; datetime_field = field of datetime data type - for example creation_date, expires_on; literal_iso_8601_datetime_format = either a double quoted string representing ISO 8601 datetime or a numerical field representing microseconds from unix epoch.
メタデータ フィルタを使用して検索する
メタデータ フィルタを使用して検索する手順は次のとおりです。
検索クエリのフィルタリングに使用するメタデータ フィールドを決定します。たとえば、始める前にのメタデータの場合、
category
フィールドを検索フィルタとして使用できます。ユーザーはpersona_A
、persona_B
、persona_C
でフィルタリングできるため、検索は関心のあるペルソナに関連付けられたドキュメントに限定されます。メタデータ フィールドをインデックス登録可能にします。
Google Cloud コンソールで、[Gemini Enterprise] ページに移動し、ナビゲーション メニューで [アプリ] をクリックします。
検索アプリをクリックします。
ナビゲーション メニューで [データ] をクリックします。
[スキーマ] タブをクリックします。このタブには、現在のフィールド設定が表示されます。
[編集] をクリックします。
インデックス登録可能にするフィールドの [インデックス登録可能] チェックボックスをオンにします。
[保存] をクリックします。詳細については、フィールド設定を構成するをご覧ください。
データストア ID を確認します。データストア ID がすでにある場合は、次のステップに進みます。
Google Cloud コンソールで、[Gemini Enterprise] ページに移動し、ナビゲーション メニューで [データストア] をクリックします。
データストアの名前をクリックします。
データストアの [データ] ページで、データストア ID を取得します。
検索結果をプレビューします。
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/servingConfigs/default_search:search" \ -d '{ "query": "QUERY", "filter": "FILTER" }'
次のように置き換えます。
PROJECT_ID
: 実際のプロジェクトの ID。DATA_STORE_ID
: データストアの ID。QUERY
: 検索するクエリテキスト。FILTER
: 省略可。フィルタ式の構文を使用して、指定されたフィールドセットでフィルタリングできるテキスト フィールド。デフォルト値は空の文字列です。これは、フィルタが適用されないことを意味します。
たとえば、始める前にで説明したメタデータを含む 4 つの PDF ファイルをインポートしたとします。「claims」という単語を含むドキュメントを検索し、
category
の値がpersona_A
のドキュメントのみをクエリするとします。そのためには、呼び出しに次のステートメントを含めます。"query": "claims", "filter": "category: ANY(\"persona_A\")"
詳細については、構造化データまたは非構造化データを含むアプリの検索結果をプレビューするの [REST] タブをご覧ください。
クリックしてレスポンスの例を表示します。
前述の手順のような検索を行うと、次のようなレスポンスが返されます。レスポンスには、
category
値がpersona_A
の 3 つのドキュメントが含まれています。{ "results": [ { "id": "2", "document": { "name": "projects/abcdefg/locations/global/collections/default_collection/dataStores/search_store_id/branches/0/documents/2", "id": "2", "structData": { "title": "Claims documentation and reporting guidelines for commercial members", "category": [ "persona_A", "persona_B" ] }, "derivedStructData": { "link": "gs://bucketname_87654321/data/document_2.pdf", "extractive_answers": [ { "pageNumber": "1", "content": "lorem ipsum" } ] } } }, { "id": "1", "document": { "name": "projects/abcdefg/locations/global/collections/default_collection/dataStores/search_store_id/branches/0/documents/1", "id": "1", "structData": { "title": "Policy on accepting corrected claims", "category": [ "persona_A" ] }, "derivedStructData": { "extractive_answers": [ { "pageNumber": "2", "content": "lorem ipsum" } ], "link": "gs://bucketname_87654321/data/document_1.pdf" } } }, { "id": "4", "document": { "name": "projects/abcdefg/locations/global/collections/default_collection/dataStores/search_store_id/branches/0/documents/4", "id": "4", "structData": { "title": "Advantage claims submission guidelines", "category": [ "persona_A", "persona_C" ] }, "derivedStructData": { "extractive_answers": [ { "pageNumber": "47", "content": "lorem ipsum" } ], "link": "gs://bucketname_87654321/data/document_4.pdf" } } } ], "totalSize": 330, "attributionToken": "UvBRCgsI26PxpQYQs7vQZRIkNjRiYWY1MTItMDAwMC0yZWIwLTg3MTAtMTQyMjNiYzYzMWEyIgdHRU5FUklDKhSOvp0VpovvF8XL8xfC8J4V1LKdFQ", "guidedSearchResult": {}, "summary": {} }
フィルタ式の例
次の表に、フィルタ式の例を示します。
フィルタ | 次の条件を満たすドキュメントの結果のみを返します。 |
---|---|
category: ANY(\"persona_A\") |
テキスト フィールド category が persona_A である |
score: IN(*, 100.0e) |
数値フィールド score が負の無限大より大きく 100.0 より小さい |
non-smoking = \"true\" |
ブール値 non-smoking が true である |
pet-friendly = \"false\" |
ブール値 pet-friendly が false である |
manufactured_date = \"2023\" |
manufactured date が 2023 年の任意の日付である |
manufactured_date >= \"2024-04-16\" |
manufactured_date が 2024 年 4 月 16 日以降である |
manufactured_date < \"2024-04-16T12:00:00-07:00\" |
manufactured_date が 2024 年 4 月 16 日の正午(太平洋夏時間)より前である |
office.location:GEO_DISTANCE(\"1600 Amphitheater Pkwy, Mountain View, CA, 94043\", 500) |
位置情報フィールド office.location が、1600 Amphitheater Pkwy から 500 m 以内にある |
NOT office.location:GEO_DISTANCE(\"Palo Alto, CA\", 1000) |
位置情報フィールド office.location が、Palo Alto, California の半径 1 km 以内にない。 |
office.location:GEO_DISTANCE(34.1829, -121.293, 500) |
位置情報フィールド office.location が、緯度 34.1829、経度 -121.293 の半径 500 m 以内にある |
category: ANY(\"persona_A\") AND score: IN(*, 100.0e) |
category が persona_A で、score が 100 未満である |
office.location:GEO_DISTANCE(\"Mountain View, CA\", 500) OR office.location:GEO_DISTANCE(\"Palo Alto, CA\", 500) |
office.location は、Mountain View または Palo Alto から 500 m 以内にある。 |
(price<175 AND pet-friendly = \"true\") OR (price<125 AND pet-friendly = \"false\") |
price が 175 未満でペットを同伴できる、または price が 125 未満でペットを同伴できない |
次のステップ
- フィルタが検索品質に与える影響を把握するために、検索品質を評価します。詳細については、検索品質を評価するをご覧ください。