把ES当做关系型数据库mysql,常用的增删改查+批处理语句。ES还是很牛逼的,不仅仅是存储,今天还发现了Graph Api,可以做指示图谱用。
查看和创建
GET /_cat/indices?v 查看全部索引PUT /customer?pretty 创建名为customer的索引==创建库database
GET /customer/_search?pretty 查询全部doc
GET /bank/_search?q=*&sort=account_number:asc&pretty //pretty 只是json格式
GET /bank/_search //等价于上面的
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" } //指定排序键sort=account_number; asc正序 desc倒序
]
}
GET /bank/_search
{
"query": { "match_all": {} },
"from": 10, //从第11个文档开始
"size": 10 //显示10条
}
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"] //where条件查询 只显示指定的两个字段
}
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } } //指定条件查询
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": { //过滤查询
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
GET /bank/_search
{
"size": 0,
"aggs": { //聚合Aggregations
"group_by_state": {
"terms": {
"field": "state.keyword" //指定关键词keyword=state
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
###############################################################################
insert插入数据
PUT /customer/external/1?pretty 插入到type=external(表),id=1的一个document;索引相当于库;如果不指定id,则生成一个随机id
{
"name": "John Doe"
}
GET /customer/external/1?pretty
POST /customer/external?pretty 不指定id插入doc,使用post方式
{
"name": "Jane Doe"
}
################################################################################
delete索引:
DELETE /customer?pretty
DELETE /customer/external/2?pretty
###############################################################################
update字段数据:
PUT /customer/external/1?pretty //version会自动+1
{
"name": "John eee"
}
################################################################################
Batch Processing批处理,使用bulk api
POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
【原创】原创文章,更多关注敬请关注微信公众号。