1. 索引
1.1 创建索引
PUT /my_index?pretty
GET /_cat/indices?v
2. 写入
2.1 新增文档
PUT /my_index/_doc/1
{
"field_0" : "a",
"field_1" : ["red"]
}
2.2 更新单个文档
//普通更新
POST /my_index/_doc/1/_update
{
"doc" : {
"field_2" : "new_field"
}
}
//使用script更新
POST my_index/_doc/1/_update
{
"script" : {
"source": "ctx._source.field_1.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
//upsert
POST /my_index/_doc/2/_update
{
"doc" : {
"field_0" : "b"
},
"doc_as_upsert" : true
}
相关参数
routing: 控制文档分配到哪个shard, 或者父子文档中父文档的id
version: 可以用于事务控制, 例如防止更新结果被覆盖
refresh: 是否即时可见
_source: 控制返回数据的字段, 设为false不返回
2.3 批量更新
POST _bulk
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "my_index" }}
{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }
{ "update" : {"_id" : "2", "_type" : "_doc", "_index" : "my_index" }}
{ "doc" : {"field" : "value_2"}, "doc_as_upsert" : true }
3. 查询
3.1 URI查询
//查询所有索引
GET /_search?q=field_0:value_0&q=field_1:value_1
//指定索引
GET /my_index/_search?q=field_0:value_0
GET /my_index_0,my_index_1/_search?q=field_0:value_0
//获取评分规则
GET /_search?q=field_0:value_0&explain=true&size=1
//排序
GET /_search?sort=field_0:desc
// 当排序字段为text, 需要用
/_search?sort=field_0.keyword:desc
// 或者设置fielddata为true
PUT /my_index/_mapping/doc/
{
"properties": {
"field_0": {
"type": "text",
"fielddata": true
}
}
}
//分页
GET /_search?q=field_0:value_0&from=0&size=1
3.2 Query DSL
GET /my_index/my_type/_search
{
"query" : {
"term" : { "field_0" : "value_0" }
}
}