天天看點

elasticsearch中的keyword設定ignore_above

概念

我們在ElasticSearch的官方文檔上可以看到這樣關于mapping中ignore_above的解釋:

Strings longer than the ignore_above setting will not be indexed or stored. For arrays of strings, ignore_above will be applied for each array element separately and string elements longer than ignore_above will not be indexed or stored.

建立 mapping 時,可以為字元串(專指 keyword) 指定 ​

​ignore_above​

​​ ,用來限定字元長度。超過 ​

​ignore_above​

​ 的字元會被存儲,但不會被索引。

注意,是字元長度,一個英文字母是一個字元,一個漢字也是一個字元。

在動态生成的 mapping 中,​

​keyword​

​​類型會被設定​

​ignore_above: 256​

​。

示例

這裡,在特此說明一下,如果keyword字段的ignore_above設定上限超過給定的值,比如是20,那麼當存儲一個字段超過20個字元時,會怎麼樣呢。

PUT my_index

{

 "mappings": {

     "properties": {

       "message": {

         "type": "keyword",

         "store": true,

         "ignore_above": 20              

       }

     }

 }

}

添加測試資料

PUT _bulk

{"index":{"_index":"my_index","_id":"1"}}

{"message":"123456789"}

{"index":{"_index":"my_index","_id":"2"}}

{"message":"12345678901234567890"}

{"index":{"_index":"my_index","_id":"3"}}

{"message":"12345678901234567890123"}

驗證

GET my_index/_search

結果

{

 "took" : 0,

 "timed_out" : false,

 "_shards" : {

   "total" : 1,

   "successful" : 1,

   "skipped" : 0,

   "failed" : 0

 },

 "hits" : {

   "total" : {

     "value" : 3,

     "relation" : "eq"

   },

   "max_score" : 1.0,

   "hits" : [

     {

       "_index" : "my_index",

       "_type" : "_doc",

       "_id" : "1",

       "_score" : 1.0,

       "_source" : {

         "message" : "123456789"

       }

     },

     {

       "_index" : "my_index",

       "_type" : "_doc",

       "_id" : "2",

       "_score" : 1.0,

       "_source" : {

         "message" : "12345678901234567890"

       }

     },

     {

       "_index" : "my_index",

       "_type" : "_doc",

       "_id" : "3",

       "_score" : 1.0,

       "_source" : {

         "message" : "12345678901234567890123"

       }

     }

   ]

 }

}

驗證我們可以發現,超過20個字元的資料是可以被存儲的。

再來驗證搜尋是否可以被搜尋到

GET my_index/_search

{

 "query":{

   "match":{

     "message":"123456789"

   }

 }

}

GET my_index/_search

{

 "query":{

   "match":{

     "message":"12345678901234567890123"

   }

 }

}

驗證發現,20個以内的資料在_source中存在,20個以上的字元時不會被檢索到的。

可以通過下面的方式修改ignore_above

PUT my_index

{

 "mappings": {

     "properties": {

       "message": {

         "type": "keyword",

         "store": true,

         "ignore_above": 10              

       }

     }

 }

}

改大改小都行,但隻對新資料有效。

注意:text 類型不支援 ignore_above

總結

1. 超過ignore_above時檢索是查不到的

2. 但是資料都是完整存儲在source中的

3. ES5.X版本以後,keyword支援的最大長度為32766個UTF-8字元,text對字元長度沒有限制。這個選項在保護 Lucene 的項的位元組長度限制 32766 發揮作用。