Elasticsearch常用操作API

简介:

1. 查询所有的索引

1
2
3
4
[root@Server01 cx] # curl '10.0.0.5:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size 
yellow  open    customer   5   1          2            0      6.6kb          6.6kb 
yellow  open    bank       5   1       1000            0    442.1kb        442.1kb


2. 查询bank表中的所有数据

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
[root@Server01 cx] # curl '10.0.0.5:9200/bank/_search?q=*&pretty'
{
   "took"  : 41,
   "timed_out"  :  false ,
   "_shards"  : {
     "total"  : 5,
     "successful"  : 5,
     "failed"  : 0
   },
   "hits"  : {
     "total"  : 1000,
     "max_score"  : 1.0,
     "hits"  : [ {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "25" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 25,
         "balance"  : 40540,
         "firstname"  :  "Virginia" ,
         "lastname"  :  "Ayala" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "171 Putnam Avenue" ,
         "employer"  :  "Filodyne" ,
         "email"  :  "virginiaayala@filodyne.com" ,
         "city"  :  "Nicholson" ,
         "state"  :  "PA"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "44" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 44,
         "balance"  : 34487,
         "firstname"  :  "Aurelia" ,
         "lastname"  :  "Harding" ,
         "age"  : 37,
         "gender"  :  "M" ,
         "address"  :  "502 Baycliff Terrace" ,
         "employer"  :  "Orbalix" ,
         "email"  :  "aureliaharding@orbalix.com" ,
         "city"  :  "Yardville" ,
         "state"  :  "DE"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "99" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 99,
         "balance"  : 47159,
         "firstname"  :  "Ratliff" ,
         "lastname"  :  "Heath" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "806 Rockwell Place" ,
         "employer"  :  "Zappix" ,
         "email"  :  "ratliffheath@zappix.com" ,
         "city"  :  "Shaft" ,
         "state"  :  "ND"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "119" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 119,
         "balance"  : 49222,
         "firstname"  :  "Laverne" ,
         "lastname"  :  "Johnson" ,
         "age"  : 28,
         "gender"  :  "F" ,
         "address"  :  "302 Howard Place" ,
         "employer"  :  "Senmei" ,
         "email"  :  "lavernejohnson@senmei.com" ,
         "city"  :  "Herlong" ,
         "state"  :  "DC"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "126" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 126,
         "balance"  : 3607,
         "firstname"  :  "Effie" ,
         "lastname"  :  "Gates" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "620 National Drive" ,
         "employer"  :  "Digitalus" ,
         "email"  :  "effiegates@digitalus.com" ,
         "city"  :  "Blodgett" ,
         "state"  :  "MD"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "145" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 145,
         "balance"  : 47406,
         "firstname"  :  "Rowena" ,
         "lastname"  :  "Wilkinson" ,
         "age"  : 32,
         "gender"  :  "M" ,
         "address"  :  "891 Elton Street" ,
         "employer"  :  "Asimiline" ,
         "email"  :  "rowenawilkinson@asimiline.com" ,
         "city"  :  "Ripley" ,
         "state"  :  "NH"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "183" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 183,
         "balance"  : 14223,
         "firstname"  :  "Hudson" ,
         "lastname"  :  "English" ,
         "age"  : 26,
         "gender"  :  "F" ,
         "address"  :  "823 Herkimer Place" ,
         "employer"  :  "Xinware" ,
         "email"  :  "hudsonenglish@xinware.com" ,
         "city"  :  "Robbins" ,
         "state"  :  "ND"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "190" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 190,
         "balance"  : 3150,
         "firstname"  :  "Blake" ,
         "lastname"  :  "Davidson" ,
         "age"  : 30,
         "gender"  :  "F" ,
         "address"  :  "636 Diamond Street" ,
         "employer"  :  "Quantasis" ,
         "email"  :  "blakedavidson@quantasis.com" ,
         "city"  :  "Crumpler" ,
         "state"  :  "KY"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "208" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 208,
         "balance"  : 40760,
         "firstname"  :  "Garcia" ,
         "lastname"  :  "Hess" ,
         "age"  : 26,
         "gender"  :  "F" ,
         "address"  :  "810 Nostrand Avenue" ,
         "employer"  :  "Quiltigen" ,
         "email"  :  "garciahess@quiltigen.com" ,
         "city"  :  "Brooktrails" ,
         "state"  :  "GA"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "222" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 222,
         "balance"  : 14764,
         "firstname"  :  "Rachelle" ,
         "lastname"  :  "Rice" ,
         "age"  : 36,
         "gender"  :  "M" ,
         "address"  :  "333 Narrows Avenue" ,
         "employer"  :  "Enaut" ,
         "email"  :  "rachellerice@enaut.com" ,
         "city"  :  "Wright" ,
         "state"  :  "AZ"
       }
     } ]
   }
}
  • took –  Elasticsearch执行查询的毫秒响应时间

  • timed_out – 是否超时

  • _shards – 查询过的分片,包含成功的分片和失败的分片

  • hits – 搜索结果

  • hits.total – 符合搜索结果的记录数

  • hits.hits – 默认显示前10条搜索结果

  • _score and max_score - ignore these fields for now


