ElasticSearch 基本操作

_cat 操作

 # 查询所有的indeces
GET /_cat/indices?v
# 查看节点以及index健康情况
GET /_cat/health?v
#查看所有节点
GET /_cat/nodes?v
#查看所有的模板
GET /_cat/templates
#查看所有shards
GET /_cat/shards?v

数据的基本操作

customer 为索引名
# 创建索引
PUT /customer?pretty
# 删除索引
DELETE /customer?pretty
# 创建数据  其中1 为id,如果没有指定id,则随机生成一个Id
PUT /customer/_doc/1?pretty
{"name", "zwshao"}
# 根据ID查询数据
GET /customer/_doc/1?pretty
#查询操作
POST /bank_account/_search?pretty
{"query" : {"match_all": {}}, "size" : "3", "from": 0,"sort": { "balance": { "order": "desc" } }, "_source": ["account_number", "balance"]} 

_bulk 操作

bulk提供四种操作 index, create, update, delete

/_bulk?pretty
{"index": {"_index": "customer", "_id": "10" }}
{"name": "xiaoshao"}  ## 创建index 并且创建 id为10的数据
{"create": {"_index": "customer", "_id": "3"}}
{"name": "John Doe becomes Jane Doe"}   # 创建id为3的数据
{"update": {"_index": "customer", "_id": "4"}}
{"doc": {"name": "55"}} # 更新id为4 的数据,name在原来数据中必须存在
{"delete":{"_index": "customer", "_id": "2"}} # 删除id为2的数据

query中的match_all, match 和match_phase

GET /bank_account/_search?pretty
{"query": {"match_all": {}}}    ## 查询bank_account索引下的所有数据

{"query": {"match": {"des": "He is"}}  # # 查询索引下des字段中包含He 或者is的所有数据

{"query": {"match_phase": {"des" : "He is"}}} ## 查询索引下des字段包含“He is”这个组合的所有记录

组合查询 must, must_not, should

GET /bank_account/_search?pretty
{
  "query": {
      "bool": {  ### 下面几个条件同时满足
           "must": [             ## address字段必须同时满足两个条件
             { "match": {"address": "china"}},
             { "match": {"address": "taiwan"}}
          ],
         "must_not": [   ## address 必须同时不满足这两个条件
             { "match": {"address": "taibei"}},
             { "match": {"address": "beijing"}}
          ],
        "should": [    ## address 满足任意一个条件
             { "match": {"address": "Bill"}},
             { "match": {"address": "Tan"}}
          ]
      }
  }
}

Filter 过滤

相比query,Filter的速度更快一些,因为在query的过程中,我们需要对每一个文档查看相关程度并打分,然后根据打分结果,返回查询结果,而filter只需要回答是否相关并不需要打分,所以相对速度是比较快的。

GET /bank_account/_search?pretty
{
  "query": {
    "bool": {
      "filter": {
        "range": {  
          "balance": {
            "gte": 10,
            "lte": 20000
          }
        }
      }
    }
  }
}

filter: terms/term(用于精准指定) range 配合gte,gt,lte,lt指定范围,同时gte,gt,lte,lt这些也可以单独使用, exists/missing用户判断是否存在某个字段

multi_match 允许在组合多个查询。
wildcards shell 通配符查询匹配
regexp 正则表达式查询
prefix 前缀查询

聚合

GET /bank_account/_search?pretty
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        },
        "sum_balance": {
          "sum": {
            "field": "balance"
          }
        }
      }
    }
  }
}

你可能感兴趣的:(ElasticSearch 基本操作)