ElasticSearch 學(xué)習(xí)筆記 之基本操作

檢查狀態(tài)

$ curl localhost:9200/_cat/health
1597911300 08:15:00 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%

查看集群狀態(tài)

$ curl 'localhost:9200/_cat/health?v'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time ctive_shards_percent
1597911567 08:19:27  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

集群的名字是“elasticsearch”星爪,正常運(yùn)行湃密,并且狀態(tài)是綠色。

集群節(jié)點(diǎn)信息

$ curl localhost:9200/_cat/nodes?v
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           16          90   7    0.12    1.05     1.21 dilmrt    *      royzeng-VirtualBox

看到叫“royzeng-VirtualBox”的節(jié)點(diǎn)丑慎,這個(gè)節(jié)點(diǎn)是我們集群中的唯一節(jié)點(diǎn)移剪。

列出所有的索引

$ curl 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

這個(gè)結(jié)果意味著究珊,在我們的集群中沒有任何索引。

創(chuàng)建一個(gè)索引

Request

PUT /<index>

現(xiàn)在讓我們創(chuàng)建一個(gè)叫做“customer” 的索引纵苛,然后再列出所有的索引:

curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'

第一個(gè)命令使用PUT創(chuàng)建了一個(gè)叫做“customer” 的索引剿涮。將pretty附加到調(diào)用的尾部言津,使其以格式化的形式打印出JSON響應(yīng)

響應(yīng)如下:

$ curl -XPUT 'localhost:9200/customer?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
$ curl 'localhost:9200/_cat/indices?v'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer 0zmIaw_fSeKLcEmKsdXb7g   1   1          0            0       208b           208b

第二個(gè)命令的結(jié)果告知我們,我們現(xiàn)在有一個(gè)叫做 customer 的索引取试,并且它有1個(gè)主分片和1份復(fù)制悬槽,其中包含0個(gè)文檔。

你可能也注意到了這個(gè)customer索引有一個(gè)黃色健康標(biāo)簽瞬浓〕跗牛回顧我們之前的討論,黃色意味著某些復(fù)制沒有(或者還未)被分配猿棉。這個(gè)索引之所以這樣磅叛,是因?yàn)?Elasticsearch默認(rèn) 為這個(gè)索引創(chuàng)建一份復(fù)制。 由于現(xiàn)在我們只有一個(gè)節(jié)點(diǎn)在運(yùn)行萨赁,那一份復(fù)制就分配不了了(為了高可用)宪躯,直到當(dāng)另外一個(gè)節(jié)點(diǎn)加入到這個(gè)集群后,才能分配位迂。一旦那份復(fù)制在第二個(gè)節(jié)點(diǎn)上被復(fù)制,這個(gè)節(jié)點(diǎn)的健康狀態(tài)就會(huì)變成綠色详瑞。

創(chuàng)建索引的同時(shí)指定mapping

See Removal of mapping types.

7.0 版本后沒有用type 了掂林,這里的例子只是向前兼容。

PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "age":    { "type": "integer" },  
      "email":  { "type": "keyword"  }, 
      "name":   { "type": "text"  }     
    }
  }
}

例如

$ curl -H "Content-Type: application/json" -XPUT localhost:9200/students -d '
> {
>   "mappings": {
>     "properties": {
>       "age":    { "type": "integer" },  
>       "email":  { "type": "keyword"  }, 
>       "name":   { "type": "text"  }     
>     }
>   }
> }'
{"acknowledged":true,"shards_acknowledged":true,"index":"students"}

Add a field to existing index

PUT /my-index-000001/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false
    }
  }
}

例如

$ curl -H "Content-Type: application/json" -XPUT localhost:9200/customer/_mapping -d ' 
> {
>   "properties": {
>     "name": {
>       "type": "text"
>     }
>   }
> }'
{"acknowledged":true}

添加一個(gè)文檔

Request

PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>

例如

$ curl -H "Content-Type: application/json" -XPUT 'localhost:9200/teacher/_doc/3?pretty' -d '
> {
>   "name": "Roy Zeng"
> }'
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 2
}
# 用 POST 方式會(huì)自動(dòng)生成一個(gè)id坝橡。
$ curl -H "Content-Type: application/json" -XPOST 'localhost:9200/teacher/_doc/' -d '
> {
>   "name": "Tony He"
> }'
{"_index":"teacher","_type":"_doc","_id":"-L-6D3QBwc726urOj0Un","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":3,"_primary_term":2}