3. 另外一种查询全部结果的方法

1
2
3
4
curl -XPOST  '10.0.0.5:9200/bank/_search?pretty'  -d '
{
   "query" : {  "match_all" : {} }
}'


4. 分页排序的检索例子

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
[root@Server01 cx] # curl -XPOST '10.0.0.5:9200/bank/_search?pretty' -d '
{
   "query" : {  "match_all" : {} },
   "from" : 10,
   "size" : 3,
   "sort" : {  "balance" : {  "order" :  "desc"  } }
}'
{
   "took"  : 8,
   "timed_out"  :  false ,
   "_shards"  : {
     "total"  : 5,
     "successful"  : 5,
     "failed"  : 0
   },
   "hits"  : {
     "total"  : 1000,
     "max_score"  : null,
     "hits"  : [ {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "255" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 255,
         "balance"  : 49339,
         "firstname"  :  "Iva" ,
         "lastname"  :  "Rivers" ,
         "age"  : 38,
         "gender"  :  "M" ,
         "address"  :  "470 Rost Place" ,
         "employer"  :  "Mantrix" ,
         "email"  :  "ivarivers@mantrix.com" ,
         "city"  :  "Disautel" ,
         "state"  :  "MD"
       },
       "sort"  : [ 49339 ]
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "524" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 524,
         "balance"  : 49334,
         "firstname"  :  "Salas" ,
         "lastname"  :  "Farley" ,
         "age"  : 30,
         "gender"  :  "F" ,
         "address"  :  "499 Trucklemans Lane" ,
         "employer"  :  "Xumonk" ,
         "email"  :  "salasfarley@xumonk.com" ,
         "city"  :  "Noxen" ,
         "state"  :  "AL"
       },
       "sort"  : [ 49334 ]
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "751" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 751,
         "balance"  : 49252,
         "firstname"  :  "Patrick" ,
         "lastname"  :  "Osborne" ,
         "age"  : 23,
         "gender"  :  "M" ,
         "address"  :  "915 Prospect Avenue" ,
         "employer"  :  "Gynko" ,
         "email"  :  "patrickosborne@gynko.com" ,
         "city"  :  "Takilma" ,
         "state"  :  "MO"
       },
       "sort"  : [ 49252 ]
     } ]
   }
}


5. 添加文档,如果id存在则更新文档

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
[root@Server01 data] # curl -XPUT '10.0.0.5:9200/customer/external/1?pretty' -d '
> { "name" : "John Doe" }'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 1,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  true
}
[root@Server01 data] # curl -XPUT '10.0.0.5:9200/customer/external/1?pretty' -d '
{ "name" : "Jane Doe" }'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 2,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  false
}


6. 不指定Id时创建文档,可以发现自动生成一个随机Id

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external?pretty' -d '{"name": "Jane Doe"}'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "AVTh53KZ4Pj5B2ZQ2voK" ,
   "_version"  : 1,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  true
}


7. 修改文档

1
2
3
4
5
6
7
8
9
10
11
12
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/2/_update?pretty' -d '{"doc":{"name":"favourite boy","age":18}}'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "2" ,
   "_version"  : 3,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   }
}


8. 在开启javascript后,可以使用JS脚本更新文档

