Elasticsearch聚合查詢(xún)

聚合解決的問(wèn)題

聚合描述數(shù)據(jù)的度量、統(tǒng)計(jì)以及其他分析結(jié)果。它回答:

  • 網(wǎng)站平均響應(yīng)時(shí)間
  • 基于交易量,誰(shuí)是最有價(jià)值客戶
  • 在網(wǎng)絡(luò)傳輸中,多大的文件才認(rèn)為是大文件
  • 每個(gè)產(chǎn)品種類(lèi)中各有多少產(chǎn)品

ES聚合分類(lèi)

Elasticsearch將聚合組織為三類(lèi):

  • Metric 聚合計(jì)算度量,例如:字段值的總和、平均數(shù)
  • Bucket 聚合基于字段值、范圍或其他條件,將文檔組合到bucket中,也稱(chēng)作bins(箱)
  • Pipeline 聚合以其他的聚合作為輸入,而不是文檔或字段

聚合的運(yùn)行

在search API中,使用 "aggs" 參數(shù):

GET /my-index-000001/_search
{
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "my-field"
      }
    }
  }
}

限定聚合的范圍

使用 query 參數(shù)來(lái)信啊頂聚合運(yùn)行在哪些文檔

只返回聚合結(jié)果

默認(rèn)是要返回搜索命中結(jié)果,為了只返回聚合結(jié)果,需要指定 size 參數(shù)為 0

GET /my-index-000001/_search
{
  "size": 0,
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "my-field"
      }
    }
  }
}

運(yùn)行多個(gè)聚合

一個(gè)請(qǐng)求中可以包含多個(gè)聚合

GET /my-index-000001/_search
{
  "aggs": {
    "my-first-agg-name": {
      "terms": {
        "field": "my-field"
      }
    },
    "my-second-agg-name": {
      "avg": {
        "field": "my-other-field"
      }
    }
  }
}

運(yùn)行子聚合

bucket 聚合支持 bucket 聚合或 metric 子聚合

GET /my-index-000001/_search
{
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "my-field"
      },
      "aggs": {
        "my-sub-agg-name": {
          "avg": {
            "field": "my-other-field"
          }
        }
      }
    }
  }
}

如上為 term 聚合和 avg 子聚合計(jì)算每個(gè) bucket 的文檔的平均值。
嵌套子聚合沒(méi)有級(jí)數(shù)和深度的限制。

添加指定元數(shù)據(jù)

使用 meta 對(duì)象來(lái)關(guān)聯(lián)指定元數(shù)據(jù)和聚合

GET /my-index-000001/_search
{
  "aggs": {
    "my-agg-name": {
      "terms": {
        "field": "my-field"
      },
      "meta": {
        "my-metadata-field": "foo"
      }
    }
  }
}

返回聚合類(lèi)型

在查詢(xún)參數(shù)上使用 typed_keys 可以讓響應(yīng)消息返回聚合的類(lèi)型

GET /my-index-000001/_search?typed_keys
{
  "aggs": {
    "my-agg-name": {
      "histogram": {
        "field": "my-field",
        "interval": 1000
      }
    }
  }
}

返回的類(lèi)型名作為聚合名的前綴

{
  ...
  "aggregations": {
    "histogram#my-agg-name": {
      "buckets": []
    }
  }
}

在聚合中使用腳本

字段可能并不精確匹配聚合,這時(shí)你需要聚合的是運(yùn)行時(shí)字段

GET /my-index-000001/_search?size=0
{
  "runtime_mappings": {
    "message.length": {
      "type": "long",
      "script": "emit(doc['message.keyword'].value.length())"
    }
  },
  "aggs": {
    "message_length": {
      "histogram": {
        "interval": 10,
        "field": "message.length"
      }
    }
  }
}

腳本動(dòng)態(tài)計(jì)算字段值,這個(gè)給聚合添加了少許開(kāi)銷(xiāo)。
有些聚合,如 terms 和 filters 可以使用為運(yùn)行時(shí)字段做一些優(yōu)化,總體來(lái)說(shuō),在性能表現(xiàn)上,使用運(yùn)行時(shí)字段,聚合與聚合之間存在很大差異。

聚合緩存

為了更快的響應(yīng),ES 在 shard request cache 緩存頻繁運(yùn)行聚合的結(jié)果。要獲取緩存結(jié)果,每個(gè)search 需使用相同的 preference string

如果不需要搜索 hits,設(shè)置size 為0 來(lái)避免填充緩存

Elasticsearch 給相同的分片用相同的preference string 來(lái)路由 searches。如果分片的數(shù)據(jù)在搜索間不改變,分片將返回緩存的聚合結(jié)果

對(duì)long 值的限制

在運(yùn)行聚合時(shí),ES 使用double 值來(lái)保存并呈現(xiàn)數(shù)字?jǐn)?shù)據(jù),因此,對(duì)于long 數(shù)字的聚合,超出253的值將取近似值

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容