查詢一個(gè)文檔get

查詢剛才的輸入

Request

GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
$ curl -XGET localhost:9200/teacher/_doc/3?pretty
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "Roy Zeng"
  }
}

$ curl -XGET localhost:9200/teacher/_source/3?pretty
{
  "name" : "Roy Zeng"
}


$ curl -XGET localhost:9200/teacher/_doc/-L-6D3QBwc726urOj0Un?pretty
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "-L-6D3QBwc726urOj0Un",
  "_version" : 1,
  "_seq_no" : 3,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "Tony He"
  }
}

查詢Index 里面全部?jī)?nèi)容

GET /<target>/_search

批量讀取mget

mget它允許我們一次get大量的document泻帮,與get單條數(shù)據(jù)的api get方法類似,mget查詢是基于index计寇,id兩個(gè)個(gè)條件進(jìn)行的锣杂,比如我們可以一次mget 50條數(shù)據(jù),這50條數(shù)據(jù)可以是在50個(gè)不同index中番宁,并且每一個(gè)get都可以單獨(dú)指定它的路由查詢信息元莫,或者返回的字段內(nèi)容。

mget可以批量的根據(jù)index蝶押,type踱蠢,id三個(gè)字段來獲取一批數(shù)據(jù),它不能用來查詢棋电,最少得需要知道index 和 id兩個(gè)字段的值茎截,才能進(jìn)行g(shù)et,這一點(diǎn)與query是不一樣的赶盔。

Request

GET /_mget
GET /<index>/_mget

不指定index

GET /_mget
{
  "docs": [
    {
      "_index": "my-index-000001",
      "_id": "1"
    },
    {
      "_index": "my-index-000002",
      "_id": "2"
    }
  ]
}

指定index

GET /my-index-000001/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": "1"
    },
    {
      "_type": "_doc",
      "_id": "2"
    }
  ]
}

或者簡(jiǎn)化

GET /my-index-000001/_mget
{
  "ids" : ["1", "2"]
}

例如

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/_mget?pretty -d '
> {
>     "docs" : [
>         {
>             "_index": "teacher",
>             "_id" : "1"
>         },
>         {
>             "_index": "customer",
>             "_id" : "2"
>         }
>     ]
> }'
{
  "docs" : [
    {
      "_index" : "teacher",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 12,
      "_primary_term" : 2,
      "found" : true,
      "_source" : {
        "name" : "John Doe becomes Jane Doe"
      }
    },
    {
      "_index" : "customer",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "Roy Doe"
      }
    }
  ]
}

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/customer/_source/_mget?pretty -d '
> {
>   "ids" : ["1", "2"]
> }'
{
  "docs" : [
    {
      "_index" : "customer",
      "_type" : "_source",
      "_id" : "1",
      "found" : false
    },
    {
      "_index" : "customer",
      "_type" : "_source",
      "_id" : "2",
      "found" : false
    }
  ]
}

此外企锌,還可以單獨(dú)的設(shè)置對(duì)返回的數(shù)據(jù)(source)進(jìn)行過濾操作,默認(rèn)情況下如果這條數(shù)據(jù)被store了于未,那么它會(huì)返回整個(gè)document撕攒。

常見使用source過濾

GET /_mget
{
  "docs": [
    {
      "_index": "test",
      "_type": "_doc",
      "_id": "1",
      "_source": false
    },
    {
      "_index": "test",
      "_type": "_doc",
      "_id": "2",
      "_source": [ "field3", "field4" ]
    },
    {
      "_index": "test",
      "_type": "_doc",
      "_id": "3",
      "_source": {
        "include": [ "user" ],
        "exclude": [ "user.location" ]
      }
    }
  ]
}

例如

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/customer/_mget?pretty -d '
> {
>   "docs": [
>     {
>       "_id": "1",
>       "_source": [ "age" ]
>     }
>   ]
> }'
{
  "docs" : [
    {
      "_index" : "customer",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 2,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "age" : 20
      }
    }
  ]
}

刪除