1
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/2/_update?pretty' -d '{"script":"ctx._source.age+=5"}'


9. 删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Server01 data] # curl -XDELETE '10.0.0.5:9200/customer/external/1?pretty'
{
   "found"  :  true ,
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 4,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   }
}


10. 批量更新文档

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
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/_bulk?pretty' -d '
{ "index" :{ "_id" : "1" }}
{ "name" :  "John Doe"  }
{ "index" :{ "_id" : "2" }}
{ "name" :  "Jane Doe"  }'
{
   "took"  : 33,
   "errors"  :  false ,
   "items"  : [ {
     "index"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "1" ,
       "_version"  : 3,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200
     }
   } ]
}
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/_bulk?pretty' -d '
> { "update" :{ "_id" : "1" }}
> { "doc" : {  "name" :  "John Doe becomes Jane Doe"  } }
> { "delete" :{ "_id" : "2" }}
> '
{
   "took"  : 34,
   "errors"  :  false ,
   "items"  : [ {
     "update"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "1" ,
       "_version"  : 4,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200
     }
   }, {
     "delete"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "2" ,
       "_version"  : 5,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200,
       "found"  :  true
     }
   } ]
}

1. 查询所有的索引

1
2
3
4
[root@Server01 cx] # curl '10.0.0.5:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size 
yellow  open    customer   5   1          2            0      6.6kb          6.6kb 
yellow  open    bank       5   1       1000            0    442.1kb        442.1kb


2. 查询bank表中的所有数据

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
[root@Server01 cx] # curl '10.0.0.5:9200/bank/_search?q=*&pretty'
{
   "took"  : 41,
   "timed_out"  :  false ,
   "_shards"  : {
     "total"  : 5,
     "successful"  : 5,
     "failed"  : 0
   },
   "hits"  : {
     "total"  : 1000,
     "max_score"  : 1.0,
     "hits"  : [ {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "25" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 25,
         "balance"  : 40540,
         "firstname"  :  "Virginia" ,
         "lastname"  :  "Ayala" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "171 Putnam Avenue" ,
         "employer"  :  "Filodyne" ,
         "email"  :  "virginiaayala@filodyne.com" ,
         "city"  :  "Nicholson" ,
         "state"  :  "PA"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "44" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 44,
         "balance"  : 34487,
         "firstname"  :  "Aurelia" ,
         "lastname"  :  "Harding" ,
         "age"  : 37,
         "gender"  :  "M" ,
         "address"  :  "502 Baycliff Terrace" ,
         "employer"  :  "Orbalix" ,
         "email"  :  "aureliaharding@orbalix.com" ,
         "city"  :  "Yardville" ,
         "state"  :  "DE"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "99" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 99,
         "balance"  : 47159,
         "firstname"  :  "Ratliff" ,
         "lastname"  :  "Heath" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "806 Rockwell Place" ,
         "employer"  :  "Zappix" ,
         "email"  :  "ratliffheath@zappix.com" ,
         "city"  :  "Shaft" ,
         "state"  :  "ND"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "119" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 119,
         "balance"  : 49222,
         "firstname"  :  "Laverne" ,
         "lastname"  :  "Johnson" ,
         "age"  : 28,
         "gender"  :  "F" ,
         "address"  :  "302 Howard Place" ,
         "employer"  :  "Senmei" ,
         "email"  :  "lavernejohnson@senmei.com" ,
         "city"  :  "Herlong" ,
         "state"  :  "DC"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "126" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 126,
         "balance"  : 3607,
         "firstname"  :  "Effie" ,
         "lastname"  :  "Gates" ,
         "age"  : 39,
         "gender"  :  "F" ,
         "address"  :  "620 National Drive" ,
         "employer"  :  "Digitalus" ,
         "email"  :  "effiegates@digitalus.com" ,
         "city"  :  "Blodgett" ,
         "state"  :  "MD"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "145" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 145,
         "balance"  : 47406,
         "firstname"  :  "Rowena" ,
         "lastname"  :  "Wilkinson" ,
         "age"  : 32,
         "gender"  :  "M" ,
         "address"  :  "891 Elton Street" ,
         "employer"  :  "Asimiline" ,
         "email"  :  "rowenawilkinson@asimiline.com" ,
         "city"  :  "Ripley" ,
         "state"  :  "NH"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "183" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 183,
         "balance"  : 14223,
         "firstname"  :  "Hudson" ,
         "lastname"  :  "English" ,
         "age"  : 26,
         "gender"  :  "F" ,
         "address"  :  "823 Herkimer Place" ,
         "employer"  :  "Xinware" ,
         "email"  :  "hudsonenglish@xinware.com" ,
         "city"  :  "Robbins" ,
         "state"  :  "ND"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "190" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 190,
         "balance"  : 3150,
         "firstname"  :  "Blake" ,
         "lastname"  :  "Davidson" ,
         "age"  : 30,
         "gender"  :  "F" ,
         "address"  :  "636 Diamond Street" ,
         "employer"  :  "Quantasis" ,
         "email"  :  "blakedavidson@quantasis.com" ,
         "city"  :  "Crumpler" ,
         "state"  :  "KY"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "208" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 208,
         "balance"  : 40760,
         "firstname"  :  "Garcia" ,
         "lastname"  :  "Hess" ,
         "age"  : 26,
         "gender"  :  "F" ,
         "address"  :  "810 Nostrand Avenue" ,
         "employer"  :  "Quiltigen" ,
         "email"  :  "garciahess@quiltigen.com" ,
         "city"  :  "Brooktrails" ,
         "state"  :  "GA"
       }
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "222" ,
       "_score"  : 1.0,
       "_source"  : {
         "account_number"  : 222,
         "balance"  : 14764,
         "firstname"  :  "Rachelle" ,
         "lastname"  :  "Rice" ,
         "age"  : 36,
         "gender"  :  "M" ,
         "address"  :  "333 Narrows Avenue" ,
         "employer"  :  "Enaut" ,
         "email"  :  "rachellerice@enaut.com" ,
         "city"  :  "Wright" ,
         "state"  :  "AZ"
       }
     } ]
   }
}
  • took –  Elasticsearch执行查询的毫秒响应时间

  • timed_out – 是否超时

  • _shards – 查询过的分片,包含成功的分片和失败的分片

  • hits – 搜索结果

  • hits.total – 符合搜索结果的记录数

  • hits.hits – 默认显示前10条搜索结果

  • _score and max_score - ignore these fields for now


