Operate on Elasticsearch 2.2

Server environment

1
2
3
4
5
6
7
8
$ cat /etc/issue
Ubuntu 14.04.5 LTS

$ curl localhost:9200
{
"version" : {
"number" : "2.2.0",
...

Concept

Elasticsearch Relational DB
Indices Databases
Types Tables
Documents Rows
Fields Columns

API Request

Request format:

1
curl -X <VERB '<PROTOCOL://<HOST:<PORT/<PATH?<QUERY_STRING' -d '<BODY'
Parameters Description
VERB HTTP Method : GET、 POST、 PUT、 HEAD or DELETE
PROTOCOL http or https
HOST node names in the cluster
PORT Port. Default is 9200
PATH API Path
QUERY_STRING (Optional) Query string parameters
BODY (Optional) Request JSON body

Index document

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
$ curl -X PUT 'http://localhost:9200/company/employee/1?pretty' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love swimming",
"interests": [ "sports", "music" ]
}'
{
"_index" : "company",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}


$ curl -X PUT 'http://localhost:9200/company/employee/2?pretty' -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like shopping",
"interests": [ "music" ]
}'
{
"_index" : "company",
"_type" : "employee",
"_id" : "2",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}

$ curl -X PUT 'http://localhost:9200/company/employee/3?pretty' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like cooking and swimming",
"interests": [ "cooking" ,"swimming"]
}'
{
"_index" : "company",
"_type" : "employee",
"_id" : "3",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}

Find document

Get ID 1 Document:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ curl -X GET 'http://localhost:9200/company/employee/1?pretty'
{
"_index" : "company",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love swimming",
"interests" : [ "sports", "music" ]
}
}
Search employee information whose last name is ‘Smith’:
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
$ curl -X GET 'http://localhost:9200/company/employee/_search?q=last_name:Smith&pretty'
{
"took" : 97,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "company",
"_type" : "employee",
"_id" : "2",
"_score" : 0.30685282,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like shopping",
"interests" : [ "music" ]
}
}, {
"_index" : "company",
"_type" : "employee",
"_id" : "1",
"_score" : 0.30685282,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love swimming",
"interests" : [ "sports", "music" ]
}
} ]
}
}
  • The search value must match exactly except case
Search employee information using query expressions whose last name is ‘Smith’:
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
$ curl -X GET 'http://localhost:9200/company/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"last_name" : "smith"
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "company",
"_type" : "employee",
"_id" : "2",
"_score" : 0.30685282,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like shopping",
"interests" : [ "music" ]
}
}, {
"_index" : "company",
"_type" : "employee",
"_id" : "1",
"_score" : 0.30685282,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love swimming",
"interests" : [ "sports", "music" ]
}
} ]
}
}
  • The search value must match exactly except case
Search employee information whose last name is ‘Smith’ and order than 30:
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
$ curl -X GET 'http://localhost:9200/company/employee/_search?pretty' -d '
{
"query" : {
"bool" : {
"must" : {
"match" : {
"last_name" : "Smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "company",
"_type" : "employee",
"_id" : "2",
"_score" : 0.30685282,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like shopping",
"interests" : [ "music" ]
}
} ]
}
}
  • The search value must match exactly except case
Full Search employee information who likes swimming:
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
$ curl -X GET 'http://localhost:9200/company/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"about" : "love swimming"
}
},
"highlight" : {
"fields" : {
"about" : {}
}
}
}
}'
{
"took" : 42,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2169777,
"hits" : [ {
"_index" : "company",
"_type" : "employee",
"_id" : "1",
"_score" : 0.2169777,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love swimming",
"interests" : [ "sports", "music" ]
},
"highlight" : {
"about" : [ "I <em>love</em> <em>swimming</em>" ]
}
}, {
"_index" : "company",
"_type" : "employee",
"_id" : "3",
"_score" : 0.019691018,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like cooking and swimming",
"interests" : [ "cooking", "swimming" ]
},
"highlight" : {
"about" : [ "I like cooking and <em>swimming</em>" ]
}
} ]
}
}
  • Replace “match” with “match_phrase” in above request can exactly matches the short word “swimming”
  • Add the “highlight” parameter at the same level in “query” to tag matching keywords in the result with the </ em> tag
Aggregation analyze of employee interests

Enable the analysis of relate field first

1
2
3
4
5
6
7
8
9
10
11
$ curl -X PUT 'http://localhost:9200/company/_mapping/employee?pretty' -d '
{
"properties": {
"interests": {
"type": "string"
}
}
}'
{
"acknowledged" : true
}

Query the aggregation result

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
$ curl -X GET 'http://localhost:9200/company/employee/_search?pretty' -d '
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}'
{
"took" : 95,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
...
]
},
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "music",
"doc_count" : 2
}, {
"key" : "cooking",
"doc_count" : 1
}, {
"key" : "sports",
"doc_count" : 1
}, {
"key" : "swimming",
"doc_count" : 1
} ]
}
}
}

Update document

Update the document which id is 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ curl -X PUT 'http://localhost:9200/company/employee/2?pretty' -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 33,
"about" : "I like shopping ,dancing and music",
"interests": [ "music" ]
}'
{
"_index" : "company",
"_type" : "employee",
"_id" : "2",
"_version" : 2,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false
}

Delete document

1
2
3
4
5
6
7
8
9
10
11
12
13
$ curl -X DELETE 'http://localhost:9200/company/employee/3?pretty'
{
"found" : true,
"_index" : "company",
"_type" : "employee",
"_id" : "3",
"_version" : 3,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}

Thank you for reading.
This post is copyrighted by Liyuliang’s Blog.
If reproduced, please indicate the source: Liyuliang’s Blog
This blog uses Creative Commons Attribution-NonCommercial-Share-Sharing 4.0 International License Agreement to license.


Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×