刪除需求陡鹃,通常如下:

  • 刪除某個(gè)index的數(shù)據(jù)
  • 根據(jù)某個(gè)主鍵id刪除單條數(shù)據(jù)
  • 根據(jù)某個(gè)查詢條件刪除一批數(shù)據(jù)

刪除索引

$ curl -XDELETE 'localhost:9200/employee'
{"acknowledged":true}

重新驗(yàn)證一下

$ curl 'localhost:9200/_cat/indices?v'

刪除文檔

Request

DELETE /<index>/_doc/<_id>

刪除文檔是非常直觀的。以下的例子展示了怎樣刪除ID為2的文檔:

$ curl  -XDELETE "localhost:9200/teacher/_doc/2"
{"_index":"teacher","_type":"_doc","_id":"2","_version":2,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":9,"_primary_term":2}

根據(jù)查詢刪除文檔

Request

POST /<target>/_delete_by_query
POST /my-index-000001/_delete_by_query
{
  "query": {
    "match": {
      "user.id": "elkbee"
    }
  }
}

例子

$ curl -H 'Content-Type: application/json' -XPOST localhost:9200/customer/_delete_by_query?pretty -d '
> {
>   "query": {
>     "match": {
>       "age": 20
>     }
>   }
> }'
{
  "took" : 168,
  "timed_out" : false,
  "total" : 1,
  "deleted" : 1,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

更新文檔

Request

POST /<index>/_update/<_id>

例子

注意:json 文件的格式變了打却,多了 doc 部分

$ curl -H 'Content-Type: application/json' -XPOST "localhost:9200/teacher/_update/3?pretty" -d '
> {
>   "doc" : {
>      "name": "Roy He"
>   }
> }'
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 2
}

還可以添加新的field

$ curl -H 'Content-Type: application/json' -XPOST "localhost:9200/teacher/_update/3?pretty" -d '
> {
>   "doc" : {
>      "name": "Roy Zeng",
>      "age": 20
>   }
> }'
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 2
}

更新也可以通過使用簡(jiǎn)單的腳本來進(jìn)行杉适。這個(gè)例子使用一個(gè)腳本將age加5:

$ curl -H 'Content-Type: application/json' -XPOST "localhost:9200/teacher/_update/3?pretty" -d '
> {
>   "script" : "ctx._source.age += 5"
> }'
{
  "_index" : "teacher",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 6,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 2
}

在上面的例子中,ctx._source指向當(dāng)前被更新的文檔柳击。

還可以用重復(fù)寫入的方式來修改猿推。

批處理

Request

POST /_bulk
POST /<target>/_bulk

Provides a way to perform multiple index, create, delete, and update actions in a single request.

bulk的格式:

action:index/create/update/delete

metadata:_index,_type,_id

request body:_source (刪除操作不需要加request body)

? { action: { metadata }}

? { request body }

create 和index的區(qū)別

如果數(shù)據(jù)存在,使用create操作失敗捌肴,會(huì)提示文檔已經(jīng)存在蹬叭,使用index則可以成功執(zhí)行。

以下例子在一個(gè)bulk操作中状知,首先更新第一個(gè)文檔(ID為1)秽五,然后刪除第二個(gè)文檔(ID為2)

$ curl -H 'Content-Type: application/json' -XPOST "localhost:9200/teacher/_bulk?pretty" -d '
> {"update":{"_id":"1"}}
> {"doc": { "name": "John Doe becomes Jane Doe" } }
> {"delete":{"_id":"2"}}
> '
{
  "took" : 12,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 12,
        "_primary_term" : 2,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 13,
        "_primary_term" : 2,
        "status" : 404
      }
    }
  ]
}

$ cat request
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test",  "_id" : "2" } }
{ "create" : { "_index" : "test",  "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_index" : "test", "_id" : "1" } }
{ "doc" : {"field2" : "value2"} }
$ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@request"
{"took":29,"errors":true,"items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":6,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":8,"_primary_term":1,"status":200}},{"delete":{"_index":"test","_type":"_doc","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":9,"_primary_term":1,"status":404}},{"create":{"_index":"test","_type":"_doc","_id":"3","status":409,"error":{"type":"version_conflict_engine_exception","reason":"[3]: version conflict, document already exists (current version [1])","index_uuid":"CNHCqjrOR8CaPPqBY6-4dw","shard":"0","index":"test"}}},{"update":{"_index":"test","_type":"_doc","_id":"1","_version":7,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":10,"_primary_term":1,"status":200}}]}

