Elasticsearch常用API——Document APIs

一、文档API

1、增

使用PUT方法、指定索引名称、文档类型,文档id可指定也可以不指定,让系统自动生成。

PUT indexName/_type/id
{
    "xx" : "xxx",
    "xxx" : "xxx"
}
#curl:
curl -X PUT "localhost:9200/indexName/_type/id?pretty" -H 'Content-Type: application/json' -d'
{
     "xx" : "xxx",
    "xxx" : "xxx"
}

不指定id,让系统自动生成时, 需使用POST方法:

POST twitter/_doc/
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
#执行结果如下:
{
    "_shards" : {
        "total" : 2,
        "failed" : 0,
        "successful" : 2
    },
    "_index" : "twitter",
    "_type" : "_doc",
    "_id" : "W0tpsmIBdwcYyG50zbta",
    "_version" : 1,
    "_seq_no" : 0,
    "_primary_term" : 1,
    "result": "created"
}
#curl:
curl -X POST "localhost:9200/twitter/_doc/?pretty" -H 'Content-Type: application/json' -d'
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
'

强制新增时,可以在url后加op_type=create,此时如果系统中已存在该数据,会返回失败。

PUT twitter/_doc/1?op_type=create
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
#curl:
curl -X PUT "localhost:9200/twitter/_doc/1?op_type=create&pretty" -H 'Content-Type: application/json' -d'
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
2、删

使用DELETE 删除文档

DELETE /twitter/_doc/1

Delete By Query API

POST twitter/_delete_by_query
{
  "query": { 
    "match_all": {
      "message": "some message"
    }
  }
}
#删除全部
POST twitter/_delete_by_query
{
  "query": { 
    "match": {}
  }
}
#curl:
curl -X POST "localhost:9200/twitter/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}
'

3、改

可使用_update改变文档的内容,doc是使用特定文档更新内容,也支持使用脚本更新内容

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

使用脚本时是 “ ctx. ” 可用的字段有:_index, _type, _id, _version, _routing, and _now (the current timestamp).

#原文档:
PUT test/_doc/1
{
    "counter" : 1,
    "tags" : ["red"]
}
#改变原文档中counter
POST test/_doc/1/_update
{
    "script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}
#list文档添加值
POST test/_doc/1/_update
{
    "script" : {
        "source": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "blue"
        }
    }
}
#判断tag是 green则删除,否则什么也不做
POST test/_doc/1/_update
{
    "script" : {
        "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
        "lang": "painless",
        "params" : {
            "tag" : "green"
        }
    }
}
#添加字段
POST test/_doc/1/_update
{
    "script" : "ctx._source.new_field = 'value_of_new_field'"
}
#删除字段
POST test/_doc/1/_update
{
    "script" : "ctx._source.remove('new_field')"
}
#upsert 如果原文档不存在,则执行upsert 插入文档,如果存在则执行脚本
POST test/_doc/1/_update
{
    "script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}
4、查

使用GET 查看索引中文档内容

GET twitter/_doc/0
#文档结果会显示在"_source"字段下
{
    "_index" : "twitter",
    "_type" : "_doc",
    "_id" : "0",
    "_version" : 1,
    "_seq_no" : 10,
    "_primary_term" : 1,
    "found": true,
    "_source" : {
        "user" : "kimchy",
        "date" : "2009-11-15T14:12:12",
        "likes": 0,
        "message" : "trying out Elasticsearch"
    }
}
#仅看"_source"字段
GET twitter/_doc/0/_source
#curl:
curl -X GET "localhost:9200/twitter/_doc/0?pretty"

也可以使用HEAD 验证文档是否存在

HEAD twitter/_doc/0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值