3. 另外一种查询全部结果的方法

1
2
3
4
curl -XPOST  '10.0.0.5:9200/bank/_search?pretty'  -d '
{
   "query" : {  "match_all" : {} }
}'


4. 分页排序的检索例子

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
[root@Server01 cx] # curl -XPOST '10.0.0.5:9200/bank/_search?pretty' -d '
{
   "query" : {  "match_all" : {} },
   "from" : 10,
   "size" : 3,
   "sort" : {  "balance" : {  "order" :  "desc"  } }
}'
{
   "took"  : 8,
   "timed_out"  :  false ,
   "_shards"  : {
     "total"  : 5,
     "successful"  : 5,
     "failed"  : 0
   },
   "hits"  : {
     "total"  : 1000,
     "max_score"  : null,
     "hits"  : [ {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "255" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 255,
         "balance"  : 49339,
         "firstname"  :  "Iva" ,
         "lastname"  :  "Rivers" ,
         "age"  : 38,
         "gender"  :  "M" ,
         "address"  :  "470 Rost Place" ,
         "employer"  :  "Mantrix" ,
         "email"  :  "ivarivers@mantrix.com" ,
         "city"  :  "Disautel" ,
         "state"  :  "MD"
       },
       "sort"  : [ 49339 ]
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "524" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 524,
         "balance"  : 49334,
         "firstname"  :  "Salas" ,
         "lastname"  :  "Farley" ,
         "age"  : 30,
         "gender"  :  "F" ,
         "address"  :  "499 Trucklemans Lane" ,
         "employer"  :  "Xumonk" ,
         "email"  :  "salasfarley@xumonk.com" ,
         "city"  :  "Noxen" ,
         "state"  :  "AL"
       },
       "sort"  : [ 49334 ]
     }, {
       "_index"  :  "bank" ,
       "_type"  :  "account" ,
       "_id"  :  "751" ,
       "_score"  : null,
       "_source"  : {
         "account_number"  : 751,
         "balance"  : 49252,
         "firstname"  :  "Patrick" ,
         "lastname"  :  "Osborne" ,
         "age"  : 23,
         "gender"  :  "M" ,
         "address"  :  "915 Prospect Avenue" ,
         "employer"  :  "Gynko" ,
         "email"  :  "patrickosborne@gynko.com" ,
         "city"  :  "Takilma" ,
         "state"  :  "MO"
       },
       "sort"  : [ 49252 ]
     } ]
   }
}