權(quán)限管理

前提:x-pack 已經(jīng)配置(setting [xpack.security.enabled] to [true] in the elasticsearch.yml)。

先創(chuàng)建 role饥悴,再創(chuàng)建用戶坦喘,并分配role。

有不同范圍的權(quán)限西设,cluster/ index/ application 瓣铣,我主要關(guān)注的是 index。具體權(quán)限看這里

創(chuàng)建role

Request

POST /_security/role/<name>
PUT /_security/role/<name>

example to create role my_admin_role:

POST /_security/role/my_admin_role
{
  "cluster": ["all"],
  "indices": [
    {
      "names": [ "index1", "index2" ],
      "privileges": ["all"]
    }
   ]
}

創(chuàng)建用戶

Request

POST /_security/user/<username>
PUT /_security/user/<username>

example to create user jacknich:

POST /_security/user/jacknich
{
  "password" : "j@rV1s",
  "roles" : [ "my_admin_role" ],
  "full_name" : "Jack Nicholson",
  "email" : "jacknich@example.com"
}

Verify:

GET /_security/user/jacknich

{
  "jacknich" : {
    "username" : "jacknich",
    "roles" : [
      "my_admin_role",
      "monitor"
    ],
    "full_name" : "Jack Nicholson",
    "email" : "jacknich@example.com",
    "metadata" : { },
    "enabled" : true
  }
}

Search API

實(shí)踐中贷揽,這個(gè)才是最常用的

GET /my-index-000001/_search

Request

GET /<target>/_search
GET /_search
POST /<target>/_search
POST /_search

例子

查詢所有

GET /teacher/_search
{
  "query":{"match_all":{}}
}
$ curl -H 'Content-Type: application/json' -XGET localhost:9200/teacher/_search?pretty -d '
> {
>    "query": { "match_all": {} }
> }'
{
  "took" : 37,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "-L-6D3QBwc726urOj0Un",
        "_score" : 1.0,
        "_source" : {
          "name" : "Tony He"
        }
      },
      {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "John Doe becomes Jane Doe"
        }
      }
    ]
  }
}

對(duì)于上述的響應(yīng)信息棠笑,我們可以看到以下幾個(gè)部分:

  • took — ElasticSearch執(zhí)行搜索操作耗費(fèi)的時(shí)間,以毫秒為單位禽绪。
  • timed_out — 表示搜索操作是否超時(shí)
  • _shards — 表示搜索的分片數(shù)量蓖救,以及搜索成功/失敗的分片數(shù)量。
  • hits — 搜索結(jié)果
  • hits.total — 匹配搜索條件的文檔總數(shù)
  • hits.hits — 搜索結(jié)果的實(shí)際數(shù)組(默認(rèn)為前10個(gè)文檔)
  • max_score – 最相關(guān)的文檔分?jǐn)?shù)
  • hits.total.value - 找到的文檔總數(shù)

查詢名稱Tony 的人

GET /teacher/_search
{
    "query" : {
        "match" : {
            "name" : "Tony"
        }
    }
}
$ curl -H 'Content-Type: application/json' -XGET localhost:9200/teacher/_search?pretty -d '
> {
>     "query" : {
>         "match" : {
>             "name" : "Tony"
>         }
>     }
> }'
{
  "took" : 445,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.7801935,
    "hits" : [
      {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "-L-6D3QBwc726urOj0Un",
        "_score" : 0.7801935,
        "_source" : {
          "name" : "Tony He"
        }
      },
      {
        "_index" : "teacher",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.7801935,
        "_source" : {
          "name" : "Tony Wang"
        }
      }
    ]
  }
}

果要全部匹配而不是僅僅是包含關(guān)鍵字之一印屁,你需要使用 match_phrase 而不是 match循捺。

簡(jiǎn)單查詢(uri 搜索)

一個(gè)搜索可以用純粹的uri來執(zhí)行查詢。在這種模式下使用搜索雄人,并不是所有的選項(xiàng)都是暴露的巨柒。它可以方便快速進(jìn)行curl 測(cè)試

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'

