ES elasticsearch中的ignore_above、keyword、text限制及區別


在業務系統中,遇到過兩個問題:

問題1:設置為keyword類型的字段,插入很長的大段內容后,報字符超出異常,無法插入。

問題2:檢索超過ignore_above設定長度的字段后,無法返回結果。

思考:Elasticsearch單字段支持的最大字符數?

本文是基於設置ignore_above之后引申的問題展開討論與思考。

01

ignore_above的作用?

ES中用於設置超過設定字符后,不被索引或者存儲。

Strings longer than the ignore_above setting will not be indexed or stored.

02

ignore_above用法

PUT ali_test

{

  "mappings": {

    "ali_type": {

        "properties": {

               "url": {

              "type":"keyword",

               "ignore_above":256

            },

               "url_long": {

              "type":"keyword"

            },

             "url_long_long": {

              "type":"keyword",

               "ignore_above":32766

            }

        }

    }

  }

}

03

當字符超過給定長度后,能否存入?

驗證表名,對於以上mapping中設置的url,url_long,url_long_long3個字段。超過256字符的url,都可以存入。

3.1 keyword類型,普通長度驗證

插入url長度為:1705個字符,如下所示:

post ali_test/ali_type/1

{

  "url" : "1705個字符的url"

}

url參考地址:http://t.cn/zH6FHG7

檢索:

GET ali_test/ali_type/_search

{

  "query": {

    "term": {

"url" : "1705個字符的url"

}

}

}

 

返回結果:

{

  "took": 1,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 0,

    "max_score": null,

    "hits": []

  }

}

結論:

1705個字符,url、url_long、url_long_long都可以存入,可以通過head插件查看結果。

但是url term檢索無法檢索返回結果,原因: url字段設置了"ignore_above":256,導致超出256個字符后不被索引。

 

圖片

3.2 對於keyword類型,臨界長度驗證

post 32767個字符的文檔,報錯如下:

{
    "error":{
        "root_cause":[
            {

                "type":"illegal_argument_exception",

                "reason":"Document contains at least one immense term in field="url_long" (whose UTF8 encoding is longer than the max length 32766), all of which were skipped.  Please correct the analyzer to not produce such terms.  The prefix of the first immense term is: '[104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 47, 115, 101, 97, 114, 99, 104, 63, 104]...', original message: bytes can be at most 32766 in length; got 32767"

            }
        ],
        "caused_by":{

            "type":"max_bytes_length_exceeded_exception",

            "reason":"max_bytes_length_exceeded_exception: bytes can be at most 32766 in length; got 32767"

        }
    },
    "status":400

}

post 32766個字符后,能提交成功,返回結果如下:

{

  "_index": "ali_test",

  "_type": "ali_type",

  "_id": "2000",

  "_version": 1,

  "result": "created",

  "_shards": {

    "total": 2,

    "successful": 2,

    "failed": 0

  },

  "created": true

}

 

結論:keyword類型的最大支持的長度為——32766個UTF-8類型的字符。

也就是說term精確匹配的最大支持的長度為32766個UTF-8個字符。

04

text類型和keyword類型的存儲字符數區別?

text類型:支持分詞、全文檢索,不支持聚合、排序操作。適合大字段存儲,如:文章詳情、content字段等;

keyword類型:支持精確匹配,支持聚合、排序操作。適合精准字段匹配,如:url、name、title等字段。

一般情況,text和keyword共存,設置mapping如下:

{

  "mappings": {

    "ali_type": {

        "properties": {

            "title_v1": {

                 "analyzer":"ik_max_word",

                    "type":"text",

                 "term_vector" : "with_positions_offsets",

                    "fields":{

                        "keyword":{

                            "ignore_above":256,

                            "type":"keyword"

                        }

                    }

            }

        }

    }

  }

}

05

小結

 

1)ES5.X版本以后,keyword支持的最大長度為32766個UTF-8字符,text對字符長度沒有限制。

2)設置ignore_above后,超過給定長度后的數據將不被索引,無法通過term精確匹配檢索返回結果。

轉載只 銘毅天下 公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM