elasticsearch之文档APIs

1.创建文档

1.1 文档和索引一样是可以不需要预先定义的,可以动态写入。

  • action.auto_create_index:是否自动创建索引
  • index.mapper.dynamic :是否自动创建mapping,mapping中是包含type的。

1.2 version

  • 如果写入不指定版本,则es不会进行版本验证
  • 版本默认从1开始,以后每次递增1
  • 版本类型分为:internal:提供的版本和存储的版本相同才可以执行;external或者external_gt提供的版本大于存储的版本才可以执行;external_gte:提供的版本大于或等于存储的版本才可以执行,该情况慎用,可能丢失数据。
  • 如果指定内部版本,一般要先获取原来版本,再更新(注意:更新时版本是查询的版本,会从和es当前版本是否相同判断是否版本冲突)
  • 使用外部版本(例如版本信息由mysql维护),则版本范围是[0, 9.2e+18],是否成功判断的方式是:提供的版本号是否大于当前的版本号。
PUT twitter/tweet/1?version=2
{
    "message" : "elasticsearch now has versioning support, double cool!"
}

1.3 operation type
主要用于文档的id不存在才允许创建,下例如果文档id:1已经存在创建失败。

PUT twitter/tweet/1/_create
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
或者
PUT twitter/tweet/1?op_type=create
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

1.4 id自动生成
文档的id可以不显示提供,可以自动生成。

POST twitter/tweet/
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

1.5 显示指定routing
默认根据文档id进行hash之后分配route

POST twitter/tweet?routing=kimchy
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

1.6 Timeout

PUT twitter/tweet/1?timeout=5m
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

2.GET API

GET twitter/tweet/0

get api是实时的,当数据还没有refresh,get api会先触发一次refresh,让数据具有可见性。
获取局部字段:

GET twitter/_search
{
  "_source": {
    "includes" : ["name", "addr"]
  }
}
或者
GET twitter/_search
{
  "_source": ["name", "addr"]
}

获取除去字段其他内容:

GET twitter/_search
{
  "_source": {
    "excludes" : ["name", "addr"]
  }
}

3. DELETE API

和创建类似

DELETE /twitter/tweet/1

4. delete by query

根据条件删除

POST twitter/_delete_by_query
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}

上面的操作可能因为版本冲突导致删除失败,如果是批次删除则是遇到失败之后的删除都是失败的,已经执行的则成功删除。为了阻止因为版本冲突导致后面删除失败,可以按照下面方式删除

POST twitter/tweet/_delete_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  }
}

5. 更新操作

部分更新

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}

插入更新

POST test/type1/1/_update
{
   "doc" : {
        "name" : "new_name"
    }
    "upsert" : {
        "counter" : 1
    }
}

6. update by query

根据自己的查询条件更新es值:把incDay统一改成2021-09-23

低版本es
POST taobao/_update_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  },
  "script" : {
    "lang" : "painless",
    "inline" : "ctx._source.incDay=params.day",
    "params": {
      "day":"2021-09-23"
    }
  }
}
高版本es
POST taobao/_update_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  },
  "script" : {
    "lang" : "painless",
    "source" : "ctx._source.incDay=params.day",
    "params":{
      "day":"2021-09-23"
    }
  }
}

7. Bulk api

POST _bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

批量更新

POST _bulk
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"} }
{ "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
{ "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
{ "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }
{ "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "_source" : true} }
{ "doc" : {"field" : "value"} }
{ "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }
{ "doc" : {"field" : "value"}, "_source": true}

8. reindex api

reindex 不会复制源索引的setting等配置项,所以在reindex之前要先对目标索引进行setting设置。

POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "tweet",
    "query": {
      "term": {
        "user": "kimchy"
      }
    }
  },
  "dest": {
    "index": "new_twitter"
  }
}

9. ?refresh

设置更改是否refresh

  • true 默认值:立即可见
  • false :不需要立即可见
  • wait_for :等待自动刷新(默认值1s一次)
PUT /test/test/4?refresh=wait_for
{"test": "test"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值