elasticsearch常用操作

elasticsearch的接口是restful api风格

基本操作

查看集群健康状态

GET /_cat/health?v

我们可以看到我们的名称为“elasticsearch”的集群正在运行,状态标识为green。
无论何时查看集群健康状态,我们会得到green、yellow、red中的任何一个。
Green - 一切运行正常(集群功能齐全)
Yellow - 所有数据是可以获取的,但是一些复制品还没有被分配(集群功能齐全)
Red - 一些数据因为一些原因获取不到(集群部分功能不可用)
注意:当一个集群处于red状态时,它会通过可用的分片继续提供搜索服务,但是当有未分配的分片时,你需要尽快的修复它。
 
查看节点信息
GET /_cat/nodes?v
 

可以看到在ip 10.38.33.83有一节点 4tLXFfE
 
新建索引
PUT /index?pretty

说明索引user创建成功

查看索引
GET /_cat/indices?v

 

删除索引
DELETE /index?pretty

 

创建文档document
PUT /index/type/id?pretty

/index/type/id?pretty 中id不一定是数字,可以字符串 "abc" 等,新增文档的时候,可以不指定id,需要改成POST请求,返回的id是随机的。result为created表示创建,updated表示更新

 

参数 pretty=true表示以易读的方式返回, 可以省略。

 

查询文档
GET /index/type/id?pretty

found为true表示查询成功,_source表示值

 

更新文档
PUT /index/type/id?pretty

result为 update 表示更新,_version表示版本号,创建的时候为1,更新一次为2

再查询看看结果

 

删除文档
DELETE  /index/type/id?pretty

result:deleted说明删除成功

再次查询

found:false 表示查询失败

批处理创建文档
批处理
除了在单个文档上执行索引,更新和删除操作外,Elasticsearch还提供了批操作的功能,通过使用 _bulk API完成。这个功能非常重要,因为它提供了一种非常高效的机制去通过更少的网络切换尽可能快的执行多个操作。
NOTE: the final line of data must end with a newline character \n. Each newline character may be preceded by a carriage return \r. When sending requests to this endpoint the Content-Type header should be set to application/x-ndjson

高级查询

查询所有

GET /index/_search  

{
  "query": { "match_all": {} }
}
 默认返回前10条
 
结果排序 (根据account_number 升序)
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

返回指定的条数(返回前50条)

{
  "query": { "match_all": {} },
  "size": 50
}
 
返回从某个位置开始后指定的条数 (返回10~20)
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}
 
返回指定filed(只返回account_number 和 balance)
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

 

条件过滤 
返回 account_number = 20 的文档
{
  "query": { "match": { "account_number": 20 } }
}
返回address包含 mill 的记录
{
  "query": { "match": { "address": "mill" } }
}
返回address包含 mill 或者 lane 的记录
{
  "query": { "match": { "address": "mill lane" } }
}
返回address包含 mill lane 的记录 [mill lane]作为一个整体的词条
{
  "query": { "match_phrase": { "address": "mill lane" } }
}

  

bool 查询
查询address 既包含mill 也包含 lane的记录 【must 表示 "逻辑与"】
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
 

查询address 包含mill 或者包含 lane的记录 【should表示 "逻辑或"】

{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

  

查询address 既不包含mill 且不包含 lane的记录 【must_not 表示 "与非"】

{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

  

查询年龄为40但是state不是ID的记录
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

  

filter 查询
查询在范围 20000~30000的记录
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

  

集合查询( aggregation query)
按state分组,并且count每组 
相当于 sql 语句 SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC,["size": 0 表示不返回查询的document信息,只展示集合信息]
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}

  

按state分组,并且求出每组的balance的平均值  
相当于sql语句:SELECT state, COUNT(*),AVG(balance) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

  

按state分组,并且求出每组的balance的平均值 ,按照balance的平均值降序  
相当于sql语句:SELECT state, COUNT(*),AVG(balance) FROM bank GROUP BY state ORDER BY  AVG(balance) DESC
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

  

按年龄20~30 30~40 40~50分段分组,统计每个年龄段男和女的balance平均值
{
  "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"
              }
            }
          }
        }
      }
    }
  }
}

 

 参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.1/index.html

猜你喜欢

转载自www.cnblogs.com/xiaojianfeng/p/9474446.html
今日推荐