ES初探!

📅 2026/7/28 16:09:42
ES初探!
前言在上手使用前需要先了解一些基本的概念。推荐可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 阅读《Elastic Search 权威指南》有非常详细和全面的说明。ES中的一些概念index索引相当于mysql中的数据库type类型相当于mysql中的一张表document文档相当于mysql中的一行一条记录field域相当于mysql中的一列一个字段节点一个服务器由一个名字来标识集群一个或多个节点组织在一起分片将一份数据划分为多小份的能力允许水平分割和扩展容量。多个分片可以响应请求提高性能和吞吐量。副本复制数据一个节点出问题时其余节点可以顶上。倒排索引可参考https://www.elastic.co/guide/cn/elasticsearch/guide/current/inverted-index.html。索引类型对索引的基本操作创建索引通过以下命令可创建一个索引PUT job { settings:{ index:{ number_of_shards:5, number_of_replicas:1 } } }返回{ acknowledged: true, shards_acknowledged: true }Elasticsearch 是利用分片将数据分发到集群内各处的。分片是数据的容器文档保存在分片内分片又被分配到集群内的各个节点里。当你的集群规模扩大或者缩小时 Elasticsearch 会自动的在各节点中迁移分片使得数据仍然均匀分布在集群里。一个分片可以是 主 分片或者 副本 分片。 索引内任意一个文档都归属于一个主分片所以主分片的数目决定着索引能够保存的最大数据量。一个副本分片只是一个主分片的拷贝。 副本分片作为硬件故障时保护数据不丢失的冗余备份并为搜索和返回文档等读操作提供服务。在上面例子中主分片为5副本分片为1.查看索引的信息GET job查看job这个索引的信息{ job: { aliases: {}, mappings: {}, settings: { index: { creation_date: 1502342603160, number_of_shards: 5, number_of_replicas: 1, uuid: LGalsb3eRKeGb5SbWCxO8w, version: { created: 5010199 }, provided_name: job } } } }可以只查看某一项信息GET job/_settings可以查看job这个索引的settings信息{ job: { settings: { index: { creation_date: 1502342603160, number_of_shards: 5, number_of_replicas: 1, uuid: LGalsb3eRKeGb5SbWCxO8w, version: { created: 5010199 }, provided_name: job } } } }修改索引信息例如将副本分片数量修改为2PUT job/_settings { number_of_replicas:2 }映射在创建索引时我们可以预先设定映射规定好各个字段及其数据类型便于es更好地进行管理。比如说以文章库为例 一篇文章的关键词字段应当作为完整的词语而文章的正文字段必须通过中文分词器进行分词。通过设置映射mapping可以告知es这些字段的规则。更详细文档参见https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-intro.html数据类型Elasticsearch支持如下类型字符串: text, keyword注5之前的版本里有string类型5之后不再支持此类型数字: byte, short, integer, long, float, double布尔型:boolean日期: date复杂类型如object, nested等查看映射输入GET job/_mapping可以查看job索引下的所有映射。默认映射在创建索引存入数据时如果不指定类型es会自动根据实际数据为其添加类型。例如通过下面的语句插入文档PUT job/type1/1 { title:abc, words:123, date:2017-01-01, isok:true }然后查看映射结果为{ job: { mappings: { type1: { properties: { date: { type: date }, isok: { type: boolean }, title: { type: text, fields: { keyword: { type: keyword, ignore_above: 256 } } }, words: { type: long } } } } } }可见es自动根据类型对字段进行了映射。设置映射在创建索引时可以设置映射规则具体格式形如上面查看映射时的返回结果。PUT job { mappings:{ type2:{ properties:{ title:{ type:keyword }, salary:{ type:integer }, desc:{ type:text, analyzer: ik_max_word }, date:{ type:date, format:yyyy-MM-dd } } } } }注意在上面为desc字段指定了analyzer就是一个自定义分词器。在es-rtf中默认给安装了ik_smart和ik_max_word两个分词器区别在于后者会分出更多的词。为text类型的字段会被进行分词然后索引而keyword字段不会被分词。自动转换创建索引和映射后插入文档时字段会自动转换成映射中规定的类型。比如插入123到integer字段会自动尝试对字符串进行类型转换。如果无法转换则会报错无法插入。文档一个“文档”即所谓的一条记录。可对文档进行增删改操作。插入文档可以指定文档id即 PUT index_name/type_name/id。PUT job/type2/1 { title:Python工程师, salary:1000, desc:1. 参与devops相关系统开发包括云资源管理平台cmdb平台、资源申请流程、基础支撑平台开发2. 参与公司业务系统及自动化运维平台的开发3. 积累并规范化系统开发的最佳实践并文档化4. 完善并遵守团队的编码规范编写高质量、结构清晰、易读、易维护的代码。, date:2017-08-08 }返回{_index: job,_type: type2,_id: 1,_version: 1,result: created,_shards: {total: 2,successful: 1,failed: 0},created: true}也可不指定id则会自动分配id。注意这里要使用POST方式。POST job/type2/ { title:Python工程师2, salary:1000, desc:1. 参与devops相关系统开发包括云资源管理平台cmdb平台、资源申请流程、基础支撑平台开发2. 参与公司业务系统及自动化运维平台的开发3. 积累并规范化系统开发的最佳实践并文档化4. 完善并遵守团队的编码规范编写高质量、结构清晰、易读、易维护的代码。, date:2017-08-08 }查看文档只需通过GET方式查看GET job/type2/1返回文档信息{ _index: job, _type: type2, _id: 1, _version: 3, found: true, _source: { title: Java, salary: 2000, desc: 易维护的代码, date: 2017-08-08 } }可以只查看_source中的部分字段GET job/type2/1?_sourcetitle,salary返回{ _index: job, _type: type2, _id: 1, _version: 3, found: true, _source: { title: Java, salary: 2000 } }修改文档一种是通过PUT的全覆盖方式旧数据将被删除以新的代替。PUT job/type2/1 { title:Java, salary:1400, desc:易维护的代码, date:2017-08-08 }另一种是通过POST方式只对部分字段进行修改。POST job/type2/1/_update { doc:{ salary:2000 } }删除文档通过DELETE方式可删除文档DELETE job/type2/1mget取回多个文档可参考https://www.elastic.co/guide/cn/elasticsearch/guide/current/_Retrieving_Multiple_Documents.html通过将查询合并可以减少连接次数提高效率。GET _mget { docs : [ { _index : job, _type : type2, _id : 1 }, { _index : job, _type : type2, _id : 2, _source: salary } ] }返回两个文档{ docs: [ { _index: job, _type: type2, _id: 1, _version: 3, found: true, _source: { title: Java, salary: 2000, desc: 易维护的代码, date: 2017-08-08 } }, { _index: job, _type: type2, _id: 2, found: false } ] }还可进行简写比如index和type都相同查找两个id可以写作GET job/type2/_mget { ids:[1, 2] } }bulk批量操作bulk API 允许在单个步骤中进行多次 create 、 index 、 update 或 delete 请求。详细参考https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.htmlbulk批量操作的请求比较特殊格式为{ action: { metadata }}\n{ request body }\n{ action: { metadata }}\n{ request body }\n ...一般两行为一条请求第一行说明操作和元数据第二行是操作数据。不过delete请求只有一行。POST _bulk { delete: { _index: website, _type: blog, _id: 123 }} { create: { _index: website, _type: blog, _id: 123 }} { title: My first blog post } { index: { _index: website, _type: blog }} { title: My second blog post } { update: { _index: website, _type: blog, _id: 123, _retry_on_conflict : 3} } { doc : {title : My updated blog post} }返回结果会列出每个请求的处理状态。{ took: 4, errors: false, items: [ { delete: { _index: website, _type: blog, _id: 123, _version: 2, status: 200, found: true }}, { create: { _index: website, _type: blog, _id: 123, _version: 3, status: 201 }}, { create: { _index: website, _type: blog, _id: EiwfApScQiiy7TIKFxRCTw, _version: 1, status: 201 }}, { update: { _index: website, _type: blog, _id: 123, _version: 4, status: 200 }} ] }通过以上操作可以将数据以一定的组织方式写入到es中。下一篇将总结如何进行搜索和查找。感谢你的阅读 文章如有错误之处还请不吝批评指正