elasticsearch restful API

基本状态管理

  1. 查看集群
curl -X GET "localhost:9200/_cat/health?v&pretty"
  1. 查看节点
curl -X GET "localhost:9200/_cat/nodes?v&pretty"
  1. 查看索引
curl -X GET "localhost:9200/_cat/indices?v&pretty"

增删改操作

  1. 创建索引
    customer为索引名称
curl -X PUT "localhost:9200/customer?pretty&pretty"
curl -X GET "localhost:9200/_cat/indices?v&pretty"
  1. 创建文档
    文档名称为doc,id为1,文档内容通过-d选项指定
curl -X PUT "localhost:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
  1. 查询文档内容
    通过指定的文档名称=doc和id=1查询指定文档
curl -X GET "localhost:9200/customer/doc/1?pretty&pretty"
  1. 删除文档
    通过指定的文档名称=doc和id=1删除指定文档
curl -X DELETE "localhost:9200/customer/doc/1?pretty&pretty"
  1. 删除索引
    customer为索引名称
curl -X DELETE "localhost:9200/customer?pretty&pretty"
  1. 修改文档内容
    通过指定的文档名称=doc和id=1修改文档内容,这里修改了键name对应的value
curl -X PUT "localhost:9200/customer?pretty"
curl -X PUT "localhost:9200/customer/doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Tim Doe"
}
'

也可以在文档内插入新的key-value,使用_update

curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe", "age": 20 }
}
'

通过ctx修改文档内容,使用_update,修改age对应的值,_source表示文档内容

curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'
  1. 批处理
    使用_bulk API实现增删改的批处理,下例为更新id为1的文档name字段的值,删除id为2的文档
curl -X POST "localhost:9200/customer/doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'

数据查询

  1. 从json导入数据集到文档
    导入文档名称accouts.json, 导入索引bank,文档accout,使用_bulk API,导入时如果索引或者文档不存在则创建相应的索引和文档
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
  1. match_all
    使用_search API进行搜索,需要指定搜索的索引,query键指定搜索条件,match_all代表无条件匹配,sort指定输出排序方式,注意,elasticsearch不会存储搜索结果,是无状态的
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}
'

指定搜索的范围,使用from和size

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}
'

使用sort指定按某个键(这里是banlance)进行排序,order指定是升序(asc)还是降序(desc)

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}
'

过滤字段,_source指定需要输出的字段

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}
'
  1. match
    match指定搜索条件,这里是搜索account_number为20的record
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match": { "account_number": 20 } }
}
'

这里的搜索条件是包含,即address需要包含mill或者lane的record

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match": { "address": "mill lane" } }
}
'
  1. bool
    使用must指定多个查询条件(每个查询条件使用must),返回满足所有查询条件的record
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
'

使用should返回至少满足一个搜索条件的记录

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
'

使用must_not指定同时不满足查询条件的查询结果

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
'
  1. filter
    使用filter指定过滤条件,如range指定过滤条件的范围
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}
'
  1. 分组
    使用aggs进行信息聚合处理,group_by_state指定分组的keyword,下例按照年龄进行分组,并在每个组中按性别再次分组,计算每个年龄段男女的平均薪水,aggs应该作为group_by_key的value
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}
'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值