ElasticSearch入门简介
ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务。本文以6.2.3版本为例介绍ElasticSearch的应用。
本文首先介绍ElasticSearch中的索引和文档的概念,并在系列的其它文章进行更进一步的介绍。
目录:
可以在官网下载压缩包, 在解压目录中执行bin/elasticsearch来启动服务, 或者使用包管理器来安装启动.
ES默认端口为9200, 本地启动ES后向http://localhost:9200发送GET请求可以查看ES的基本信息:
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对象,搜索的结果是文档的集合因此被称为面向文档的搜索。
与三层数据结构相对应,我们可以使用三个字段来唯一标识文档:
- _index: 代表文档所在的索引。索引名必须小写, 不能以下划线开头, 不能包含逗号.
- _type: 代表文档所在的类型集。type名可以为大小写, 不能以下划线开头, 不能包含逗号.
- _id: 用于唯一标识某个type中的文档
空查询请求可以列出某个数据结构中所有文档:
- 列出所有文档: GET /_search
- 列出索引blog下的所有文档:GET /blog/_search
- 列出类型/blog/user下的所有文档:GET /blog/user/_search
创建文档
IndexAPI可以用于创建文档:
$ 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
    }
}

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号