ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务。本文以6.2.3版本为例介绍ElasticSearch的应用。json
本文首先介绍ElasticSearch中的索引和文档的概念,并在系列的其它文章进行更进一步的介绍。bash
目录:数据结构
能够在官网下载压缩包, 在解压目录中执行bin/elasticsearch
来启动服务, 或者使用包管理器来安装启动.app
ES默认端口为9200, 本地启动ES后向http://localhost:9200
发送GET请求能够查看ES的基本信息:elasticsearch
GET 'localhost:9200' { "name" : "hiTUe19", "cluster_name" : "elasticsearch_finley", "cluster_uuid" : "cfKnyFL1Rx6URmrmAuMBFw", "version" : { "number" : "5.1.2", "build_hash" : "c8c4c16", "build_date" : "2017-01-11T20:18:39.146Z", "build_snapshot" : false, "lucene_version" : "6.3.0" }, "tagline" : "You Know, for Search" }
ElasticSearch采用三层数据结构来管理数据:分布式
index
): 索引是最高层的数据结构,能够定义独立的搜索索引和分片存储策略type
): 每一个index能够拥有多个type, 用于存储不一样类型的文档ElasticSearch中的文档是一个Json对象,搜索的结果是文档的集合所以被称为面向文档的搜索。ui
与三层数据结构相对应,咱们可使用三个字段来惟一标识文档:搜索引擎
_index
: 表明文档所在的索引。索引名必须小写, 不能如下划线开头, 不能包含逗号._type
: 表明文档所在的类型集。type名能够为大小写, 不能如下划线开头, 不能包含逗号._id
: 用于惟一标识某个type中的文档空查询请求能够列出某个数据结构中全部文档:url
GET /_search
blog
下的全部文档: GET /blog/_search
/blog/user
下的全部文档: GET /blog/user/_search
IndexAPI
能够用于建立文档:code
$ POST 'localhost:9200/blog/user/' content-type: application/json body: { "id": 1, "nickname": "finley" } response: { "_index": "blog", "_type": "user", "_id": "AV5WoO0MdsHuOObNBTWU", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
使用POST请求建立文档, 在url中指定_index
和_type
, 在请求体中使用json格式提交文档数据。
若_index
或_type
不存在ES会自动建立。上述请求中文档的_id
字段由ElasticSearch建立,咱们也能够本身指定_id
:
POST localhost:9200/blog/user/2/ content-type: application/json { "id": 2, "nickname": "easy" } response: { "_index": "blog", "_type": "user", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
使用GET请求访问文档,须要提供_index
, _type
和_id
三个参数惟一标识文档。
GET localhost:9200/blog/user/2/ response: { "_index": "blog", "_type": "user", "_id": "2", "_version": 2, "found": true, "_source": { "id": 2, "nickname": "easy" } }
由于修改文档后难以更新索引,所以ElasticSearch修改文档的操做是经过删除原文档后从新添加新文档来进行的。
使用IndexAPI
对已存在的文档发送POST请求则会更新文档:
POST localhost:9200/blog/user/2/ content-type: application/json { "nickname": "easy", "gender": "male" } response: { "_index": "blog", "_type": "user", "_id": "2", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false }
注意_version
, created
, result
字段显示文档已被更新。经过GET请求查看更新后的文档:
GET localhost:9200/blog/user/2/ { "_index": "blog", "_type": "user", "_id": "2", "_version": 2, "found": true, "_source": { "nickname": "easy2", "gender": ”male“ } }
注意到原文档中的_id
字段已经不见了,文档彻底由咱们发送的上一个POST请求定义。
修改文档也能够经过PUT方法:
PUT localhost:9200/blog/user/2/ content-type: application/json { "id": 2, "nickname": "easy3" } { "_index": "blog", "_type": "user", "_id": "2", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false }
再次经过GET请求确认文档已被修改:
GET localhost:9200/blog/user/2/ { "_index": "blog", "_type": "user", "_id": "2", "_version": 3, "found": true, "_source": { "id": 2 "nickname": "easy3", } }
删除文档须要发送DELETE
请求:
DELETE localhost:9200/blog/user/2/ response: { "found": true, "_index": "blog", "_type": "user", "_id": "2", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 } }