> 技术文档 > Elasticsearch 索引与文档操作指南_elasticsearch接口文档

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映射属性

属性 说明 常见值/配置示例 type 字段数据类型 text、keyword、long、nested、object 等 analyzer 文本分词器 ik_smart(智能分词)、ik_max_word(最细粒度) index 是否创建倒排索引(影响是否可被搜索) true(默认)、false(仅存储不检索) format 日期格式定义 yyyy-MM-dd HH:mm:ss properties 嵌套字段定义 用于定义对象或嵌套类型的子字段 copy_to 组合字段 将多个字段值复制到新字段进行统一检索

完整示例:创建索引库

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 基础操作

操作类型 HTTP方法 端点格式 特点 新增文档 POST /{索引名}/_doc 自动生成文档ID 指定ID新增 PUT /{索引名}/_doc/{id} 强制指定文档ID(存在则覆盖) 全量修改 PUT /{索引名}/_doc/{id} 本质是删除旧文档+新建文档(版本号递增) 局部修改 POST /{索引名}/_update/{id} 仅更新指定字段(支持脚本更新) 删除文档 DELETE /{索引名}/_doc/{id} 根据ID物理删除 查询文档 GET {索引库名}/_doc/{id} 根据ID查询

关键提示

  1. put不允许不带id,post可以自己生成id
  2. put如果id数据存在则为update,否则为create:updated状态码200,created状态码201
  3. 局部修改是只修改指定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 完全匹配(不分词) { \"term\": { \"status\": \"published\" } } terms 多值匹配 { \"terms\": { \"tags\": [\"java\", \"es\"] } } range 范围查询 { \"range\": { \"age\": { \"gte\": 18, \"lte\": 30 } } }
3.2.2 全文检索
类型 特点 示例 match 分词查询(OR逻辑) { \"match\": { \"content\": \"分布式系统\" } } match_phrase 短语匹配(顺序一致) { \"match_phrase\": { \"content\": \"elastic search\" } } match_phrase_prefix 短语前缀匹配 { \"match_phrase_prefix\": { \"content\": \"quick bro\" } } multi_match 多字段匹配 { \"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\"} }
操作类型 特点 index 存在则替换,不存在则新增 create 仅当文档不存在时创建(冲突返回409) update 支持局部更新和脚本操作 delete 无需请求体,单独占一行

响应示例

{ \"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\"}

自定义词典

  1. config/analysis-ik目录下创建custom.dic
  2. 修改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. 最佳实践

  1. 索引设计原则
    • 避免过度嵌套(nested类型有性能损耗)
    • 合理使用keyword类型存储枚举值
    • 对不需要搜索的字段设置\"index\": false

  2. 查询优化建议
    • 优先使用filter上下文(不计算相关性得分)
    • 避免深度分页(使用search_after替代from/size)
    • 对text字段的聚合操作需使用fielddata: true

  3. 写入性能优化
    • 使用bulk API批量操作
    • 调整refresh_interval(默认1秒刷新)
    • 禁用副本写入(\"index.number_of_replicas\": 0