01Inverted Index และ Near Real-time

ฐานข้อมูลทั่วไปมักเริ่มจาก document แล้วหาค่า field แต่ Elasticsearch สร้าง inverted index ที่จับคู่ term ไปยัง document ที่มีคำนั้น จึงค้นข้อความจำนวนมากและคำนวณ relevance ได้เร็ว ข้อมูลใน field แบบ `text` จะผ่าน analyzer เพื่อ tokenize และ normalize ก่อนเข้า index

ข้อความต้นฉบับ→Analyzer→Terms→Inverted index

การเขียนสำเร็จไม่ได้แปลว่าจะค้นเจอทันที Document จะมองเห็นโดย search หลัง refresh จึงเรียกว่า near real-time ถ้า flow ต้อง “เขียนแล้วค้นทันที” ให้ใช้ `refresh=wait_for` เฉพาะจุดที่จำเป็น แทนการบังคับ refresh ทุก request ซึ่งลด indexing throughput

อ่านเอกสารทางการ: Elastic: Refresh parameter ↗

02Mapping คือ Schema ของ Index

Mapping ระบุชนิดและวิธี index ของแต่ละ field เลือกชนิดผิดแล้ว query อาจช้า ให้ผลผิด หรือเปลี่ยนย้อนหลังไม่ได้โดยตรง งาน production ควรประกาศ mapping และ index template ให้ชัด แทนการปล่อย dynamic mapping รับ field ใหม่โดยไม่จำกัดจนเกิด mapping explosion

text

ผ่าน analyzer เหมาะกับ full-text search และ relevance

keyword

เก็บค่าเป็นหน่วยเดียว เหมาะกับ exact match, sort และ aggregation

Multi-field

เก็บค่าหนึ่ง field ได้ทั้ง `text` และ `keyword` ตามรูปแบบการใช้

index-mapping.json
PUT /policies-v1
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "policyNumber": { "type": "keyword" },
      "customerName": {
        "type": "text",
        "fields": {
          "keyword": { "type": "keyword" }
        }
      },
      "status": { "type": "keyword" },
      "createdAt": { "type": "date" },
      "premium": {
        "type": "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

`object` จะ flatten field ภายใน ส่วน array of objects ที่ต้องรักษา ความสัมพันธ์ของสมาชิกแต่ละตัวควรใช้ `nested` แต่ nested document เพิ่มต้นทุน index และ query จึงควร denormalize เท่าที่ access pattern ต้องการจริง

อ่านเอกสารทางการ: Elastic: Text field type และ multi-fields ↗

03Query, Filter และ Relevance

Query context ถามว่า document ตรงกับคำค้นดีแค่ไหน และสร้าง `_score` ส่วน filter context ตอบเพียง match หรือไม่ match จึงเหมาะกับ status, tenant, permission, date range และเงื่อนไขที่ไม่ต้องมีผลต่อคะแนน

search-query.json
GET /policies-read/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "customerName": {
              "query": "สมชาย ใจดี",
              "operator": "and"
            }
          }
        }
      ],
      "filter": [
        { "term": { "status": "ACTIVE" } },
        { "range": { "createdAt": { "gte": "now-1y" } } }
      ]
    }
  },
  "sort": ["_score", { "createdAt": "desc" }]
}
  • `match` วิเคราะห์ข้อความก่อนค้น เหมาะกับ field แบบ `text`
  • `term` เทียบ term ตรงตัว เหมาะกับ `keyword` ไม่ใช่ข้อความทั่วไป
  • `bool.must` รวมเงื่อนไขที่มีผลต่อ score
  • `bool.filter` ตัดชุดข้อมูลโดยไม่คำนวณ score และ cache ได้

Relevance เริ่มจากคะแนนแบบ BM25 แต่ของจริงต้องนิยามความหมายของ “ผลลัพธ์ที่ดี” จากธุรกิจ เช่น boost ชื่อสินค้า, ลดคะแนนของข้อมูลเก่า และวัดด้วยชุด query พร้อม expected result ไม่ใช่ปรับ boost จากความรู้สึก

อ่านเอกสารทางการ: Elastic: Query and filter context ↗

04Shard, Replica และ Routing

Index แบ่งเป็น primary shards โดยแต่ละ shard คือหน่วยเก็บข้อมูลและ กระจายงาน Replica เป็นสำเนาที่ช่วยทนต่อ node failure และรับ search เพิ่มได้ แต่ทุก replica เพิ่ม write, network และ storage cost

จำนวน shard มากเกินไปสร้าง overhead ต่อ cluster ส่วน shard ใหญ่เกินไป ย้ายและ recover ช้า อย่าเลือกจำนวน shard จากจำนวน node อย่างเดียว ให้เริ่มจาก data volume, retention, query rate, recovery target แล้ว load test ด้วยข้อมูลและ query ที่ใกล้ของจริง

อ่านเอกสารทางการ: Elastic: Shard allocation, replica และ recovery ↗

05ให้ Database เป็น Source of Truth

สำหรับข้อมูลธุรกรรม ให้ PostgreSQL หรือระบบหลักเป็นผู้บังคับ invariant แล้วส่งการเปลี่ยนแปลงไป Elasticsearch ผ่าน transactional outbox, CDC หรือ event stream วิธีนี้ยอมรับ eventual consistency แต่หลีกเลี่ยงการทำ dual write ตรง ๆ ที่ DB สำเร็จแต่ index ล้มเหลว

Database + Outbox→CDC / Event→Indexer→Elasticsearch
  • ใช้ business ID เป็น document ID เพื่อให้ upsert ซ้ำได้อย่าง idempotent
  • ส่ง version หรือ sequence มากัน event เก่าเขียนทับข้อมูลใหม่
  • มี reconciliation ตรวจ DB กับ index และ replay/backfill ได้
  • เปลี่ยน mapping ด้วย index ใหม่ แล้วสลับ alias เมื่อ reindex เสร็จ
อ่านเอกสารทางการ: Elastic: Index aliases และ reindex โดยไม่หยุดอ่าน ↗

06Performance และสิ่งที่ต้องเฝ้าระวัง

  • ส่งหลาย document ด้วย Bulk API และทำ batch ที่ไม่ใหญ่จน memory พุ่ง
  • หลีกเลี่ยง deep pagination ด้วย `from`/`size`; ใช้ `search_after` + PIT
  • จำกัด field และ dynamic mapping เพื่อป้องกัน mapping explosion
  • Monitor indexing/search latency, rejection, JVM heap, GC, disk watermark, shard health และ slow logs
  • วัด query distribution, cache hit และขนาด response ไม่ใช่ดูเฉพาะ QPS
อ่านเอกสารทางการ: Elastic: Paginate search results ↗