5. 添加文档,如果id存在则更新文档

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
[root@Server01 data] # curl -XPUT '10.0.0.5:9200/customer/external/1?pretty' -d '
> { "name" : "John Doe" }'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 1,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  true
}
[root@Server01 data] # curl -XPUT '10.0.0.5:9200/customer/external/1?pretty' -d '
{ "name" : "Jane Doe" }'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 2,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  false
}


6. 不指定Id时创建文档,可以发现自动生成一个随机Id

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external?pretty' -d '{"name": "Jane Doe"}'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "AVTh53KZ4Pj5B2ZQ2voK" ,
   "_version"  : 1,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   },
   "created"  :  true
}


7. 修改文档

1
2
3
4
5
6
7
8
9
10
11
12
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/2/_update?pretty' -d '{"doc":{"name":"favourite boy","age":18}}'
{
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "2" ,
   "_version"  : 3,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   }
}


8. 在开启javascript后,可以使用JS脚本更新文档

1
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/2/_update?pretty' -d '{"script":"ctx._source.age+=5"}'


9. 删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Server01 data] # curl -XDELETE '10.0.0.5:9200/customer/external/1?pretty'
{
   "found"  :  true ,
   "_index"  :  "customer" ,
   "_type"  :  "external" ,
   "_id"  :  "1" ,
   "_version"  : 4,
   "_shards"  : {
     "total"  : 2,
     "successful"  : 1,
     "failed"  : 0
   }
}


10. 批量更新文档

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
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/_bulk?pretty' -d '
{ "index" :{ "_id" : "1" }}
{ "name" :  "John Doe"  }
{ "index" :{ "_id" : "2" }}
{ "name" :  "Jane Doe"  }'
{
   "took"  : 33,
   "errors"  :  false ,
   "items"  : [ {
     "index"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "1" ,
       "_version"  : 3,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200
     }
   } ]
}
[root@Server01 data] # curl -XPOST '10.0.0.5:9200/customer/external/_bulk?pretty' -d '
> { "update" :{ "_id" : "1" }}
> { "doc" : {  "name" :  "John Doe becomes Jane Doe"  } }
> { "delete" :{ "_id" : "2" }}
> '
{
   "took"  : 34,
   "errors"  :  false ,
   "items"  : [ {
     "update"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "1" ,
       "_version"  : 4,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200
     }
   }, {
     "delete"  : {
       "_index"  :  "customer" ,
       "_type"  :  "external" ,
       "_id"  :  "2" ,
       "_version"  : 5,
       "_shards"  : {
         "total"  : 2,
         "successful"  : 1,
         "failed"  : 0
       },
       "status"  : 200,
       "found"  :  true
     }
   } ]
}






本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1793613,如需转载请自行联系原作者
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
存储 人工智能 自然语言处理
Elasticsearch Inference API增加对阿里云AI的支持
本文将介绍如何在 Elasticsearch 中设置和使用阿里云的文本生成、重排序、稀疏向量和稠密向量服务,提升搜索相关性。
945 14
Elasticsearch Inference API增加对阿里云AI的支持
|
存储 人工智能 API
(Elasticsearch)使用阿里云 infererence API 及 semantic text 进行向量搜索
本文展示了如何使用阿里云 infererence API 及 semantic text 进行向量搜索。
884 8
|
监控 API 索引
Elasticsearch集群使用 _cluster/health API
Elasticsearch集群使用 _cluster/health API
1012 2
|
Unix API 索引
Elasticsearch集群使用 _cat/health API
Elasticsearch集群使用 _cat/health API
557 1
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
2815 64
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
7129 0
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
2967 5
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
851 5
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
1751 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。

热门文章

最新文章