前言
在es檢索時為了實現searchAfter,需要根據指定字段排序以實現正確的滾動
實際使用中,進行檢索時使用的_id作排序,因為_id天然的唯一性可以實現准確滾動。結果發現filedData過高報警。
查閱官網,建議不要直接使用_id進行排序,而是將_id寫入一個單獨且啟用doc_values的字段作排序。
官網原文:
The _id
field is restricted from use in aggregations, sorting, and scripting. In case sorting or aggregating on the _id
field is required, it is advised to duplicate the content of the _id
field into another field that has doc_values
enabled.
原文鏈接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html
原理:
filedData,在搜索時動態創建使用的jvm heap。缺點:當索引的文檔數上千萬后很容易導致內存占用很高,甚至直接OOM。優點:速度快。適用:text
doc_values,會在寫入索引時就會創建在硬盤。優點:不會大量占用內存。缺點:速度相對較慢。適用:inteter/long/fload/double/date/text
使用_id排序默認用filedData。
優化方案:
將排序字段改為其他doc_values的字段。filedData降回正常