elasticsearch简单的CRUD操作

create

1
2
3
4
PUT /index/type/id
{
"key":"value"
}

例如:

1
2
3
4
5
6
7
PUT /ecommerce/product/1
{
"name":"高露洁牙膏",
"desc":"高效美白",
"price":30,
"tags":["美白","防蛀"]
}

delete

1
DELETE /index/type/id

例如:

1
DELETE /ecommerce/product/1

update

1
2
3
4
5
6
POST /index/type/id/_update
{
"doc": {
"key":"value"
}
}

例如:

1
2
3
4
5
6
POST /ecommerce/product/1/_update
{
"doc": {
"name":"佳洁士牙膏"
}
}

read

1
GET /index/type/id

例如:

1
GET /ecommerce/product/1

返回内容过滤

  1. 过滤_source
    1
    GET /index/type/_search?_source=false
1
2
3
4
5
6
7
GET /index/type/_search
{
"query": {
"match_all": {}
},
"_source": false
}
  1. 过滤_source中的部分字段
    1
    GET /index/type/_search?_source_include=key1,key2
1
2
3
4
5
6
7
8
9
GET /index/type/_search
{
"query": {
"match_all": {}
},
"_source": {
"include": ["key1","key2","key3.*"]
}
}
  1. 显示_source中的部分字段
    1
    GET /index/type/_search?_source_exclude=key3
1
2
3
4
5
6
7
8
9
GET /index/type/_search
{
"query": {
"match_all": {}
},
"_source": {
"exclude": ["key3"]
}
}
  1. 只返回_source
    1
    GET /index/type/id/_source

script

例如:

1
2
3
4
5
6
7
POST /ecommerce/product/1/_update
{
"script": {
"params": {"count":2},
"inline": "ctx._source.price += params.count"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GET /ecommerce/product/_search
{
"query": {
"bool": {
"must": [
{
"script":{
"script":{
"params": {"count":36},
"inline": "doc.price.value == params.count"
}
}
}
]
}
}
}
>