路径及方式 | value |
---|---|
GET / _cat / health?v | 健康检查 |
GET / _cat / nodes?v | 获取节点列表 |
GET / _cat / indices?v | 获取索引列表 |
PUT /customer?pretty | 建立名为 customer的索引 |
为文档添加内容使用postman时注意body,json格式html
PUT /customer/_doc/1?pretty { "name": "John Doe"}
路径及方式 | value |
---|---|
GET /customer/_doc/1?pretty | 查询索引名称为customer的_doc文档内容 |
DELETE /customer?pretty | 删除索引 |
POST /customer/_doc/1/_update?pretty | 更新文档 |
DELETE /customer/_doc/2?pretty | 删除文档 |
此示例显示如何经过将名称字段更改成“Jane Doe”来更新咱们之前的文档(ID为1),同时向其添加年龄字段:node
POST / customer / _doc / 1 / _update?pretty { “doc”:{“name”:“Jane Doe”,“age”:20} }
也可使用简单脚本执行更新。此示例使用脚本将年龄增长5:web
POST / customer / _doc / 1 / _update?pretty { “script”:“ctx._source.age + = 5” }
在上面的示例中,ctx._source指的是即将更新的当前源文档。json
Elasticsearch提供了在给定查询条件(如SQL UPDATE-WHERE语句)的状况下更新多个文档的功能。请参阅docs-update-by-query api
批处理:
在一个批量操做中索引两个文档(ID1-john doe和id2-jane doe):api
POST /customer/_doc/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
此示例更新第一个文档(ID为1),而后在一个批量操做中删除第二个文档(ID为2):数组
POST /customer/_doc/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
注:删除只须要删除id便可
加载模板数据app
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"
search api:
在bank索引中搜索,q=*参数指示Elasticsearch匹配索引中的全部文档,sort=account_number:asc参数表示使用account_number每一个文档的字段以升序对结果进行排序,pretty表示返回json结果curl
GET / bank / _search?q = *&sort = account_number:asc&pretty
与上面彻底相同的结果elasticsearch
GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] }
结果值 | 意义 |
---|---|
took | Elasticsearch执行搜索的时间(以毫秒为单位) |
timed_out | 搜索是否超时 |
_shards | 告诉咱们搜索了多少个分片,以及搜索成功/失败分片的计数 |
hits | 搜索结果 |
hits.total | 符合咱们搜索条件的文档总数 |
hits.hits | 实际的搜索结果数组(默认为前10个文档) |
hits.sort | 对结果进行排序键(若是按分数排序则丢失) |
hits._score和max_score | 暂时忽略这些字段 |
查询语句ide
GET /bank/_search { "query": { "match_all": {} }, "from": 10, "size": 1 "sort":{“balance”:{“order”:“desc”}} "_source":{"account_number","balance"} }
query为查询定义 ,match_all(查询类型)在指定索引的全部文件搜索,size默认为10(文件数量),from起始位置(默认为0),sort对balance(非系统字段,此字段根据本身实际状况自行使用)进行降序排序(默认大小10个文档),_source是简化了的_source字段。它将只返回一个包含account_number和balance在内_sourced字段
返回编号为20的帐号:
GET / bank / _search { “query”:{“match”:{“account_number”:20}} }
返回地址中包含“mill”的全部帐号:
GET / bank / _search { “query”:{“match”:{“address”:“mill”}} }
返回地址中包含“mill”或“lane”的全部帐号:
GET / bank / _search { “query”:{“match”:{“address”:“mill lane”}} }
bool使用布尔逻辑将较小的查询组成两个match并返回“mill”和“lane”的全部帐号:
bool must子句指定必须为true才能将文档视为匹配的全部查询,must_not则是取不包含两个关键字的帐号
GET / bank / _search { “query”:{ “bool”:{ “must”:[ {“match”:{“address”:“mill”}}, {“match”:{“address”:“lane” }} ] } } }