Elasticsearch 5.2 增删改查

16 篇文章 1 订阅
14 篇文章 1 订阅

把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"}}

【原创】原创文章,更多关注敬请关注微信公众号。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值