查询
curl -X PUT "localhost:9200/customer?pretty&pretty"
curl -X GET "localhost:9200/_cat/indices?v&pretty"
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
curl -X GET "localhost:9200/customer/_doc/1?pretty&pretty"
删除
curl -X DELETE "localhost:9200/customer?pretty&pretty"
## 查询
curl -X GET "localhost:9200/_cat/indices?v&pretty"
curl -X PUT "localhost:9200/ppp/_doc/1?pretty" -H 'Content-Type:application/json' -d'{"userName":"jackson"}'
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
curl -X PUT "localhost:9200/customer/_doc/2?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
更新
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe222" }
}
'
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type:application/json' -d'{"doc":{"name":"jackson","age":33}}'
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type:application/json' -d'{"script":"ctx._source.age+=5"}'
curl -X DELETE "localhost:9200/customer/_doc/2?pretty&pretty"
curl -X DELETE "localhost:9200/customer/_doc/1?pretty"
批量操作
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type:application/json' -d'
{"index":{"_id":"111"}}
{"name":"Tinny"}
{"index":{"_id":"22"}}
{"name":"Lilly","age":40}
'
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type:application/json' -d'
{"update":{"_id":"111"}}
{"doc":{"name":"Tiffiny","age":23}}
{"delete":{"_id":"22"}}
'
查询API
数据准备
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
通过_search的url请求方式
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty"
通过request body方式
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
'
引入查询语言
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} }
}
'
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"size": 1
}
'
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
'
返回部分字段
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
'
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
'
聚合过滤相关
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 5,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
'
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 3,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
'
设置集群名称与节点名称
elasticsearch-6.3.2/bin/elasticsearch -Ecluster.name=集群名称 -Enode.name=节点名称
参考:添加链接描述