篩選網站搜尋結果

本頁說明如何使用網站資料篩選搜尋應用程式的搜尋查詢。

事前準備

請確認您已建立應用程式和資料儲存庫,並將網站資料擷取至資料儲存庫。詳情請參閱「建立搜尋應用程式」。

關於篩選運算式

使用篩選運算式建構網站搜尋篩選器。篩選條件的建構方式取決於是否已啟用進階網站索引。請參閱下列其中一個章節,視您使用的是基本網站搜尋或進階網站索引而定:

基本網站搜尋的篩選運算式

本節說明基本網站搜尋的篩選器運算式行為 (進階網站索引已關閉)。

基本網站搜尋的語法

以下擴充 Backus-Naur 形式總結了篩選器運算式語法,方便您在擁有基本網站搜尋功能時,建構網站搜尋篩選器。篩選器中冒號後方必須加上雙引號。

    # A single expression or multiple expressions that are joined by "AND".
  filter = expression, { "AND", expression };
  expression =
    # A simple expression applying to a text url string.
    | filter_key, ":", \"text_value\"
  filter_key = (cr | highRange | lowRange | fileType | lr | rights | siteSearch);
  text_value = string value to filter on;

基本網站搜尋可用的欄位

如果您使用基本網站搜尋功能,可以透過下列欄位篩選網站搜尋結果:

  • cr (字串):將搜尋結果限制為來自特定國家/地區的文件。如需支援的值清單,請參閱「國家/地區集合值」。

  • highRange (字串):指定搜尋範圍的上限。如果文件包含數字,該數字必須小於或等於 highRange 的值,才能納入回應。同時指定 highRangelowRange,在這些參數的範圍內建立搜尋查詢。

  • lowRange (字串):指定搜尋範圍的下限。如果文件包含數字,該數字必須大於或等於 lowRange 的值,才會納入回應。同時指定 lowRangehighRange,在這些參數的範圍內建立搜尋查詢。

  • fileType (字串):限制搜尋結果只顯示具有指定副檔名的文件。如需支援的檔案類型清單,請參閱「Google 可建立索引的檔案類型」。

  • lr (字串):限制搜尋結果只顯示以指定語言撰寫的文件。如需支援的語言清單,請參閱查詢參數 (lr)

  • rights (字串) 根據授權篩選搜尋結果。如要瞭解支援的值,請參閱查詢參數 (權利)

  • siteSearch (字串):指定查詢應搜尋的網頁網址模式。

基本網站搜尋範例

以下列舉幾個基本網站搜尋的篩選器範例:

  • {"filter": "cr:\"countryUS\" AND siteSearch:\"https://example.com/example_domain\""}

    篩選條件:(1) 來自美國,且 (2) 位於 https://example.com/example_domain 網域的文件。

  • {"filter": "fileType:\".pdf\" AND lr:\"lang_en\""}

    篩選條件:(1) PDF 檔案,以及 (2) 英文文件。

  • {"filter": "rights:\"cc_publicdomain\""}

    屬於公共領域資源的文件篩選器。

使用進階網站索引建立功能篩選運算式

本節說明進階網站索引建立功能 (已開啟) 的篩選器運算式行為。

進階網站索引建立功能可用的欄位

進階網站索引功能可讓您使用下列欄位篩選網站搜尋結果:

  • siteSearch (字串):查詢應搜尋的網頁網址模式。
  • meta 代碼名稱和 PageMap 屬性名稱:可新增至資料儲存庫結構定義的網頁結構化資料,讓欄位可供搜尋、擷取及建立索引。詳情請參閱「使用結構化資料進行進階網站索引」。

進階網站索引建立功能的語法

下列擴充 Backus-Naur 形式會摘要說明篩選器運算式語法,方便您在進階網站索引功能啟用時,建構網站搜尋篩選器。篩選器中冒號後方必須加上雙引號。

篩選 siteSearch 時,EBNF 語法如下:

    # A single expression or multiple expressions that are joined by "OR".
  filter = expression, { "OR", expression };
    # Expressions can be prefixed with "-" or "NOT" to express a negation.
  expression = [ "-" | "NOT " ],
    # A simple expression applying to a text url string.
    | filter_key, ":", \"url_string\"
  filter_key = siteSearch;
  url_string = double quoted string representing a URL;

篩選 meta 標記名稱和 PageMap 屬性名稱時,EBNF 語法為:

    # A single expression or multiple expressions that are joined by "OR".
  filter = expression, { "OR", expression };
    # Expressions can be prefixed with "-" or "NOT" to express a negation.
  expression = [ "-" | "NOT " ],
   # Function "ANY" returns true if the field exactly matches any of the literals.
    | text_field, ":", "ANY", "(", literal, { ",", literal }, ")"
  literal = double quoted string;
  # text_field corresponds to the meta tag or PageMap Attribute name, for example, category
  text_field = text field;

進階網站索引建立功能範例

以下列舉一些進階網站索引建立功能的篩選器範例:siteSearch

  • {"filter": "siteSearch:\"https://example.com/example_domain\""}

    篩選網域中的文件 https://example.com/example_domain。例如:https://example.com/example_domain/index.html

  • {"filter": "siteSearch:\"https://example.com/subdomains/*\""}

    篩選符合 https://example.com/subdomains/* 中任何網域的文件。例如:https://example.com/subdomains/example_subdomain_page

  • {"filter": "siteSearch:\"https://altostrat.com/subdomain/pages/*\" OR siteSearch:\"http://cymbalgroup.com/pages/*\""}

    篩選符合第一個或第二個網址模式的任何網域中的文件。例如 https://altostrat.com/subdomain/pages/title_pagehttps://cymbalgroup.com/subdomain/pages/title_pagehttps://altostrat.com/subdomain/pages/INFO

以下列舉 meta 或 PageMap 屬性名稱篩選器的範例:

  • {"filter": "product: ANY(\"networking\",\"compute\")"}

    篩選條件:包含 meta 標記或名為 product 的 PageMap 屬性,且值為 networkingcompute 的文件。

  • {"filter": "NOT product: ANY(\"storage\")"}

    篩選不含 meta 標記或 PageMap 屬性名稱 product (值為 storage) 的文件。

如要進一步瞭解用於建立索引的 meta 標記名稱和 PageMap 屬性名稱,請參閱「meta 標記的範例用途」和「PageMap 的範例用途」。

如要篩選網站搜尋結果,請按照下列步驟操作:

  1. 找出應用程式 ID。如果已有應用程式 ID,請跳到下一個步驟。

    1. 前往 Google Cloud 控制台的「AI Applications」頁面。

      前往「應用程式」

    2. 在「應用程式」頁面中,找出應用程式名稱,然後從「ID」欄取得應用程式的 ID。

  2. 如要篩選網站搜尋結果,請使用 filter 欄位和 engines.servingConfigs.search 方法。

    curl -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:search" \
    -d '{
     "servingConfig": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search",
    "query": "QUERY",
    "filter": "FILTER"
    }'
    

    更改下列內容: