Elastic Search 分布式特性

在 Elasticsearch 中, 每个字段的所有数据 都是 默认被索引的 。 即每个字段都有为了快速检索设置的专用倒排索引。而且,不像其他多数的数据库,它能在 同一个查询中 使用所有这些倒排索引,并以惊人的速度返回结果。

文档

一个对象仅仅是类似于 hash 、 hashmap 、字典或者关联数组的 JSON 对象,对象中也可以嵌套其他的对象。 对象可能包含了另外一些对象。

在 Elasticsearch 中,术语 文档 有着特定的含义。它是指最顶层或者根对象, 这个根对象被序列化成 JSON 并存储到 Elasticsearch 中,指定了唯一 ID。

文档元数据
_index:一个 索引 应该是因共同的特性被分组到一起的文档集合。类比数据库
_type:在索引中对数据进行逻辑分区。类比数据表
_id:当它和 _index 以及 _type 组合就可以唯一确定 Elasticsearch 中的一个文档。类比数据唯一标识

写数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT /{index}/{type}/{id}
{
"field": "value",
...
}

# For Example

curl -X PUT "10.96.83.188:9200/website/blog/123" -H 'Content-Type: application/json' -d'
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
'
{"_index":"website","_type":"blog","_id":"123","_version":1,"result":"created",
"_shards":{"total":2,"successful":1,"failed":0},"created":true}

# 如果没有指定ID,Elasticsearch 可以帮我们自动生成 ID
curl -X POST "10.96.83.188:9200/website/blog/" -H 'Content-Type: application/json' -d'
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2014/01/01"
}
'
{"_index":"website","_type":"blog","_id":"AWoPmj2eBvBN4YG5RXks","_version":1,
"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

读数据
pretty 为了让输出更友好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#取所有字段
curl -X GET "10.96.83.188:9200/website/blog/123?pretty"
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
"title" : "My first blog entry",
"text" : "Just trying this out...",
"date" : "2014/01/01"
}
}

# 取部分字段
curl -X GET "10.96.83.188:9200/website/blog/123?_source=title,text&pretty"
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
"text" : "Just trying this out...",
"title" : "My first blog entry"
}
}

# 只取数据,不去元数据
curl -X GET "10.96.83.188:9200/website/blog/123/_source"

{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}

# 读取多个数据
curl -X GET "10.96.83.188:9200/_mget?pretty" -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : 1
},
{
"_index" : "website",
"_type" : "blog",
"_id" : 124
}
]
}
'

判断文档是否存在(200存在,404不存在)

1
2
3
4
5
6
7
8
9
$ curl -I -XHEAD "10.96.83.188:9200/website/blog/123/"
HTTP/1.1 200 OK
content-type: text/plain; charset=UTF-8
content-length: 0

$ curl -I -XHEAD "10.96.83.188:9200/website/blog/124/"
HTTP/1.1 404 Not Found
content-type: text/plain; charset=UTF-8
content-length: 0

更新操作

创建新文档

要么id保证唯一,要么不带id新建,由ES自动创建
创建时,带上_create,保证只新增,如果已存在则报错

1
2
3
4
5
6
7
8
9
curl -X PUT "10.96.83.188:9200/website/blog/123/_create" -H 'Content-Type: application/json' -d'
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}
'

{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[blog][123]: version conflict, document already exists (current version [2])","index_uuid":"_sBTT-DdSnKytaOhbXEYfw","shard":"0","index":"website"}],"type":"version_conflict_engine_exception","reason":"[blog][123]: version conflict, document already exists (current version [2])","index_uuid":"_sBTT-DdSnKytaOhbXEYfw","shard":"0","index":"website"},"status":409}

删除文档

1
2
curl -X DELETE "10.96.83.188:9200/website/blog/123"
{"found":true,"_index":"website","_type":"blog","_id":"123","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0}}

更新部分文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
curl -X POST "10.96.83.188:9200/website/blog/1/_update" -H 'Content-Type: application/json' -d'
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
'

curl -X GET "10.96.83.188:9200/website/blog/1?pretty"
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"title" : "My first blog entry",
"text" : "Starting to get the hang of this...",
"views" : 0,
"tags" : [
"testing"
]
}
}

乐观并发控制

本文标题:Elastic Search 分布式特性

文章作者:Craze lee

发布时间:2019年04月11日 - 18:04

最后更新:2019年04月12日 - 14:04

原始链接:http://craze-lee.github.io/2019/04/11/ElasticSearch/ES数据输入与输出/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

您的支持将鼓励我继续创作!