elasticsearch quickstart

Environment Setup

1
2
3
4
5
6
7
8
9
10
11
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.6.0-linux-x86_64.tar.gz

# if you want to access kibana remotely, please update `server.host` to real IP in
# `kibana-6.6.0-linux-x86_64/config/kibana.yml` like 192.168.77.140

# You should run elasticsearch in non-root user
bin/elasticsearch -d
bin/kibana or `nohup bin/kibana &`

# now you can access http://192.168.77.140:5601

elasticsearch http verb

elasticsearch 按照HTTP协议下RestFul框架的交互格式就像这样:

1
2
3
4
POST /uri 创建
DELETE /uri/xxx 删除
PUT /uri/xxx 更新或创建
GET /uri/xxx 查看

sample case

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
PUT /customer?pretty

PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}

GET /customer/_doc/1?pretty

DELETE /customer?pretty
GET /_cat/indices?v

PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}

PUT /customer/_doc/1?pretty
{
"name": "Jane Doe"
}

PUT /customer/_doc/2?pretty
{
"name": "Jane Doe"
}

POST /customer/_doc?pretty
{
"name": "Jane Doe"
}

GET /customer/_search?q=Doe

GET /customer/_search
{
"query" : {
"match": { "name": "Doe" }
},
"highlight" : {
"fields" : {
"name" : {}
}
}
}

GET /_search
{
"query" : {
"match": { "name": "Doe" }
},
"highlight" : {
"fields" : {
"name" : {}
}
}
}

//这种更新是删除再增加,version变化了
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}

//这样更新是真的更新,version不变
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}

//get all index
curl "localhost:9200/_cat/indices?v"

curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
curl -X GET "localhost:9200/bank/_search?q=Fitzgerald&sort=account_number:asc&pretty"

//查询某个index
GET /twitter/_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}

// 查询所有的index
GET /_search
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "user" : "kimchy" }
}
}