Canal与Elasticsearch数据同步企业级实战 - Elasticsearch索引设计
本文是 Canal与Elasticsearch数据同步企业级实战 的分章阅读版。
Elasticsearch索引设计
// 索引设计三要素:mapping + 分片 + 副本
// 1. Mapping(字段映射) — 房屋数据索引
PUT /house_index
{
"settings": {
"number_of_shards": 2, // 1000万文档≈50GB → 2分片
"number_of_replicas": 1, // 2节点集群 → 1副本
"analysis": {
"analyzer": {
"ik_smart_analyzer": {
"type": "custom",
"tokenizer": "ik_smart" // 中文分词
}
}
}
},
"mappings": {
"properties": {
"houseId": { "type": "keyword" }, // 精确匹配
"title": { "type": "text",
"analyzer": "ik_smart_analyzer",
"search_analyzer": "ik_max_word" }, // 中文搜索
"address": { "type": "text", "analyzer": "ik_smart" },
"price": { "type": "double" }, // 范围查询
"area": { "type": "double" },
"district": { "type": "keyword" }, // 聚合分组
"createTime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" },
"status": { "type": "keyword" }
}
}
}
// 2. 高亮搜索 + 权重排序
GET /house_index/_search
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "南山 三室",
"fields": ["title^3", "address^1"], // title权重3倍
"type": "best_fields"
}
},
"functions": [
{ "field_value_factor": { "field": "price", "factor": 0.1 } }
],
"boost_mode": "multiply"
}
},
"highlight": {
"fields": {
"title": {
"pre_tags": ["<em class='highlight'>"],
"post_tags": ["</em>"],
"fragment_size": 150
}
}
},
"from": 0, "size": 20
}
// 面试话术:"分片数=数据量GB÷50(每分片30-50GB最佳),
// 副本数=读节点数-1。1000万房屋约50GB,2主分片+1副本,
// 支持2节点集群,单分片查询延迟在50ms以内。"
相关页面