使用分面搜尋

    在 Meilisearch 中,分面是一種特殊的篩選器。本指南將說明如何設定分面,以及如何在搜尋書籍資料庫時使用它們。它還會教您如何取得

    需求

    設定篩選索引設定

    首先,使用這個 書籍資料集 建立一個新的索引。此資料集中的文件包含以下欄位

    {
      "id": 5,
      "title": "Hard Times",
      "genres": ["Classics","Fiction", "Victorian", "Literature"],
      "publisher": "Penguin Classics",
      "language": "English",
      "author": "Charles Dickens",
      "description":"Hard Times is a novel of social […] ",
      "format": "Hardcover",
      "rating": 3
    }
    

    接著,將 genreslanguagerating 加入 filterableAttributes 的清單中

    curl \
      -X PUT 'https://127.0.0.1:7700/indexes/books/settings/filterable-attributes' \
      -H 'Content-Type: application/json' \
      --data-binary '[
        "genres", "rating", "language"
      ]'

    您現在已設定您的索引使用這些屬性作為篩選器。

    在搜尋查詢中使用篩選

    設定 facets 搜尋參數進行搜尋查詢

    curl \
      -X POST 'https://127.0.0.1:7700/indexes/books/search' \
      -H 'Content-Type: application/json' \
      --data-binary '{
        "q": "classic",
        "facets": [
        "genres", "rating", "language"
      ]
    }'

    回應會傳回所有符合查詢的書籍。它還會傳回兩個可用於建立分面搜尋介面的欄位,facetDistributionfacetStats

    {
      "hits":[],"facetDistribution":{
        "genres":{
          "Classics":6,},
        "language":{
          "English":6,
          "French":1,
          "Spanish":1
        },
        "rating":{
          "2.5":1,}
      },
      "facetStats":{
        "rating":{
          "min":2.5,
          "max":4.7
        }
      }
    }
    

    facetDistribution 列出搜尋結果中存在的所有分面,以及每個分面傳回的文件數量。

    facetStats 包含所有包含數值的分面的最高值和最低值。

    排序分面值

    預設情況下,所有分面值都以字母數字升序排序。您可以使用 faceting 索引設定sortFacetValuesBy 屬性來變更此設定

    curl \
      -X PATCH 'https://127.0.0.1:7700/indexes/books/settings/faceting' \
      -H 'Content-Type: application/json' \
      --data-binary '{
        "sortFacetValuesBy": {
          "genres": "count"
      }
    }'

    上面的程式碼範例依遞減的值計數排序 genres 分面。

    使用新的設定重複先前的查詢將導致 facetsDistribution 中出現不同的順序

    {"facetDistribution": {
        "genres": {
          "Fiction": 8,
          "Literature": 7,
          "Classics": 6,
          "Novel": 2,
          "Horror": 2,
          "Fantasy": 2,
          "Victorian": 2,
          "Vampires": 1,
          "Tragedy": 1,
          "Satire": 1,
          "Romance": 1,
          "Historical Fiction": 1,
          "Coming-of-Age": 1,
          "Comedy": 1
        },}
    }
    

    搜尋分面值

    您也可以使用 分面搜尋端點 搜尋分面值

    curl \
      -X POST 'https://127.0.0.1:7700/indexes/books/facet-search' \
      -H 'Content-Type: application/json' \
      --data-binary '{
        "facetQuery": "c",
        "facetName": "genres"
    }'

    以下程式碼範例搜尋以 c 開頭的 genres 分面值

    回應包含一個 facetHits 陣列,其中列出所有符合的分面,以及包含該分面的文件總數

    {"facetHits": [
        {
          "value": "Children's Literature",
          "count": 1
        },
        {
          "value": "Classics",
          "count": 6
        },
        {
          "value": "Comedy",
          "count": 2
        },
        {
          "value": "Coming-of-Age",
          "count": 1
        }
      ],
      "facetQuery": "c",}
    

    您可以使用 qfiltermatchingStrategy 參數進一步精簡結果。在 API 參考資料中了解更多關於它們的資訊。