iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
1
Elastic Stack on Cloud

Elastic Stack武學練功坊系列 第 8

Elastic Stack第八重

Searching Part II(搜尋)

延續上一篇,介紹其他搜尋的應用
此篇的指令以 kibana 操作為主,個人覺得比較方便好用且易上手


搜尋 (Search)

前置作業
後續範例皆使用 Elastic Stack第七重 匯入的範例資料,所以還未匯入可以先照那一重做批次匯入

bool query

es提供了bool query來建立更複雜的查詢條件,
主要分成4種:

  • must: 必須要有
  • should: 可有可無
  • must_not: 一定不會有
  • filter: 一定會有

不多說,用範例來解釋會更清楚

[情境] 找出帳戶的擁有者是40歲,但排除住在 Idaho(ID) 的人

Request (kibana)

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "age": "40"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "state": "ID"
          }
        }
      ]
    }
  }
}

Response
Bool Response

es把 Boolean query中 must, should, must_not, and filter 內的每個條件(object)稱為 query clause
以上述情境的Request為例,

{
  "match": {
    "age": "40"
  }
}

即為 query clause

其中 mustshould 內的匹配條件是會影響 relevance score(相關分數)
must_notfilter 一樣,只是做篩選的動作,即 document是否會包含在results內 ,不會影響 relevance score(相關分數)
如前一篇有提到, es預設使用 relevance score(相關分數) 排序匹配的documents

[情境] 找出收支平衡在 $20,000(含) 和 $30,000(含) 之間的帳戶

Request (kibana)

GET /bank/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

Response
Bool Response

es的 bool query 真的很多奧妙之處,
下一篇會再延續介紹 query,以及一些實際應用


小小新手,如有理解錯誤或寫錯再請不吝提醒或糾正


Reference

Start searching
Boolean query


上一篇
Elastic Stack第七重
下一篇
Elastic Stack第九重
系列文
Elastic Stack武學練功坊30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言