ElasticSearch7.X學習四--數據類型--keyword datatype

官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html

概念

keyword類型是索引ID,郵箱地址,hostnames,zip codes,tags,狀態碼(status codes)等結構化內容。通常是在查詢中用來“過濾”,“排序”,“聚合”等操作,並且當keyword類型字段作爲查詢條件時,是需要精確查找的。如果是需要對多文本查詢的話還是使用text類型。
對於numberic類型文檔中提到如果是明確需要用到範圍查詢(range query)的話那還是使用numberic,如果不需要使用範圍查詢的話就使用keyword,因爲term和term-level查詢對於keywrod類型來說查詢是很高效的。如果不確定是否使用range查詢文檔中還提到可以使用多類型,如下定義:

  "state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }

多類型,不限於numberic,爲了查詢效率高,text類型也可以,只是我認爲多類型肯定會佔用更多的內存和存儲。上面 "ignore_above"的含義是“state”字段長度不超過256,單位應該是字節,最大值是2147483647。

查詢

查詢過程中如果是keyword類型的字段,需要將這個keyword加上,不加和加前後對比:

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "bool": {"must":{"term":{"state":"KS"}}} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 0,
  "size": 10
}
'

結果是沒有數據hit到。加上後:

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "bool": {"must":{"term":{"state.keyword":"KS"}}} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 0,
  "size": 10
}
'

結果是:

  {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "534",
        "_score" : null,
        "_source" : {
          "account_number" : 534,
          "balance" : 20470,
          "firstname" : "Cristina",
          "lastname" : "Russo",
          "age" : 25,
          "gender" : "F",
          "address" : "500 Highlawn Avenue",
          "employer" : "Cyclonica",
          "email" : "[email protected]",
          "city" : "Gorst",
          "state" : "KS"
        },
        "sort" : [
          534
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "561",
        "_score" : null,
        "_source" : {
          "account_number" : 561,
          "balance" : 12370,
          "firstname" : "Sellers",
          "lastname" : "Davis",
          "age" : 30,
          "gender" : "M",
          "address" : "860 Madoc Avenue",
          "employer" : "Isodrive",
          "email" : "[email protected]",
          "city" : "Trail",
          "state" : "KS"
        },
        "sort" : [
          561
        ]
      }
    ]
  }

開始學習的時候就是因爲拉下了keyword這個關鍵字導致查詢無結果集。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章