BlackChen'site

ElasticSearch 基本操作

注意, 页面操作均在 Kibana Consul 中操作.

基本操作

  1. 获取集群健康
GET /_cat/health?v

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

Green - everything is good (cluster is fully functional)
Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional)
Red - some data is not available for whatever reason (cluster is partially functional)

  1. 获取nodes列表
GET /_cat/nodes?v
  1. 获取所有索引
GET /_cat/indices?v
  1. 创建索引
PUT /customer?pretty
  1. 新增数据
PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}
  1. 通过id获取数据
GET /customer/_doc/1?pretty
  1. 删除索引
DELETE /customer?pretty
  1. 修改数据 - 指定ID重新put
PUT /customer/_doc/1?pretty
{
  "name": "2222 Doe"
}

POST /customer/_doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}

POST /customer/_doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}

POST /customer/_doc/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}
  1. 删除数据(document)
DELETE /customer/_doc/2?pretty

搜索

  1. 检索文档
GET /bank/_search?q=*&sort=account_number:asc&pretty


GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

took – time in milliseconds for Elasticsearch to execute the search
timed_out – tells us if the search timed out or not
_shards – tells us how many shards were searched, as well as a count of the successful/failed searched shards
hits – search results
hits.total – total number of documents matching our search criteria
hits.hits – actual array of search results (defaults to first 10 documents)
hits.sort - sort key for results (missing if sorting by score)
hits._score and max_score - ignore these fields for now

注意: 默认返回前10条.

  1. 指定返回列
GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}
  1. 指定返回数据匹配match
GET /bank/_search
{
  "query": { "match": { "account_number": 20 } }
}
  1. bool query
    • must (必须全部符合, true)
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
  • should
GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
  • must_not
GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
  • 组合
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

评论