多個(gè)條件使用&拼接柠衍;排序:默認(rèn)是升序asc

其它參數(shù)

例子

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/teacher/_search?q=name:Tony
{"took":50,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":0.5442147,"hits":[{"_index":"teacher","_type":"_doc","_id":"3","_score":0.5442147,"_source":
 {
   "name": "Tony Wang"
 }},{"_index":"teacher","_type":"_doc","_id":"-L-6D3QBwc726urOj0Un","_score":0.5442147,"_source":
{
  "name": "Tony He"
}}]}}

分頁查詢

可以用from和size參數(shù)對(duì)結(jié)果進(jìn)行分頁洋满。from表示你想獲得的第一個(gè)結(jié)果的偏移量,size表示你想獲得的結(jié)果的個(gè)數(shù)珍坊。from默認(rèn)是0牺勾,size默認(rèn)是10.

例如查詢第二頁

GET /teacher/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 1
}

聚合

查詢各城市的人有多少。請(qǐng)求使用 terms 聚合對(duì) teacher 索引中對(duì)所有賬戶進(jìn)行分組

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/teacher/_search?pretty -d '
> {
>   "size": 0,
>   "aggs": {
>     "group_by_address": {
>       "terms": {
>         "field": "address.keyword"
>       }
>     }
>   }
> }'
{
  "took" : 250,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_address" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Guangzhou",
          "doc_count" : 2
        },
        {
          "key" : "Shanghai",
          "doc_count" : 1
        }
      ]
    }
  }
}

在返回結(jié)果中 buckets 的值是字段 address 的桶阵漏。 doc_count 表示每市的賬戶數(shù)驻民》撸可以看到廣州有 2 個(gè)帳戶,由于 "size": 0 表示了返回只有聚合結(jié)果回还,無具體數(shù)據(jù)裆泳。

聚合嵌套

請(qǐng)求將 avg 聚合嵌套在 group_by_address 聚合內(nèi),以計(jì)算沒人的平均年齡柠硕。

$ curl -H 'Content-Type: application/json' -XGET localhost:9200/teacher/_search?pretty -d '
> {
>   "size": 0,
>   "aggs": {
>     "group_by_address": {
>       "terms": {
>         "field": "address.keyword"
>       },
>       "aggs": {
>         "average_age": {
>           "avg": {
>             "field": "age"
>           }
>         }
>       }
>     }
>   }
> }'
{
  "took" : 58,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_address" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Guangzhou",
          "doc_count" : 2,
          "average_age" : {
            "value" : 25.0
          }
        },
        {
          "key" : "Shanghai",
          "doc_count" : 1,
          "average_age" : {
            "value" : 20.0
          }
        }
      ]
    }
  }
}

聚合

聚合框架有助于基于搜索查詢提供聚合數(shù)據(jù)工禾。

不同類型的聚合

有許多不同類型的聚合,每種聚合都有自己的目的和輸出蝗柔。為了更好地理解這些類型闻葵,通常更容易將它們分為四個(gè)主要系列:

Bucketing

生成存儲(chǔ)桶的一組聚合,其中每個(gè)存儲(chǔ)桶都與一個(gè)鍵和一個(gè)文檔條件相關(guān)聯(lián)癣丧。執(zhí)行聚合時(shí)槽畔,將對(duì)上下文中的每個(gè)文檔評(píng)估所有存儲(chǔ)桶條件,并且當(dāng)條件匹配時(shí)胁编,該文檔將被視為 “落入” 相關(guān)存儲(chǔ)桶厢钧。在匯總過程結(jié)束時(shí),我們將獲得一個(gè)存儲(chǔ)桶列表 - 每個(gè)存儲(chǔ)桶都帶有一組 “屬于” 的文檔嬉橙。

Metric

用于跟蹤和計(jì)算一組文檔的指標(biāo)的聚合早直。

Matrix

一組聚合,可在多個(gè)字段上進(jìn)行操作憎夷,并根據(jù)從請(qǐng)求的文檔字段中提取的值生成矩陣結(jié)果。與指標(biāo)和存儲(chǔ)桶聚合不同昧旨,此聚合系列尚不支持腳本拾给。

Pipeline

