1. 前言
profile API 是 Elasticsearch 5.x 的一個新接口。通過這個功能,可以看到一個搜索聚合請求,是如何拆分成底層的 Lucene 請求,並且顯示每部分的耗時情況。
2. profile API 使用
可以通過在 query 部分上方提供 “profile: true” 來啟用Profile API。
GET /ljjtest/book/_search
{
"profile":"true",
"query":{
"match":{
"author":"魯迅"
}
}
}
3. profile API響應說明
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.3728157,
"hits": [ ... ]
},
"profile": {
"shards": [
{
"id": "[0mFoaNASRaGO050a_a28gA][ljjtest][0]",
"searches": [
{
"query": [
{
"type": "BooleanQuery",
"description": "author:魯 author:迅",
"time": "0.5203070000ms",
"time_in_nanos": 520307,
"breakdown": {
"score": 18400,
"build_scorer_count": 1,
"match_count": 0,
"create_weight": 213200,
"next_doc": 28200,
"match": 0,
"create_weight_count": 1,
"next_doc_count": 3,
"score_count": 2,
"build_scorer": 260500,
"advance": 0,
"advance_count": 0
},
"children": [
{
"type": "TermQuery",
"description": "author:魯",
"time": "0.3040070000ms",
"time_in_nanos": 304007,
"breakdown": {
"score": 9100,
"build_scorer_count": 1,
"match_count": 0,
"create_weight": 118200,
"next_doc": 14500,
"match": 0,
"create_weight_count": 1,
"next_doc_count": 3,
"score_count": 2,
"build_scorer": 162200,
"advance": 0,
"advance_count": 0
}
},
{
"type": "TermQuery",
"description": "author:迅",
"time": "0.1005070000ms",
"time_in_nanos": 100507,
"breakdown": {
"score": 2600,
"build_scorer_count": 1,
"match_count": 0,
"create_weight": 63500,
"next_doc": 2200,
"match": 0,
"create_weight_count": 1,
"next_doc_count": 3,
"score_count": 2,
"build_scorer": 32200,
"advance": 0,
"advance_count": 0
}
}
]
}
],
"rewrite_time": 327100,
"collector": [
{
"name": "CancellableCollector",
"reason": "search_cancelled",
"time": "0.04830000000ms",
"time_in_nanos": 48300,
"children": [
{
"name": "SimpleTopScoreDocCollector",
"reason": "search_top_hits",
"time": "0.03680000000ms",
"time_in_nanos": 36800
}
]
}
]
}
],
"aggregations": []
}
]
}
}
Profile API響應說明:
上面的響應顯示的是單個分片。每個分片都被分配一個唯一的ID,ID的格式是[nodeID][indexName][shardID]。現在在"shards"數組里還有另外三個元素,它們是:
- query
- rewrrite_time
- collector
Query
Query 段由構成Query的元素以及它們的時間信息組成。Profile API結果中Query 部分的基本組成是:
type—— 它向我們顯示了哪種類型的查詢被觸發。此處是布爾值。因為多個關鍵字匹配查詢被分成兩個布爾查詢。description—— 該字段顯示啟動查詢的lucene方法。這里是"author:魯 author:迅"time—— lucene 執行此查詢所用的時間。單位是毫秒。time_in_nanos—— lucene 執行此查詢所用的時間。單位是微秒。breakdown—— 有關查詢的更詳細的細節,主要與lucene參數有關。children—— 具有多個關鍵字的查詢被拆分成相應術語的布爾查詢,每個查詢都作為單獨的查詢來執行。每個子查詢的詳細信息將填充到Profile API輸出的子段中。在上面的章節中,可以看到第一個子元素查詢是"魯",下面給出查詢時間和其他breakdown參數等詳細信息。同樣,對於第二個關鍵字,有一個名為"迅"的子元素具有與其兄弟相同的信息。從查詢中的子段中,我們可以得到關於哪個搜索項在總體搜索中造成最大延遲的信息。
Rewrite Time
由於多個關鍵字會分解以創建個別查詢,所以在這個過程中肯定會花費一些時間。將查詢重寫一個或多個組合查詢的時間被稱為“重寫時間”。(以納秒為單位)。
Collectors
在Lucene中,收集器是負責收集原始結果,收集和組合結果,執行結果排序等的過程。例如,在上面的執行的查詢中,當查詢語句中給出size:0時,使用的收集器是"totalHitCountCollector"。這只返回搜索結果的數量(search_count),不返回文檔。此外,收集者所用的時間也一起給出了。
參考:https://it.baiked.com/elasticsearch/1795.html
</div>
