ElasticSearch入门相关API实例

查询

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=节点名称

参考:添加链接描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值