
Full-text, lọc, sắp xếp, phân trang, ElasticsearchFull-text, filters, sorting, pagination, Elasticsearch
Săn áo khoác mùa lạnhHunting winter jackets.Đầu mùa lạnh, một sàn thời trang có 2,4 triệu sản phẩm nhận 3.000 lượt tìm mỗi giây. Phần lớn khách gõ không dấu, kiểu “ao khoac nam”.At the start of winter, a fashion marketplace with 2.4 million products gets 3,000 searches a second. Most shoppers type without diacritics, like “ao khoac nam”.
Hãy theo một khách gõ “ao khoac nam” giữa đầu mùa lạnh và một người bán vừa giảm giá chiếc áo đó. Catalog Service chỉ ghi PostgreSQL; Debezium đọc WAL và đẩy product.changed lên Kafka, Indexer ghi vào OpenSearch theo lô kèm version, còn Search API chỉ đọc qua alias products. Chạy luồng tìm kiếm trước, rồi bật “Cập nhật sản phẩm”, “Tài liệu sai mapping” và “Reindex sau alias” để xem giá mới lan tới index, lỗi bị cách ly và mapping đổi mà không gián đoạn.Follow a shopper typing “ao khoac nam” at the start of winter and a seller who just cut the price of that jacket. The Catalog Service only writes PostgreSQL; Debezium reads the WAL and publishes product.changed to Kafka, the Indexer bulk-writes to OpenSearch with versions, and the Search API only reads through the products alias. Play the search first, then turn on “Product updated”, “Document breaks the mapping” and “Reindex behind an alias” to see a new price reach the index, a bad document get isolated and a mapping change with no downtime.
1Người dùng gõ “ao khoac nam” (không dấu), chọn thương hiệu Uniqlo và khoảng giá 200k–800k.A user types “ao khoac nam” without diacritics, picks the Uniqlo brand and a 200k–800k price range.
Vài trăm nghìn tới vài triệu dòng, ít facet, chấp nhận ts_rank đơn giản: tsvector + GIN + unaccent là đủ, khỏi đồng bộ hệ thứ hai. Cần facet phức tạp, sửa lỗi gõ, gợi ý, chấm điểm tinh chỉnh hoặc tải tìm kiếm lớn thì chuyển sang OpenSearch. Đặt sau một SearchPort để đổi engine không đụng nghiệp vụ.Hundreds of thousands to a few million rows, few facets, simple ts_rank: tsvector, GIN and unaccent are enough, with no second system to sync. Rich facets, typo tolerance, suggestions, tuned scoring or heavy search load call for OpenSearch. Put it behind a SearchPort so the engine can change without touching business code.
Ghi DB rồi gọi OpenSearch trong request thì một trong hai có thể lỗi. Ghi thay đổi qua outbox hoặc CDC (Debezium đọc WAL), Indexer tiêu thụ từ Kafka và thử lại tới khi thành công.Writing the DB and then calling OpenSearch in the request lets one of them fail alone. Capture changes through an outbox or CDC (Debezium on the WAL); the Indexer consumes from Kafka and retries until it succeeds.
Retry hoặc backfill có thể đưa bản cũ tới sau bản mới. Key Kafka theo id sản phẩm giữ thứ tự trong partition; thêm version_type=external với version của dòng để OpenSearch tự từ chối bản cũ.Retries or a backfill can deliver an old copy after a new one. Keying Kafka by product id keeps order per partition; version_type=external with the row version makes OpenSearch reject the stale copy itself.
Analyzer là chuỗi char filter → tokenizer → token filter. Dùng icu_tokenizer + lowercase + icu_folding (hoặc asciifolding với preserve_original) để “đồng hồ” khớp “dong ho”; giữ thêm field gốc có dấu và boost cao hơn để kết quả đúng dấu lên trước. Lưu ý chữ đ phải gập thành d.An analyzer is a chain: char filters → tokenizer → token filters. Use icu_tokenizer, lowercase and icu_folding (or asciifolding with preserve_original) so “đồng hồ” matches “dong ho”; keep an accented field with a higher boost so exact-diacritic matches rank first. Make sure đ folds to d.
Mỗi bộ lọc là một Specification nhỏ tự biết thêm term, range hay terms vào filter. Builder ghép chúng với must, should, function_score và aggregation; facet của chính bộ lọc đang chọn đặt vào post_filter để vẫn đếm đủ các lựa chọn khác.Each filter is a small Specification that knows how to add its term, range or terms clause. A Builder combines them with must, should, function_score and aggregations; the selected facet's own filter goes into post_filter so its other options still get counts.
from + size buộc mỗi shard xếp hạng from+size tài liệu; OpenSearch chặn ở max_result_window 10.000. Dùng search_after với sort có tie-breaker (score, id), kèm point-in-time nếu cần ảnh chụp nhất quán khi cuộn.from + size makes every shard rank from+size documents, and OpenSearch stops at max_result_window 10,000. Use search_after with a tie-breaking sort (score, id), plus a point-in-time when the scroll needs a consistent snapshot.
Client luôn đọc qua alias, không gọi tên index thật. Tạo index mới, ghi kép, backfill, kiểm tra, rồi đổi alias bằng một lệnh _aliases nguyên tử. Alias đóng vai proxy đứng trước index thật.Clients always read through an alias, never a physical index name. Create the new index, dual-write, backfill, verify, then switch with one atomic _aliases call. The alias acts as a proxy in front of the real index.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
product | id, name, brand, category_id, price, in_stock, sold_30d, description, version, updated_at, search_vector tsvector | Nguồn sự thật. version tăng mỗi lần sửa; search_vector là cột generated với GIN index khi chỉ dùng PostgreSQLSource of truth. version increases on every edit; search_vector is a generated column with a GIN index when PostgreSQL alone is enough |
outbox | id, aggregate_id, type, payload, created_at | Dùng khi không bật CDC trên bảng product; Debezium outbox router đẩy sang product.changedUsed when CDC is not enabled on product itself; the Debezium outbox router publishes it to product.changed |
products_v4 (OpenSearch) | id keyword, name text(vi_folding)+name.raw, brand text+brand.keyword, category keyword, price scaled_float, in_stock boolean, sold_30d integer | Mapping strict (dynamic: strict) để field lạ bị từ chối thay vì tự đoán kiểuStrict mapping (dynamic: strict) so unknown fields are rejected instead of guessed |
alias products / products_write | products → products_v4, products_write → products_v4 (is_write_index) | Search API đọc products, Indexer ghi products_write; reindex chỉ đổi aliasThe Search API reads products, the Indexer writes products_write; a reindex only moves aliases |
ProductSearchService (Java) là hộp Search API ở sơ đồ: ghép filter, chấm điểm, facet và search_after rồi gọi alias products trên hộp OpenSearch bằng Elasticsearch Java client (OpenSearch client có API gần như giống hệt); product_search.sql là phương án chỉ dùng hộp PostgreSQL. Indexer (Go) là hộp Indexer: đọc product.changed từ Kafka, ghi bulk có external version và đẩy tài liệu hỏng sang DLQ.ProductSearchService (Java) is the Search API box in the diagram: it composes filters, scoring, facets and search_after and queries the products alias on the OpenSearch box with the Elasticsearch Java client (the OpenSearch client is nearly identical); product_search.sql is the PostgreSQL-only alternative. Indexer (Go) is the Indexer box: it reads product.changed from Kafka, bulk-writes with external versions and sends broken documents to the DLQ.
package com.shop.search;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.AggregationRange;
import co.elastic.clients.elasticsearch._types.query_dsl.FieldValueFactorModifier;
import co.elastic.clients.elasticsearch._types.query_dsl.FunctionBoostMode;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class ProductSearchService {
private final ElasticsearchClient es;
public SearchPage<ProductDoc> search(ProductQuery r) throws IOException {
List<Query> filters = new ArrayList<>();
if (r.brand() != null)
filters.add(TermQuery.of(t -> t.field("brand.keyword").value(r.brand()))._toQuery());
if (r.minPrice() != null || r.maxPrice() != null)
filters.add(RangeQuery.of(q -> q.number(n -> n.field("price")
.gte(r.minPrice()).lte(r.maxPrice())))._toQuery());
filters.add(TermQuery.of(t -> t.field("in_stock").value(true))._toQuery());
SearchResponse<ProductDoc> res = es.search(s -> s
.index("products") // alias, not products_v4
.size(24)
.query(q -> q.functionScore(fs -> fs
.query(b -> b.bool(bool -> bool
.must(m -> m.multiMatch(mm -> mm.query(r.text())
.fields("name^3", "name.raw^4", "brand^2", "description")))
.filter(filters)))
.functions(f -> f.fieldValueFactor(v -> v
.field("sold_30d").modifier(FieldValueFactorModifier.Log1p)))
.boostMode(FunctionBoostMode.Sum)))
.sort(o -> o.score(sc -> sc.order(SortOrder.Desc)))
.sort(o -> o.field(f -> f.field("id").order(SortOrder.Asc)))
.searchAfter(r.cursor()) // [score, id] of the last hit
.aggregations("brand", a -> a.terms(t -> t.field("brand.keyword").size(20)))
.aggregations("price", a -> a.range(g -> g.field("price").ranges(
AggregationRange.of(x -> x.to(300_000.0)),
AggregationRange.of(x -> x.from(300_000.0).to(800_000.0)),
AggregationRange.of(x -> x.from(800_000.0))))),
ProductDoc.class);
return SearchPage.of(res);
}
}package main
import (
"bytes"
"context"
"encoding/json"
"net/http"
"github.com/elastic/go-elasticsearch/v8/esutil"
"github.com/segmentio/kafka-go"
)
type ProductChanged struct {
ID string `json:"id"`
Version int64 `json:"version"` // product.version from PostgreSQL
Deleted bool `json:"deleted"`
Doc json.RawMessage `json:"doc"`
}
type Indexer struct {
bulk esutil.BulkIndexer // FlushInterval 1s, FlushBytes 5 MB
targets func() []string // ["products_write"], or v3 + v4 during a reindex
dlq Publisher
}
func (ix *Indexer) Handle(ctx context.Context, m kafka.Message) error {
var ev ProductChanged
if err := json.Unmarshal(m.Value, &ev); err != nil {
return ix.dlq.Publish(ctx, m) // poison message: do not block the partition
}
for _, index := range ix.targets() {
item := esutil.BulkIndexerItem{
Index: index,
Action: "index",
DocumentID: ev.ID,
Version: &ev.Version,
VersionType: "external", // older than the stored version → 409
Body: bytes.NewReader(ev.Doc),
OnFailure: func(ctx context.Context, _ esutil.BulkIndexerItem,
res esutil.BulkIndexerResponseItem, err error) {
if res.Status == http.StatusConflict {
return // a newer version is already indexed
}
_ = ix.dlq.Publish(ctx, m)
},
}
if ev.Deleted {
item.Action, item.Body = "delete", nil
}
if err := ix.bulk.Add(ctx, item); err != nil {
return err
}
}
return nil
}| PostgreSQL tsvector + GINPostgreSQL tsvector + GIN | Elasticsearch / OpenSearchElasticsearch / OpenSearch | |
|---|---|---|
| Hợp vớiGood for | Tới vài triệu dòng, tìm trong admin, ít facetUp to a few million rows, admin search, few facets | Tìm kiếm cho khách hàng, nhiều facet, tải lớnCustomer-facing search, many facets, heavy load |
| Tiếng Việt, lỗi gõVietnamese, typos | unaccent + simple; pg_trgm cho gõ saiunaccent + simple; pg_trgm for typos | ICU folding, fuzziness, synonym, completion suggesterICU folding, fuzziness, synonyms, completion suggester |
| Nhất quánConsistency | Cùng transaction, thấy ngaySame transaction, visible at once | Trễ vài giây, cần pipeline đồng bộSeconds behind, needs a sync pipeline |
| Chi phí vận hànhOperating cost | Không thêm hệ thống nàoNo extra system | Cụm riêng, CDC, Kafka, quy trình reindexA separate cluster, CDC, Kafka, a reindex procedure |
Service UPDATE PostgreSQL rồi gọi OpenSearch: nếu lần gọi thứ hai timeout, index giữ giá cũ mãi mà không ai biết. Chỉ ghi DB, để Debezium hoặc bảng outbox mang thay đổi sang Kafka, và Indexer chỉ commit offset sau khi bulk thành công. Theo dõi độ trễ từ commit tới searchable, cảnh báo khi vượt 10 giây.The service UPDATEs PostgreSQL then calls OpenSearch: if the second call times out, the index keeps the old price forever and nobody notices. Write only the database, let Debezium or an outbox carry the change to Kafka, and have the Indexer commit offsets only after the bulk succeeds. Track commit-to-searchable lag and alert above 10 seconds.
from=10000 buộc mỗi shard lấy và sắp xếp 10.024 bản ghi rồi bỏ gần hết; mặc định index.max_result_window là 10.000 nên request còn bị từ chối. Dùng search_after với sort values của kết quả cuối kèm tiebreaker là id, và PIT nếu cần ảnh chụp nhất quán. Giới hạn giao diện ở 50 trang cho người dùng thường.from=10000 makes each shard fetch and sort 10,024 documents only to discard most of them, and the default index.max_result_window of 10,000 rejects the request anyway. Use search_after with the last hit's sort values plus an id tiebreaker, and a PIT when you need a consistent snapshot. Cap the UI at 50 pages for normal users.
Khi Debezium dừng vài giờ, replication slot giữ lại toàn bộ WAL và đĩa PostgreSQL primary có thể đầy, kéo sập cả việc ghi đơn. Theo dõi pg_replication_slots theo số byte WAL bị giữ, cảnh báo ở 20 GB và đặt max_slot_wal_keep_size. Có runbook drop slot rồi chạy snapshot lại khi connector không cứu được.When Debezium stops for a few hours, its replication slot retains all WAL and the primary's disk can fill up, taking order writes down with it. Monitor retained WAL bytes in pg_replication_slots, alert at 20 GB and set max_slot_wal_keep_size. Keep a runbook to drop the slot and re-snapshot when the connector cannot recover.
Sản phẩm đầu tiên gửi size là số 42 nên field thành long, sản phẩm sau gửi “XL” và bulk báo mapper_parsing_exception. Đặt dynamic: strict, khai báo mapping tường minh và gửi tài liệu lỗi sang DLQ thay vì chặn cả partition. Cảnh báo khi DLQ có tài liệu mới để sửa mapper và phát lại.The first product sends size as the number 42 so the field becomes long; the next sends “XL” and the bulk fails with mapper_parsing_exception. Set dynamic: strict, declare explicit mappings, and send failing documents to a DLQ instead of blocking the partition. Alert on new DLQ documents so the mapper can be fixed and replayed.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
Khi dữ liệu vài triệu dòng, tải tìm kiếm vừa phải, ít facet và cần thấy ngay sau khi ghi. tsvector generated + GIN + unaccent, thêm pg_trgm cho lỗi gõ. Mỗi hệ thống thêm vào là một pipeline đồng bộ, một cụm phải vận hành và một nguồn lệch dữ liệu.When the data is a few million rows, search load is moderate, facets are few and results must be visible right after a write. A generated tsvector with GIN and unaccent, plus pg_trgm for typos. Every extra system brings a sync pipeline, a cluster to run and a place for data to drift.
Outbox ghi cùng transaction hoặc CDC đọc WAL bằng Debezium, đẩy lên Kafka với key là id. Indexer tiêu thụ, ghi bulk có external version và commit offset sau khi thành công. Định kỳ chạy job so khớp (đếm, checksum theo khoảng id) để bắt lệch còn sót.An outbox written in the same transaction, or CDC on the WAL with Debezium, into Kafka keyed by id. The Indexer consumes, bulk-writes with external versions and commits offsets after success. A periodic reconciliation job (counts, checksums by id range) catches any remaining drift.
Mỗi shard phải xếp hạng from+size tài liệu rồi node điều phối gộp lại, chi phí tăng tuyến tính theo độ sâu; mặc định bị chặn ở 10.000. search_after dùng sort values của kết quả cuối nên mỗi trang tốn như nhau; cần point-in-time nếu muốn ảnh chụp ổn định.Every shard ranks from+size documents and the coordinator merges them, so cost grows with depth, and it is capped at 10,000 by default. search_after uses the last hit's sort values so every page costs the same; add a point-in-time for a stable snapshot.
Không đổi tại chỗ được. Tạo index mới với mapping mới, ghi kép, backfill từ DB hoặc _reindex, kiểm tra số lượng và truy vấn mẫu, đổi alias bằng một lệnh _aliases, giữ index cũ một thời gian để quay lại.It cannot change in place. Create a new index with the new mapping, dual-write, backfill from the DB or with _reindex, check counts and sample queries, switch the alias in one _aliases call, and keep the old index for a while as a rollback.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.