在使用ES的過程中,有的搜索請求的響應可能比較慢,其中大部分的原因是DSL的執行邏輯有問題。
ES提供了profile功能,該功能詳細地列出了搜索時每一個步驟的耗時,可以幫助用戶對DSL的性能進行剖析。
開啟profile功能只需要在一個正常的搜索請求的DSL中添加"profile":"true"即可。以下查詢將開啟profile功能:
{ "profile": "true", //打開性能剖析開關 "query": { //查詢條件 "match": { "title": "金都" } } }
執行以上DSL后ES返回了一段比較冗長的信息,下面是省略一些信息的返回數據。
{ "took":2, "timed_out":false, "_shards":…, "hits":…, "profile":{ //命中的分片信息 "shards":[ { "id":"[N533dYYvQWeYoRSPjpo8EA][hotel][0]", "searches":[ { "query":[ { //在title中搜索“金都”,被ES拆分成兩個子查詢 "type":"BooleanQuery", "description":"title:金 title:都", "time_in_nanos":311540, //match搜索的總耗時 "breakdown":{ "set_min_competitive_score_count":0, "match_count":3, //命中的文檔個數 "shallow_advance_count":0, "set_min_competitive_score":0, "next_doc":11689, "match":1833, "next_doc_count":3, "score_count":3, //打分的文檔個數 "compute_max_score_count":0, "compute_max_score":0, "advance":46290, "advance_count":1, "score":9070, "build_scorer_count":2, "create_weight":137353, "shallow_advance":0, "create_weight_count":1, "build_scorer":105305 }, "children":[ //子查詢 { //子查詢"title:金" "type":"TermQuery", "description":"title:金", "time_in_nanos":123649, //耗時 "breakdown":Object{…} }, { //子查詢"title:都" "type":"TermQuery", "description":"title:都", "time_in_nanos":29648, "breakdown":Object{…} } ] } ], "rewrite_time":12001, "collector":[ //ES 收集數據性能剖析 { "name":"SimpleTopScoreDocCollector", "reason":"search_top_hits", "time_in_nanos":18004 //ES收集數據的耗時 } ] } ], "aggregations":… //聚合性能剖析,本次搜索無聚合,因此數據為空 } ] } }
如上所示,在帶有profile的返回信息中,除了包含搜索結果外,還包含profile子句,在該子句中展示了搜索過程中各個環節的名稱及耗時情況。
需要注意的是,使用profile功能是有資源損耗的,建議用戶只在前期調試的時候使用該功能,在生產中不要開啟profile功能。
上面只是一個很簡單的例子,如果查詢比較復雜或者命中的分片比較多,profile返回的信息將特別冗長。在這種情況下,用戶進行性能剖析的效率將非常低。
為此,Kibana提供了可視化的profile功能,該功能建立在ES的profile功能基礎上。在Kibana的Dev Tools界面中單擊Search Profiler鏈接,就可以使用可視化的profile了,其區域布局如圖所示。
文章來源:Elasticsearch搜索引擎構建入門與實戰 --> 4.1.4 性能分析