วันนี้เจอปัญหาเรื่อง _id field ของ document ใน ElasticSearch
พบว่าในการย้ายเอกสารจาก version ที่ต่ำกว่า 5.0 มายัง version เกิน 5.0
อาจจะทำการ dump หรือ reindex ก็ได้
จะเจอ error ที่น่าสนใจคือ “id is too long, must be no longer than 512 bytes but was: 513”

คำถามคือ มีสาเหตุมาจากอะไร ?
เมื่อไปดูเอกสารใน Elasticsearch :: _id field
ก็บอกไว้ว่า ความยาวของ _id ต้องไปเกิน 512 bytes
ดังนั้นถ้ายาวเกิน 512 จะถูก reject

เมื่อไปดูใน source code ก็พบว่าทำการ hard code ไว้เลย

คำถามคือ จะแก้ไขปัญหานี้อย่างไร ?

การแก้ไขไม่ยากคือ การลบ _id ทิ้ง หรือ ทำการ กำหนด _id ให้เป็น null ก่อน
จากนั้นจึงทำการ dump หรือ reindex นั้นเอง
ยกตัวอย่างการเขียน painless script ได้เลย

Reference Websites

https://www.joshmlwood.com/elasticsearch-ids-are-hard/
https://discuss.elastic.co/t/reindexing-failed-validation-failed-1-id-is-too-long-must-be-no-longer-than-512-bytes-but-was-672/185650/4