Elasticsearch 索引与文档操作指南_elasticsearch接口文档
- Elasticsearch 索引与文档操作指南
-
- 1. 索引库操作
-
- 1.1 Mapping映射属性
- 2. 文档操作
-
- 2.1 基础操作
- 2.2 操作示例
- 3. 查询操作
-
- 3.1 基础查询语法
- 3.2 查询类型详解
-
- 3.2.1 精准匹配
- 3.2.2 全文检索
- 3.2.3 复合查询
- 4. 批处理与事务
-
- 4.1 Bulk API
- 5. 高级功能
-
- 5.1 分词器管理
- 5.2 多文档操作
- 6. 最佳实践
Elasticsearch 索引与文档操作指南
1. 索引库操作
索引库(Index)类比数据库表,Mapping映射类比表结构。数据存储前需先创建索引和Mapping。
1.1 Mapping映射属性
完整示例:创建索引库
PUT /heima { \"mappings\": { \"properties\": { \"info\": { \"type\": \"text\", \"analyzer\": \"ik_smart\", \"search_analyzer\": \"ik_smart\" // 查询时中文分词器 }, \"email\": { \"type\": \"keyword\", \"index\": false // 该字段不参与搜索 }, \"name\": { \"type\": \"object\", // 对象类型(可省略) \"properties\": { \"firstName\": { \"type\": \"keyword\", \"ignore_above\": 64 // 超长文本不索引 }, \"lastName\": { \"type\": \"keyword\" } } }, \"createTime\": { \"type\": \"date\", \"format\": \"yyyy-MM-dd HH:mm:ss\" } } }}
2. 文档操作
2.1 基础操作
/{索引名}/_doc
/{索引名}/_doc/{id}
/{索引名}/_doc/{id}
/{索引名}/_update/{id}
/{索引名}/_doc/{id}
{索引库名}/_doc/{id}
关键提示
- put不允许不带id,post可以自己生成id
- put如果id数据存在则为update,否则为create:updated状态码200,created状态码201
- 局部修改是只修改指定id匹配的文档中的部ll分字段。POST /{索引库名}/_update/文档id { “doc”: { “字段名”: “新的值”, } }
冲突处理示例
PUT /heima/_doc/1?version=1 // 指定版本号修改{ \"info\": \"新内容\"}
2.2 操作示例
局部修改脚本更新
POST /heima/_update/1{ \"script\": { \"source\": \"ctx._source.age += params.increment\", \"params\": { \"increment\": 2 } }}
3. 查询操作
3.1 基础查询语法
GET /heima/_search{ \"query\": { \"match_all\": {} // 匹配所有文档 }, \"from\": 0, // 分页起始位置(从0开始) \"size\": 10, // 每页数量 \"_source\": [\"name\", \"email\"], // 结果字段过滤 \"sort\": [ { \"createTime\": \"desc\" } ]}
3.2 查询类型详解
3.2.1 精准匹配
{ \"term\": { \"status\": \"published\" } }
{ \"terms\": { \"tags\": [\"java\", \"es\"] } }
{ \"range\": { \"age\": { \"gte\": 18, \"lte\": 30 } } }
3.2.2 全文检索
{ \"match\": { \"content\": \"分布式系统\" } }
{ \"match_phrase\": { \"content\": \"elastic search\" } }
{ \"match_phrase_prefix\": { \"content\": \"quick bro\" } }
{ \"multi_match\": { \"query\": \"数据库\", \"fields\": [\"title^2\", \"desc\"] } }
3.2.3 复合查询
GET /articles/_search{ \"query\": { \"bool\": { \"must\": [ { \"match\": { \"title\": \"大数据\" } } ], \"should\": [ { \"term\": { \"isHot\": true } }, { \"range\": { \"readCount\": { \"gte\": 1000 } } } ], \"must_not\": [ { \"term\": { \"status\": \"draft\" } } ], \"filter\": [ { \"term\": { \"category\": \"tech\" } } ] } }}
4. 批处理与事务
4.1 Bulk API
POST _bulk{ \"index\" : { \"_index\" : \"test\", \"_id\" : \"1\" } }{ \"field1\" : \"value1\" }{ \"delete\" : { \"_index\" : \"test\", \"_id\" : \"2\" } }{ \"create\" : { \"_index\" : \"test\", \"_id\" : \"3\" } }{ \"field1\" : \"value3\" }{ \"update\" : {\"_id\" : \"1\", \"_index\" : \"test\"} }{ \"doc\" : {\"field2\" : \"value2\"} }
响应示例
{ \"took\": 30, \"errors\": false, \"items\": [ { \"index\": { \"_id\": \"1\", \"status\": 201 } }, { \"delete\": { \"_id\": \"2\", \"status\": 404 } }, { \"create\": { \"_id\": \"3\", \"status\": 201 } } ]}
5. 高级功能
5.1 分词器管理
分词器类别
Standard Analyzer-默认分词器,按词切分,小写处理
Simple Analyzer-按照非字母切分(符号被过滤),小写处理
Stop Analyzer-小写处理,停用词过滤(the ,a,is)
Whitespace Analyzer-按照空格切分,不转小写Keyword Analyzer-不分词,直接将输入当做输出
Patter Analyzer-正则表达式,默认\\W+
Language-提供了 30 多种常见语言的分词器N
测试分词效果
GET _analyze{ \"text\": \"黑马程序员Java课程\", \"analyzer\": \"ik_smart\"}
自定义词典
- 在
config/analysis-ik
目录下创建custom.dic
- 修改
IKAnalyzer.cfg.xml
:
<entry key=\"ext_dict\">custom.dic</entry>
5.2 多文档操作
GET _mget{ \"docs\": [ { \"_index\": \"heima\", \"_id\": \"1\" }, { \"_index\": \"test\", \"_id\": \"2\", \"_source\": [\"name\", \"age\"] } ]}
6. 最佳实践
-
索引设计原则
• 避免过度嵌套(nested类型有性能损耗)
• 合理使用keyword类型存储枚举值
• 对不需要搜索的字段设置\"index\": false
-
查询优化建议
• 优先使用filter上下文(不计算相关性得分)
• 避免深度分页(使用search_after替代from/size)
• 对text字段的聚合操作需使用fielddata: true
-
写入性能优化
• 使用bulk API批量操作
• 调整refresh_interval(默认1秒刷新)
• 禁用副本写入(\"index.number_of_replicas\": 0
)