
汇总
- keyword vs text:
-
keyword:精确匹配,用于过滤、聚合、排序。text:全文搜索,用于内容检索。
- 数值类型选择:
-
integer:整数值,如状态码、等级long:大数值,如文件大小、价格date:时间日期,支持范围查询
索引配置项设计
"settings": {
"number_of_shards": 15,
"number_of_replicas": 1,
"refresh_interval": "30s"
}
- shards: 15 - 数据分片数,影响并行处理能力
- replicas: 1 - 每个分片的副本数,影响高可用性
- refresh_interval: 30s - 索引刷新间隔,影响数据实时性
索引字段类型
keyword
keyword类型:
"id": {
"type": "keyword"
}
- 含义:用于精确值匹配,不分词
- 适用场景:ID、状态码、标签等需要精确匹配的字段
- 特点:支持聚合、排序,性能优于text类型
text类型
"fileName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
- 含义:全文搜索字段,会被分词
- analyzer:索引时使用的分词器
- search_analyzer:搜索时使用的分词器
- ik_max_word:细粒度分词
- ik_smart:粗粒度分词
多字段映射 (Multi-fields)
fileName的多字段定义:
"fileName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
},
"raw": {
"type": "text",
"analyzer": "standard"
}
}
}
- fileName:主字段,使用IK中文分词
- fileName.keyword:keyword子字段,用于精确匹配、聚合
- fileName.raw:使用标准分词器的text子字段
- ignore_above: 256 - 超过256字符的不会被索引
path_analyzer
"filePath": {
"type": "text",
"analyzer": "path_analyzer",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
- path_analyzer:自定义路径分析器,用于文件路径的层次化搜索
- 可以搜索如
/home/user/documents/并匹配子路径
数值类型 float
"fileSize": {
"type": "float"
}
- float:单精度浮点数,适合文件大小存储
日期类型
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
- format:支持多种日期格式
yyyy-MM-dd HH:mm:ss:字符串格式epoch_millis:时间戳格式
tags字段设计
"tags": {
"type": "keyword",
"fields": {
"text": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
- tags:keyword类型,用于精确标签匹配
- tags.text:text类型,用于标签的全文搜索
自定义路径分析器
"analysis": {
"analyzer": {
"path_analyzer": {
"type": "custom",
"tokenizer": "path_tokenizer"
}
},
"tokenizer": {
"path_tokenizer": {
"type": "path_hierarchy"
}
}
}
- path_hierarchy:专门用于路径层次结构的tokenizer
- 例如
/a/b/c会被分词为:/a,/a/b,/a/b/c
评论区请在客户端页面查看