iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 22
1
Elastic Stack on Cloud

少女人妻的30天Elastic系列 第 22

【少女人妻的30天Elastic】Day 22 : Elastic App Search_Search, Facets API

  • 分享至 

  • xImage
  •  

Aloha!又是我少女人妻 Uerica~!剛剛出門遛狗,遛著遛著結果鑰匙就掉了,找了兩小時還是找不到,想到小時候也常常丟三落四,我媽常常問我怎麼不弄丟自己,嗚嗚...小時候都覺得鑰匙掉了、要死掉了拉 QQ


Search, Facets

Facets 篩選面板,有分為 Value 篩選 與 Range 篩選

使用類型需遵守以下表格

Type Value Facet Range Facet
text Yes No
number Yes Yes
date Yes Yes
geolocation No Yes

Value Facet

提供某欄位值的計數,可用於 text、number、date 三種欄位類型

  • query (required) : 要查詢的字詞
  • facets (required) : Facet 篩選的設定與定義
  • field key (required) : 需要被 Facet 篩選的欄位
  • type (required) : Facet 篩選類型,如果是計算值寫value
  • name (optional) : Facet 篩選名稱
  • size (optional) : 返回多少欄位值計數的種類,可介於 1 - 250 之間,預設為 10 個
  • sort (optional) : key為 count(計算用值出現的次數) 或value(計算用字元大小),value 為 asc(升冪) 或desc(降冪)
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
  "query": "花蓮",
  "facets": {
	//area 是 field key,要被篩選的欄位名稱
    "area": [
      {
        "type": "count",
        "name": "area",
        "sort": { "value": "desc" },
        "size": 5
      }
    ]
  }
}'

回傳結果

{
    "meta": {
        "alerts": [],
        "warnings": [],
        "page": {
            "current": 1,
            "total_pages": 4,
            "total_results": 32,
            "size": 10
        },
        "engine": {
            "name": "6gogo",
            "type": "default"
        },
        "request_id": "0737cd18-36b7-4d1f-beb3-747dce6fe6fd"
    },
    "results": [
		// Truncated!
    ],
    "facets": {
        "area": [
            {
                "type": "value",
                "name": "area",
                "data": [
                    {
                        "value": "豐濱鄉",
                        "count": 1
                    },
                    {
                        "value": "花蓮市",
                        "count": 8
                    },
                    {
                        "value": "秀林鄉",
                        "count": 6
                    },
                    {
                        "value": "新城鄉",
                        "count": 2
                    },
                    {
                        "value": "壽豐鄉",
                        "count": 8
                    }
                ]
            }
        ]
    }
}

Range Facet

回傳某範圍內的計數,在number,date,geolocation欄位類型上可用。

  • query (required) : 要查詢的字詞
  • facets (required) : Facet 篩選的設定與定義
  • field key (required) : 需要被 Facet 篩選的欄位
  • type (required) : Facet 篩選類型,如果是範圍要寫 range
  • name (optional) : Facet 篩選名稱
  • ranges (required) : range 物件的設定與定義
  • from (optional) : range的下限,from、to至少填一項
  • to (optional) : range的上限,from、to至少填一項

number類型欄位的Range Facet

curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
  "query": "park",
  "facets": {
    "acres": [
      {
        "type": "range",
        "name": "min-and-max-range",
        "ranges": [
          { "from": 1, "to": 10000 },
          { "from": 10000 }
        ]
      }
    ]
  }
}'

date 類型欄位的Range Facet

curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d '{
  "query": "park",
  "facets": {
    "date_established": [
      {
        "type": "range",
        "name": "half-century",
        "ranges": [
          { "from": "1900-01-01T12:00:00+00:00", "to": "1950-01-01T00:00:00+00:00" }
        ]
      }
    ]
  }
}'

geolocation 類型欄位的Range Facet

  • center (required) : 地理位置中心點
  • unit (required) : 地理位置單位,mm、cm、m (米)、km、in、ft、yd、mi(英里)。
curl -X POST '{API_Endpoint}/api/as/v1/engines/{ENGINE_NAME}/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-xxxxxxxxxx' \
-d'{
  "query": "parks",
  "facets": {
    "location": [
      {
        "type": "range",
        "name": "geo-range-from-san-francisco",
        "center": "37.386483, -122.083842",
        "unit": "m",
        "ranges": [
          { "from": 0, "to": 100000, "name": "Nearby" },
          { "from": 100000, "to": 300000, "name": "A longer drive." },
          { "from": 300000, "name": "Perhaps fly?" }
        ]
      }
    ]
  }
}'

今天就先到這邊拉,感謝大家閱讀~祝福我鑰匙被好心人撿到,然後好心人剛好知道我在找鑰匙阿哈哈,大家明天見啦!掰掰


上一篇
【少女人妻的30天Elastic】Day 21 : Elastic App Search_Search, Boosts API
下一篇
【少女人妻的30天Elastic】Day 23 : Elastic App Search_Search, Filters API
系列文
少女人妻的30天Elastic30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言