Elasticsearch 結構化搜索、keyword、Term查詢

前言

Elasticsearch 中的結構化搜索,即面向數值、日期、時間、布爾等類型數據的搜索,這些數據類型格式精確,通常使用基於詞項的term精確匹配或者prefix前綴匹配。本文還將新版本的「text」,「keyword」進行說明,還有Term查詢。

結構化搜索

結構化搜索(Structured search) 是指對結構化的數據進行搜索。比如日期、時間和數字都是結構化的,它們有精確的格式,我們可以對這些格式進行邏輯操作。比較常見的操作包括比較數字或時間的範圍、判定兩個值的大小、前綴匹配等。

文本也可以是結構化的。如彩色筆可以有離散的顏色集合: 紅(red) 、 綠(green) 、 藍(blue) 。一個博客可能被標記了關鍵詞 分佈式(distributed) 和 搜索(search) 。電商網站上的商品都有 UPCs(通用產品碼 Universal Product Codes)或其他的唯一標識,它們都需要遵從嚴格規定的、結構化的格式。

在結構化查詢中,我們得到的結果只有「是」或「否」兩個值,可以根據場景需要,決定結構化搜索是否需要打分,但通常我們是不需要打分的。

精確值查找

讓我們以下面的例子開始介紹,創建並索引一些表示產品的文檔,文檔里有字段 priceproductIDshowcreatedAttags價格產品ID是否展示創建時間打標信息

POST products/_doc/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3", "show":true, "createdAt":"2021-03-03", "tags":"abc" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5", "show":true, "createdAt":"2021-03-04" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7", "show":false, "createdAt":"2021-03-05"}
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8", "show":true, "createdAt":"2021-03-06"}

數字

現在我們想要做的是查找具有某個價格的所有產品,假設我們要獲取價格是20元的商品,我們可以使用 term 查詢,如下

GET products/_search
{
  "query": {
    "term": {
      "price": 20
    }
  }
}

通常查找一個精確值的時候,我們不希望對查詢進行評分計算。只希望對文檔進行包括或排除的計算,所以我們會使用 constant_score 查詢以非評分模式來執行 term 查詢並以1.0作為統一評分。

最終組合的結果是一個 constant_score 查詢,它包含一個 term 查詢:

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}

對於數字,一般還有範圍查詢

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

range 支持的選項

  • gt: > 大於(greater than)
  • lt: < 小於(less than)
  • gte: >= 大於或等於(greater than or equal to)
  • lte: <= 小於或等於(less than or equal to)

布爾值

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "show": true
        }
      }
    }
  }
}

日期

搜索一定時間範圍內的文檔

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createdAt": {
            "gte": "now-9d"
          }
        }
      }
    }
  }
}

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createdAt": {
            "gte": "2021-01-05"
          }
        }
      }
    }
  }
}

日期匹配表達式

  • y 年
  • M 月
  • w 周
  • d 天
  • H/h 小時
  • m 分鐘
  • s 秒

文本

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "productID.keyword": [
            "XHDK-A-1293-#fJ3",
            "KDKE-B-9947-#kL5"
          ]
        }
      }
    }
  }
}

「productID.keyword」中的「keyword」不是關鍵字,而是Elasticsearch在插入文檔的時候,自動為「productID」生成的子字段,名字是「keyword」。

null 處理

存在用「exists」,不存在用「must_not」搭配「exists」

// 存在「tags」字段
POST products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists": {
                    "field":"tags"
                }
            }
        }
    }
}

// 不存在「tags」字段,老版本用「missing」關鍵字,現在已經廢除了
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "tags"
            }
          }
        }
      }
    }
  }
}

注意,新版本不要再使用「missing」關鍵字,現在已經廢除了,用「must_not」做取反。
使用「missing」會報錯,報錯信息如下:

"reason": "no [query] registered for [missing]"

keyword

在2.x版本裏面文本使用的是string字段。
5.0之後,把string字段設置為了過時字段,引入text與keyword字段,這兩個字段都可以存儲字符串使用。

「text」用於全文搜索,「keyword」用於結構化搜索。「keyword」類似Java中的枚舉。在新版本中,如果沒有自己創建mapping,那麼在文本的處理中,會把文本自動映射為「text」,同時會生成一個子字段「keyword」,類型是「keyword」。

在存儲上,「text」會被分詞器進行分詞,而「keyword」會被原樣保留。比如「Rabit is jumping」,「text」的情況下可能被存儲為「rabit」,「jump」,而「keyword」情況下就會存儲為「Rabit is jumping」。

Term查詢

在ES中,term查詢,對輸入不做分詞,會將輸入作為一個整體,在倒排索引中查找精確的詞項,並且使用相關性算分公式為每個包含該詞項的文檔進行相關度算分。

比如上面的(”productID”: “QQPX-R-3956-#aD8″),會被分詞為「qqpx」,「r」,「3956」,「ad8」。

「productID.keyword」的類型是keyword,所以即使使用match查詢,最終也會變成Term查詢。

// "productID.keyword": "qqpx-r-3956-#ad8" 沒搜索出數據,其他都有
GET products/_search
{
  "query": {
    "match": {
      //"productID": "QQPX-R-3956-#aD8"
      //"productID": "qqpx"
      //"productID": "qqpx-r-3956-#ad8"
      //"productID.keyword": "QQPX-R-3956-#aD8"
      "productID.keyword": "qqpx-r-3956-#ad8"
    }
  }
}

// "productID": "qqpx" 與 "productID.keyword": "QQPX-R-3956-#aD8" 可以搜索出數據,其他不行
GET products/_search
{
  "query": {
    "term": {
      "productID": "QQPX-R-3956-#aD8"
      //"productID": "qqpx"
      //"productID": "qqpx-r-3956-#ad8"
      //"productID.keyword": "QQPX-R-3956-#aD8"
      //"productID.keyword": "qqpx-r-3956-#ad8"
    }
  }
}

資料