聚合其他聚合的輸出及其相關(guān)度量的聚合
接下來是有趣的部分。由于每個(gè)存儲(chǔ)桶有效地定義了一個(gè)文檔集 (所有屬于該存儲(chǔ)桶的文檔)兔沃,可以潛在地關(guān)聯(lián) bucket 級(jí)別上的聚合蒋得,這些聚合將在該 bucket 的上下文中執(zhí)行。這就是聚合的真正力量發(fā)揮作用的地方: 聚合可以嵌套乒疏!

結(jié)構(gòu)聚合

下面的代碼片段描述了聚合的基本結(jié)構(gòu):

"aggregations" : {
    "<aggregation_name>" : {
        "<aggregation_type>" : {
            <aggregation_body>
        }
        [,"meta" : {  [<meta_data_body>] } ]?
        [,"aggregations" : { [<sub_aggregation>]+ } ]?
    }
    [,"<aggregation_name_2>" : { ... } ]*
}

JSON 中的 aggregations 對(duì)象(也可以使用 aggs)保存要計(jì)算的聚合额衙。每個(gè)聚合都與用戶定義的邏輯名稱相關(guān)聯(lián)(例如,如果聚合計(jì)算平均價(jià)格怕吴,則將其命名為 avg_price 是有意義的)窍侧。這些邏輯名稱還將用于唯一地標(biāo)識(shí)響應(yīng)中的聚合。每個(gè)聚合都具有特定的類型 (上面的代碼片段中為 <aggregation_type>)转绷,通常是命名聚合主體中的第一個(gè)鍵伟件。每種聚合類型都取決于聚合的性質(zhì) (例如,特定字段上的 avg 聚合將定義要在其上計(jì)算平均值的字段)议经,定義自己的主體斧账。在聚合類型定義的同一級(jí)別上谴返,可以選擇定義一組其他聚合,盡管僅當(dāng)您定義的聚合具有存儲(chǔ)特性時(shí)才有意義咧织。在這種情況下嗓袱,將為存儲(chǔ)桶聚合構(gòu)建的所有存儲(chǔ)桶計(jì)算您在存儲(chǔ)桶聚合級(jí)別上定義的子聚合。例如习绢,如果您在 range 聚合下定義一組聚合渠抹,則將為定義的范圍存儲(chǔ)桶計(jì)算子聚合。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末毯炮,一起剝皮案震驚了整個(gè)濱河市逼肯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌桃煎,老刑警劉巖篮幢,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異为迈,居然都是意外死亡三椿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門葫辐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搜锰,“玉大人,你說我怎么就攤上這事耿战〉暗穑” “怎么了?”我有些...
    開封第一講書人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵剂陡,是天一觀的道長(zhǎng)狈涮。 經(jīng)常有香客問我,道長(zhǎng)鸭栖,這世上最難降的妖魔是什么歌馍? 我笑而不...
    開封第一講書人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮晕鹊,結(jié)果婚禮上松却,老公的妹妹穿的比我還像新娘。我一直安慰自己溅话,他們只是感情好晓锻,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著飞几,像睡著了一般带射。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上循狰,一...
    開封第一講書人閱讀 49,166評(píng)論 1 284
  • 那天窟社,我揣著相機(jī)與錄音券勺,去河邊找鬼。 笑死灿里,一個(gè)胖子當(dāng)著我的面吹牛关炼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播匣吊,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼儒拂,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了色鸳?” 一聲冷哼從身側(cè)響起社痛,我...
    開封第一講書人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎命雀,沒想到半個(gè)月后蒜哀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吏砂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年撵儿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狐血。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡淀歇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出匈织,到底是詐尸還是另有隱情浪默,我是刑警寧澤,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布缀匕,位于F島的核電站纳决,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏弦追。R本人自食惡果不足惜岳链,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一花竞、第九天 我趴在偏房一處隱蔽的房頂上張望劲件。 院中可真熱鬧,春花似錦约急、人聲如沸零远。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽牵辣。三九已至,卻和暖如春奴饮,著一層夾襖步出監(jiān)牢的瞬間纬向,已是汗流浹背择浊。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留逾条,地道東北人琢岩。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像师脂,于是被迫代替她去往敵國(guó)和親担孔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344