profile.asciidoc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. [[search-profile]]
  2. === Profile API
  3. WARNING: The Profile API is a debugging tool and adds significant overhead to search execution.
  4. The Profile API provides detailed timing information about the execution of individual components
  5. in a search request. It gives the user insight into how search requests are executed at a low level so that
  6. the user can understand why certain requests are slow, and take steps to improve them.
  7. Note that the Profile API, <<profile-limitations, amongst other things>>, doesn't measure
  8. network latency, time spent in the search fetch phase, time spent while the requests spends
  9. in queues or while merging shard responses on the coordinating node.
  10. The output from the Profile API is *very* verbose, especially for complicated requests executed across
  11. many shards. Pretty-printing the response is recommended to help understand the output
  12. [float]
  13. ==== Usage
  14. Any `_search` request can be profiled by adding a top-level `profile` parameter:
  15. [source,js]
  16. --------------------------------------------------
  17. GET /twitter/_search
  18. {
  19. "profile": true,<1>
  20. "query" : {
  21. "match" : { "message" : "some number" }
  22. }
  23. }
  24. --------------------------------------------------
  25. // CONSOLE
  26. // TEST[setup:twitter]
  27. <1> Setting the top-level `profile` parameter to `true` will enable profiling
  28. for the search
  29. This will yield the following result:
  30. [source,js]
  31. --------------------------------------------------
  32. {
  33. "took": 25,
  34. "timed_out": false,
  35. "_shards": {
  36. "total": 1,
  37. "successful": 1,
  38. "skipped" : 0,
  39. "failed": 0
  40. },
  41. "hits": {
  42. "total" : {
  43. "value": 4,
  44. "relation": "eq"
  45. },
  46. "max_score": 0.5093388,
  47. "hits": [...] <1>
  48. },
  49. "profile": {
  50. "shards": [
  51. {
  52. "id": "[2aE02wS1R8q_QFnYu6vDVQ][twitter][0]",
  53. "searches": [
  54. {
  55. "query": [
  56. {
  57. "type": "BooleanQuery",
  58. "description": "message:some message:number",
  59. "time_in_nanos": "1873811",
  60. "breakdown": {
  61. "score": 51306,
  62. "score_count": 4,
  63. "build_scorer": 2935582,
  64. "build_scorer_count": 1,
  65. "match": 0,
  66. "match_count": 0,
  67. "create_weight": 919297,
  68. "create_weight_count": 1,
  69. "next_doc": 53876,
  70. "next_doc_count": 5,
  71. "advance": 0,
  72. "advance_count": 0,
  73. "compute_max_score": 0,
  74. "compute_max_score_count": 0,
  75. "shallow_advance": 0,
  76. "shallow_advance_count": 0,
  77. "set_min_competitive_score": 0,
  78. "set_min_competitive_score_count": 0
  79. },
  80. "children": [
  81. {
  82. "type": "TermQuery",
  83. "description": "message:some",
  84. "time_in_nanos": "391943",
  85. "breakdown": {
  86. "score": 28776,
  87. "score_count": 4,
  88. "build_scorer": 784451,
  89. "build_scorer_count": 1,
  90. "match": 0,
  91. "match_count": 0,
  92. "create_weight": 1669564,
  93. "create_weight_count": 1,
  94. "next_doc": 10111,
  95. "next_doc_count": 5,
  96. "advance": 0,
  97. "advance_count": 0,
  98. "compute_max_score": 0,
  99. "compute_max_score_count": 0,
  100. "shallow_advance": 0,
  101. "shallow_advance_count": 0,
  102. "set_min_competitive_score": 0,
  103. "set_min_competitive_score_count": 0
  104. }
  105. },
  106. {
  107. "type": "TermQuery",
  108. "description": "message:number",
  109. "time_in_nanos": "210682",
  110. "breakdown": {
  111. "score": 4552,
  112. "score_count": 4,
  113. "build_scorer": 42602,
  114. "build_scorer_count": 1,
  115. "match": 0,
  116. "match_count": 0,
  117. "create_weight": 89323,
  118. "create_weight_count": 1,
  119. "next_doc": 2852,
  120. "next_doc_count": 5,
  121. "advance": 0,
  122. "advance_count": 0,
  123. "compute_max_score": 0,
  124. "compute_max_score_count": 0,
  125. "shallow_advance": 0,
  126. "shallow_advance_count": 0,
  127. "set_min_competitive_score": 0,
  128. "set_min_competitive_score_count": 0
  129. }
  130. }
  131. ]
  132. }
  133. ],
  134. "rewrite_time": 51443,
  135. "collector": [
  136. {
  137. "name": "CancellableCollector",
  138. "reason": "search_cancelled",
  139. "time_in_nanos": "304311",
  140. "children": [
  141. {
  142. "name": "SimpleTopScoreDocCollector",
  143. "reason": "search_top_hits",
  144. "time_in_nanos": "32273"
  145. }
  146. ]
  147. }
  148. ]
  149. }
  150. ],
  151. "aggregations": []
  152. }
  153. ]
  154. }
  155. }
  156. --------------------------------------------------
  157. // TESTRESPONSE[s/"took": 25/"took": $body.took/]
  158. // TESTRESPONSE[s/"hits": \[...\]/"hits": $body.$_path/]
  159. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  160. // TESTRESPONSE[s/\[2aE02wS1R8q_QFnYu6vDVQ\]\[twitter\]\[0\]/$body.$_path/]
  161. <1> Search results are returned, but were omitted here for brevity
  162. Even for a simple query, the response is relatively complicated. Let's break it down piece-by-piece before moving
  163. to more complex examples.
  164. First, the overall structure of the profile response is as follows:
  165. [source,js]
  166. --------------------------------------------------
  167. {
  168. "profile": {
  169. "shards": [
  170. {
  171. "id": "[2aE02wS1R8q_QFnYu6vDVQ][twitter][0]", <1>
  172. "searches": [
  173. {
  174. "query": [...], <2>
  175. "rewrite_time": 51443, <3>
  176. "collector": [...] <4>
  177. }
  178. ],
  179. "aggregations": [...] <5>
  180. }
  181. ]
  182. }
  183. }
  184. --------------------------------------------------
  185. // TESTRESPONSE[s/"profile": /"took": $body.took, "timed_out": $body.timed_out, "_shards": $body._shards, "hits": $body.hits, "profile": /]
  186. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  187. // TESTRESPONSE[s/\[2aE02wS1R8q_QFnYu6vDVQ\]\[twitter\]\[0\]/$body.$_path/]
  188. // TESTRESPONSE[s/"query": \[...\]/"query": $body.$_path/]
  189. // TESTRESPONSE[s/"collector": \[...\]/"collector": $body.$_path/]
  190. // TESTRESPONSE[s/"aggregations": \[...\]/"aggregations": []/]
  191. <1> A profile is returned for each shard that participated in the response, and is identified
  192. by a unique ID
  193. <2> Each profile contains a section which holds details about the query execution
  194. <3> Each profile has a single time representing the cumulative rewrite time
  195. <4> Each profile also contains a section about the Lucene Collectors which run the search
  196. <5> Each profile contains a section which holds the details about the aggregation execution
  197. Because a search request may be executed against one or more shards in an index, and a search may cover
  198. one or more indices, the top level element in the profile response is an array of `shard` objects.
  199. Each shard object lists its `id` which uniquely identifies the shard. The ID's format is
  200. `[nodeID][indexName][shardID]`.
  201. The profile itself may consist of one or more "searches", where a search is a query executed against the underlying
  202. Lucene index. Most search requests submitted by the user will only execute a single `search` against the Lucene index.
  203. But occasionally multiple searches will be executed, such as including a global aggregation (which needs to execute
  204. a secondary "match_all" query for the global context).
  205. Inside each `search` object there will be two arrays of profiled information:
  206. a `query` array and a `collector` array. Alongside the `search` object is an `aggregations` object that contains the profile information for the aggregations. In the future, more sections may be added, such as `suggest`, `highlight`, etc.
  207. There will also be a `rewrite` metric showing the total time spent rewriting the query (in nanoseconds).
  208. NOTE: As with other statistics apis, the Profile API supports human readable outputs. This can be turned on by adding
  209. `?human=true` to the query string. In this case, the output contains the additional `time` field containing rounded,
  210. human readable timing information (e.g. `"time": "391,9ms"`, `"time": "123.3micros"`).
  211. [[profiling-queries]]
  212. ==== Profiling Queries
  213. [NOTE]
  214. =======================================
  215. The details provided by the Profile API directly expose Lucene class names and concepts, which means
  216. that complete interpretation of the results require fairly advanced knowledge of Lucene. This
  217. page attempts to give a crash-course in how Lucene executes queries so that you can use the Profile API to successfully
  218. diagnose and debug queries, but it is only an overview. For complete understanding, please refer
  219. to Lucene's documentation and, in places, the code.
  220. With that said, a complete understanding is often not required to fix a slow query. It is usually
  221. sufficient to see that a particular component of a query is slow, and not necessarily understand why
  222. the `advance` phase of that query is the cause, for example.
  223. =======================================
  224. [[query-section]]
  225. ===== `query` Section
  226. The `query` section contains detailed timing of the query tree executed by Lucene on a particular shard.
  227. The overall structure of this query tree will resemble your original Elasticsearch query, but may be slightly
  228. (or sometimes very) different. It will also use similar but not always identical naming. Using our previous
  229. `match` query example, let's analyze the `query` section:
  230. [source,js]
  231. --------------------------------------------------
  232. "query": [
  233. {
  234. "type": "BooleanQuery",
  235. "description": "message:some message:number",
  236. "time_in_nanos": "1873811",
  237. "breakdown": {...}, <1>
  238. "children": [
  239. {
  240. "type": "TermQuery",
  241. "description": "message:some",
  242. "time_in_nanos": "391943",
  243. "breakdown": {...}
  244. },
  245. {
  246. "type": "TermQuery",
  247. "description": "message:number",
  248. "time_in_nanos": "210682",
  249. "breakdown": {...}
  250. }
  251. ]
  252. }
  253. ]
  254. --------------------------------------------------
  255. // TESTRESPONSE[s/^/{\n"took": $body.took,\n"timed_out": $body.timed_out,\n"_shards": $body._shards,\n"hits": $body.hits,\n"profile": {\n"shards": [ {\n"id": "$body.$_path",\n"searches": [{\n/]
  256. // TESTRESPONSE[s/]$/],"rewrite_time": $body.$_path, "collector": $body.$_path}], "aggregations": []}]}}/]
  257. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  258. // TESTRESPONSE[s/"breakdown": \{...\}/"breakdown": $body.$_path/]
  259. <1> The breakdown timings are omitted for simplicity
  260. Based on the profile structure, we can see that our `match` query was rewritten by Lucene into a BooleanQuery with two
  261. clauses (both holding a TermQuery). The `type` field displays the Lucene class name, and often aligns with
  262. the equivalent name in Elasticsearch. The `description` field displays the Lucene explanation text for the query, and
  263. is made available to help differentiating between parts of your query (e.g. both `message:search` and `message:test`
  264. are TermQuery's and would appear identical otherwise.
  265. The `time_in_nanos` field shows that this query took ~1.8ms for the entire BooleanQuery to execute. The recorded time is inclusive
  266. of all children.
  267. The `breakdown` field will give detailed stats about how the time was spent, we'll look at
  268. that in a moment. Finally, the `children` array lists any sub-queries that may be present. Because we searched for two
  269. values ("search test"), our BooleanQuery holds two children TermQueries. They have identical information (type, time,
  270. breakdown, etc). Children are allowed to have their own children.
  271. ====== Timing Breakdown
  272. The `breakdown` component lists detailed timing statistics about low-level Lucene execution:
  273. [source,js]
  274. --------------------------------------------------
  275. "breakdown": {
  276. "score": 51306,
  277. "score_count": 4,
  278. "build_scorer": 2935582,
  279. "build_scorer_count": 1,
  280. "match": 0,
  281. "match_count": 0,
  282. "create_weight": 919297,
  283. "create_weight_count": 1,
  284. "next_doc": 53876,
  285. "next_doc_count": 5,
  286. "advance": 0,
  287. "advance_count": 0,
  288. "compute_max_score": 0,
  289. "compute_max_score_count": 0,
  290. "shallow_advance": 0,
  291. "shallow_advance_count": 0,
  292. "set_min_competitive_score": 0,
  293. "set_min_competitive_score_count": 0
  294. }
  295. --------------------------------------------------
  296. // TESTRESPONSE[s/^/{\n"took": $body.took,\n"timed_out": $body.timed_out,\n"_shards": $body._shards,\n"hits": $body.hits,\n"profile": {\n"shards": [ {\n"id": "$body.$_path",\n"searches": [{\n"query": [{\n"type": "BooleanQuery",\n"description": "message:some message:number",\n"time_in_nanos": $body.$_path,/]
  297. // TESTRESPONSE[s/}$/},\n"children": $body.$_path}],\n"rewrite_time": $body.$_path, "collector": $body.$_path}], "aggregations": []}]}}/]
  298. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  299. Timings are listed in wall-clock nanoseconds and are not normalized at all. All caveats about the overall
  300. `time_in_nanos` apply here. The intention of the breakdown is to give you a feel for A) what machinery in Lucene is
  301. actually eating time, and B) the magnitude of differences in times between the various components. Like the overall time,
  302. the breakdown is inclusive of all children times.
  303. The meaning of the stats are as follows:
  304. [float]
  305. ===== All parameters:
  306. [horizontal]
  307. `create_weight`::
  308. A Query in Lucene must be capable of reuse across multiple IndexSearchers (think of it as the engine that
  309. executes a search against a specific Lucene Index). This puts Lucene in a tricky spot, since many queries
  310. need to accumulate temporary state/statistics associated with the index it is being used against, but the
  311. Query contract mandates that it must be immutable.
  312. {empty} +
  313. {empty} +
  314. To get around this, Lucene asks each query to generate a Weight object which acts as a temporary context
  315. object to hold state associated with this particular (IndexSearcher, Query) tuple. The `weight` metric
  316. shows how long this process takes
  317. `build_scorer`::
  318. This parameter shows how long it takes to build a Scorer for the query. A Scorer is the mechanism that
  319. iterates over matching documents and generates a score per-document (e.g. how well does "foo" match the document?).
  320. Note, this records the time required to generate the Scorer object, not actually score the documents. Some
  321. queries have faster or slower initialization of the Scorer, depending on optimizations, complexity, etc.
  322. {empty} +
  323. {empty} +
  324. This may also show timing associated with caching, if enabled and/or applicable for the query
  325. `next_doc`::
  326. The Lucene method `next_doc` returns Doc ID of the next document matching the query. This statistic shows
  327. the time it takes to determine which document is the next match, a process that varies considerably depending
  328. on the nature of the query. Next_doc is a specialized form of advance() which is more convenient for many
  329. queries in Lucene. It is equivalent to advance(docId() + 1)
  330. `advance`::
  331. `advance` is the "lower level" version of next_doc: it serves the same purpose of finding the next matching
  332. doc, but requires the calling query to perform extra tasks such as identifying and moving past skips, etc.
  333. However, not all queries can use next_doc, so `advance` is also timed for those queries.
  334. {empty} +
  335. {empty} +
  336. Conjunctions (e.g. `must` clauses in a boolean) are typical consumers of `advance`
  337. `matches`::
  338. Some queries, such as phrase queries, match documents using a "two-phase" process. First, the document is
  339. "approximately" matched, and if it matches approximately, it is checked a second time with a more rigorous
  340. (and expensive) process. The second phase verification is what the `matches` statistic measures.
  341. {empty} +
  342. {empty} +
  343. For example, a phrase query first checks a document approximately by ensuring all terms in the phrase are
  344. present in the doc. If all the terms are present, it then executes the second phase verification to ensure
  345. the terms are in-order to form the phrase, which is relatively more expensive than just checking for presence
  346. of the terms.
  347. {empty} +
  348. {empty} +
  349. Because this two-phase process is only used by a handful of queries, the `metric` statistic will often be zero
  350. `score`::
  351. This records the time taken to score a particular document via its Scorer
  352. `*_count`::
  353. Records the number of invocations of the particular method. For example, `"next_doc_count": 2,`
  354. means the `nextDoc()` method was called on two different documents. This can be used to help judge
  355. how selective queries are, by comparing counts between different query components.
  356. [[collectors-section]]
  357. ===== `collectors` Section
  358. The Collectors portion of the response shows high-level execution details. Lucene works by defining a "Collector"
  359. which is responsible for coordinating the traversal, scoring, and collection of matching documents. Collectors
  360. are also how a single query can record aggregation results, execute unscoped "global" queries, execute post-query
  361. filters, etc.
  362. Looking at the previous example:
  363. [source,js]
  364. --------------------------------------------------
  365. "collector": [
  366. {
  367. "name": "CancellableCollector",
  368. "reason": "search_cancelled",
  369. "time_in_nanos": "304311",
  370. "children": [
  371. {
  372. "name": "SimpleTopScoreDocCollector",
  373. "reason": "search_top_hits",
  374. "time_in_nanos": "32273"
  375. }
  376. ]
  377. }
  378. ]
  379. --------------------------------------------------
  380. // TESTRESPONSE[s/^/{\n"took": $body.took,\n"timed_out": $body.timed_out,\n"_shards": $body._shards,\n"hits": $body.hits,\n"profile": {\n"shards": [ {\n"id": "$body.$_path",\n"searches": [{\n"query": $body.$_path,\n"rewrite_time": $body.$_path,/]
  381. // TESTRESPONSE[s/]$/]}], "aggregations": []}]}}/]
  382. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  383. We see a single collector named `SimpleTopScoreDocCollector` wrapped into `CancellableCollector`. `SimpleTopScoreDocCollector` is the default "scoring and sorting"
  384. `Collector` used by Elasticsearch. The `reason` field attempts to give a plain English description of the class name. The
  385. `time_in_nanos` is similar to the time in the Query tree: a wall-clock time inclusive of all children. Similarly, `children` lists
  386. all sub-collectors. The `CancellableCollector` that wraps `SimpleTopScoreDocCollector` is used by Elasticsearch to detect if the current
  387. search was cancelled and stop collecting documents as soon as it occurs.
  388. It should be noted that Collector times are **independent** from the Query times. They are calculated, combined,
  389. and normalized independently! Due to the nature of Lucene's execution, it is impossible to "merge" the times
  390. from the Collectors into the Query section, so they are displayed in separate portions.
  391. For reference, the various collector reasons are:
  392. [horizontal]
  393. `search_sorted`::
  394. A collector that scores and sorts documents. This is the most common collector and will be seen in most
  395. simple searches
  396. `search_count`::
  397. A collector that only counts the number of documents that match the query, but does not fetch the source.
  398. This is seen when `size: 0` is specified
  399. `search_terminate_after_count`::
  400. A collector that terminates search execution after `n` matching documents have been found. This is seen
  401. when the `terminate_after_count` query parameter has been specified
  402. `search_min_score`::
  403. A collector that only returns matching documents that have a score greater than `n`. This is seen when
  404. the top-level parameter `min_score` has been specified.
  405. `search_multi`::
  406. A collector that wraps several other collectors. This is seen when combinations of search, aggregations,
  407. global aggs, and post_filters are combined in a single search.
  408. `search_timeout`::
  409. A collector that halts execution after a specified period of time. This is seen when a `timeout` top-level
  410. parameter has been specified.
  411. `aggregation`::
  412. A collector that Elasticsearch uses to run aggregations against the query scope. A single `aggregation`
  413. collector is used to collect documents for *all* aggregations, so you will see a list of aggregations
  414. in the name rather.
  415. `global_aggregation`::
  416. A collector that executes an aggregation against the global query scope, rather than the specified query.
  417. Because the global scope is necessarily different from the executed query, it must execute its own
  418. match_all query (which you will see added to the Query section) to collect your entire dataset
  419. [[rewrite-section]]
  420. ===== `rewrite` Section
  421. All queries in Lucene undergo a "rewriting" process. A query (and its sub-queries) may be rewritten one or
  422. more times, and the process continues until the query stops changing. This process allows Lucene to perform
  423. optimizations, such as removing redundant clauses, replacing one query for a more efficient execution path,
  424. etc. For example a Boolean -> Boolean -> TermQuery can be rewritten to a TermQuery, because all the Booleans
  425. are unnecessary in this case.
  426. The rewriting process is complex and difficult to display, since queries can change drastically. Rather than
  427. showing the intermediate results, the total rewrite time is simply displayed as a value (in nanoseconds). This
  428. value is cumulative and contains the total time for all queries being rewritten.
  429. ===== A more complex example
  430. To demonstrate a slightly more complex query and the associated results, we can profile the following query:
  431. [source,js]
  432. --------------------------------------------------
  433. GET /twitter/_search
  434. {
  435. "profile": true,
  436. "query": {
  437. "term": {
  438. "user": {
  439. "value": "test"
  440. }
  441. }
  442. },
  443. "aggs": {
  444. "my_scoped_agg": {
  445. "terms": {
  446. "field": "likes"
  447. }
  448. },
  449. "my_global_agg": {
  450. "global": {},
  451. "aggs": {
  452. "my_level_agg": {
  453. "terms": {
  454. "field": "likes"
  455. }
  456. }
  457. }
  458. }
  459. },
  460. "post_filter": {
  461. "match": {
  462. "message": "some"
  463. }
  464. }
  465. }
  466. --------------------------------------------------
  467. // CONSOLE
  468. // TEST[s/_search/_search\?filter_path=profile.shards.id,profile.shards.searches,profile.shards.aggregations/]
  469. // TEST[continued]
  470. This example has:
  471. - A query
  472. - A scoped aggregation
  473. - A global aggregation
  474. - A post_filter
  475. And the response:
  476. [source,js]
  477. --------------------------------------------------
  478. {
  479. ...
  480. "profile": {
  481. "shards": [
  482. {
  483. "id": "[P6-vulHtQRWuD4YnubWb7A][test][0]",
  484. "searches": [
  485. {
  486. "query": [
  487. {
  488. "type": "TermQuery",
  489. "description": "message:some",
  490. "time_in_nanos": "409456",
  491. "breakdown": {
  492. "score": 0,
  493. "build_scorer_count": 1,
  494. "match_count": 0,
  495. "create_weight": 31584,
  496. "next_doc": 0,
  497. "match": 0,
  498. "create_weight_count": 1,
  499. "next_doc_count": 2,
  500. "score_count": 1,
  501. "build_scorer": 377872,
  502. "advance": 0,
  503. "advance_count": 0,
  504. "compute_max_score": 0,
  505. "compute_max_score_count": 0,
  506. "shallow_advance": 0,
  507. "shallow_advance_count": 0,
  508. "set_min_competitive_score": 0,
  509. "set_min_competitive_score_count": 0
  510. }
  511. },
  512. {
  513. "type": "TermQuery",
  514. "description": "user:test",
  515. "time_in_nanos": "303702",
  516. "breakdown": {
  517. "score": 0,
  518. "build_scorer_count": 1,
  519. "match_count": 0,
  520. "create_weight": 185215,
  521. "next_doc": 5936,
  522. "match": 0,
  523. "create_weight_count": 1,
  524. "next_doc_count": 2,
  525. "score_count": 1,
  526. "build_scorer": 112551,
  527. "advance": 0,
  528. "advance_count": 0,
  529. "compute_max_score": 0,
  530. "compute_max_score_count": 0,
  531. "shallow_advance": 0,
  532. "shallow_advance_count": 0,
  533. "set_min_competitive_score": 0,
  534. "set_min_competitive_score_count": 0
  535. }
  536. }
  537. ],
  538. "rewrite_time": 7208,
  539. "collector": [
  540. {
  541. "name": "CancellableCollector",
  542. "reason": "search_cancelled",
  543. "time_in_nanos": 2390,
  544. "children": [
  545. {
  546. "name": "MultiCollector",
  547. "reason": "search_multi",
  548. "time_in_nanos": 1820,
  549. "children": [
  550. {
  551. "name": "FilteredCollector",
  552. "reason": "search_post_filter",
  553. "time_in_nanos": 7735,
  554. "children": [
  555. {
  556. "name": "SimpleTopScoreDocCollector",
  557. "reason": "search_top_hits",
  558. "time_in_nanos": 1328
  559. }
  560. ]
  561. },
  562. {
  563. "name": "MultiBucketCollector: [[my_scoped_agg, my_global_agg]]",
  564. "reason": "aggregation",
  565. "time_in_nanos": 8273
  566. }
  567. ]
  568. }
  569. ]
  570. }
  571. ]
  572. }
  573. ],
  574. "aggregations": [...] <1>
  575. }
  576. ]
  577. }
  578. }
  579. --------------------------------------------------
  580. // TESTRESPONSE[s/"aggregations": \[\.\.\.\]/"aggregations": $body.$_path/]
  581. // TESTRESPONSE[s/\.\.\.//]
  582. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  583. // TESTRESPONSE[s/"id": "\[P6-vulHtQRWuD4YnubWb7A\]\[test\]\[0\]"/"id": $body.profile.shards.0.id/]
  584. <1> The `"aggregations"` portion has been omitted because it will be covered in the next section
  585. As you can see, the output is significantly more verbose than before. All the major portions of the query are
  586. represented:
  587. 1. The first `TermQuery` (user:test) represents the main `term` query
  588. 2. The second `TermQuery` (message:some) represents the `post_filter` query
  589. The Collector tree is fairly straightforward, showing how a single CancellableCollector wraps a MultiCollector
  590. which also wraps a FilteredCollector to execute the post_filter (and in turn wraps the normal scoring SimpleCollector),
  591. a BucketCollector to run all scoped aggregations.
  592. ===== Understanding MultiTermQuery output
  593. A special note needs to be made about the `MultiTermQuery` class of queries. This includes wildcards, regex, and fuzzy
  594. queries. These queries emit very verbose responses, and are not overly structured.
  595. Essentially, these queries rewrite themselves on a per-segment basis. If you imagine the wildcard query `b*`, it technically
  596. can match any token that begins with the letter "b". It would be impossible to enumerate all possible combinations,
  597. so Lucene rewrites the query in context of the segment being evaluated, e.g., one segment may contain the tokens
  598. `[bar, baz]`, so the query rewrites to a BooleanQuery combination of "bar" and "baz". Another segment may only have the
  599. token `[bakery]`, so the query rewrites to a single TermQuery for "bakery".
  600. Due to this dynamic, per-segment rewriting, the clean tree structure becomes distorted and no longer follows a clean
  601. "lineage" showing how one query rewrites into the next. At present time, all we can do is apologize, and suggest you
  602. collapse the details for that query's children if it is too confusing. Luckily, all the timing statistics are correct,
  603. just not the physical layout in the response, so it is sufficient to just analyze the top-level MultiTermQuery and
  604. ignore its children if you find the details too tricky to interpret.
  605. Hopefully this will be fixed in future iterations, but it is a tricky problem to solve and still in-progress :)
  606. [[profiling-aggregations]]
  607. ==== Profiling Aggregations
  608. [[agg-section]]
  609. ===== `aggregations` Section
  610. The `aggregations` section contains detailed timing of the aggregation tree executed by a particular shard.
  611. The overall structure of this aggregation tree will resemble your original Elasticsearch request. Let's
  612. execute the previous query again and look at the aggregation profile this time:
  613. [source,js]
  614. --------------------------------------------------
  615. GET /twitter/_search
  616. {
  617. "profile": true,
  618. "query": {
  619. "term": {
  620. "user": {
  621. "value": "test"
  622. }
  623. }
  624. },
  625. "aggs": {
  626. "my_scoped_agg": {
  627. "terms": {
  628. "field": "likes"
  629. }
  630. },
  631. "my_global_agg": {
  632. "global": {},
  633. "aggs": {
  634. "my_level_agg": {
  635. "terms": {
  636. "field": "likes"
  637. }
  638. }
  639. }
  640. }
  641. },
  642. "post_filter": {
  643. "match": {
  644. "message": "some"
  645. }
  646. }
  647. }
  648. --------------------------------------------------
  649. // CONSOLE
  650. // TEST[s/_search/_search\?filter_path=profile.shards.aggregations/]
  651. // TEST[continued]
  652. This yields the following aggregation profile output:
  653. [source,js]
  654. --------------------------------------------------
  655. {
  656. "profile" : {
  657. "shards" : [
  658. {
  659. "aggregations" : [
  660. {
  661. "type" : "LongTermsAggregator",
  662. "description" : "my_scoped_agg",
  663. "time_in_nanos" : 195386,
  664. "breakdown" : {
  665. "reduce" : 0,
  666. "build_aggregation" : 81171,
  667. "build_aggregation_count" : 1,
  668. "initialize" : 22753,
  669. "initialize_count" : 1,
  670. "reduce_count" : 0,
  671. "collect" : 91456,
  672. "collect_count" : 4
  673. }
  674. },
  675. {
  676. "type" : "GlobalAggregator",
  677. "description" : "my_global_agg",
  678. "time_in_nanos" : 190430,
  679. "breakdown" : {
  680. "reduce" : 0,
  681. "build_aggregation" : 59990,
  682. "build_aggregation_count" : 1,
  683. "initialize" : 29619,
  684. "initialize_count" : 1,
  685. "reduce_count" : 0,
  686. "collect" : 100815,
  687. "collect_count" : 4
  688. },
  689. "children" : [
  690. {
  691. "type" : "LongTermsAggregator",
  692. "description" : "my_level_agg",
  693. "time_in_nanos" : 160329,
  694. "breakdown" : {
  695. "reduce" : 0,
  696. "build_aggregation" : 55712,
  697. "build_aggregation_count" : 1,
  698. "initialize" : 10559,
  699. "initialize_count" : 1,
  700. "reduce_count" : 0,
  701. "collect" : 94052,
  702. "collect_count" : 4
  703. }
  704. }
  705. ]
  706. }
  707. ]
  708. }
  709. ]
  710. }
  711. }
  712. --------------------------------------------------
  713. // TESTRESPONSE[s/\.\.\.//]
  714. // TESTRESPONSE[s/(?<=[" ])\d+(\.\d+)?/$body.$_path/]
  715. // TESTRESPONSE[s/"id": "\[P6-vulHtQRWuD4YnubWb7A\]\[test\]\[0\]"/"id": $body.profile.shards.0.id/]
  716. From the profile structure we can see that the `my_scoped_agg` is internally being run as a `LongTermsAggregator` (because the field it is
  717. aggregating, `likes`, is a numeric field). At the same level, we see a `GlobalAggregator` which comes from `my_global_agg`. That
  718. aggregation then has a child `LongTermsAggregator` which comes from the second term's aggregation on `likes`.
  719. The `time_in_nanos` field shows the time executed by each aggregation, and is inclusive of all children. While the overall time is useful,
  720. the `breakdown` field will give detailed stats about how the time was spent.
  721. ====== Timing Breakdown
  722. The `breakdown` component lists detailed timing statistics about low-level Lucene execution:
  723. [source,js]
  724. --------------------------------------------------
  725. "breakdown": {
  726. "reduce": 0,
  727. "reduce_count": 0,
  728. "build_aggregation": 49765,
  729. "build_aggregation_count": 300,
  730. "initialize": 52785,
  731. "initialize_count": 300,
  732. "reduce_count": 0,
  733. "collect": 3155490036,
  734. "collect_count": 1800
  735. }
  736. --------------------------------------------------
  737. // NOTCONSOLE
  738. Timings are listed in wall-clock nanoseconds and are not normalized at all. All caveats about the overall
  739. `time` apply here. The intention of the breakdown is to give you a feel for A) what machinery in Elasticsearch is
  740. actually eating time, and B) the magnitude of differences in times between the various components. Like the overall time,
  741. the breakdown is inclusive of all children times.
  742. The meaning of the stats are as follows:
  743. [float]
  744. ===== All parameters:
  745. [horizontal]
  746. `initialise`::
  747. This times how long it takes to create and initialise the aggregation before starting to collect documents.
  748. `collect`::
  749. This represents the cumulative time spent in the collect phase of the aggregation. This is where matching documents are passed to the aggregation and the state of the aggregator is updated based on the information contained in the documents.
  750. `build_aggregation`::
  751. This represents the time spent creating the shard level results of the aggregation ready to pass back to the reducing node after the collection of documents is finished.
  752. `reduce`::
  753. This is not currently used and will always report `0`. Currently aggregation profiling only times the shard level parts of the aggregation execution. Timing of the reduce phase will be added later.
  754. `*_count`::
  755. Records the number of invocations of the particular method. For example, `"collect_count": 2,`
  756. means the `collect()` method was called on two different documents.
  757. [[profiling-considerations]]
  758. ==== Profiling Considerations
  759. ===== Performance Notes
  760. Like any profiler, the Profile API introduces a non-negligible overhead to search execution. The act of instrumenting
  761. low-level method calls such as `collect`, `advance`, and `next_doc` can be fairly expensive, since these methods are called
  762. in tight loops. Therefore, profiling should not be enabled in production settings by default, and should not
  763. be compared against non-profiled query times. Profiling is just a diagnostic tool.
  764. There are also cases where special Lucene optimizations are disabled, since they are not amenable to profiling. This
  765. could cause some queries to report larger relative times than their non-profiled counterparts, but in general should
  766. not have a drastic effect compared to other components in the profiled query.
  767. [[profile-limitations]]
  768. ===== Limitations
  769. - Profiling currently does not measure the search fetch phase nor the network overhead
  770. - Profiling also does not account for time spent in the queue, merging shard responses on the coordinating node, or
  771. additional work such as building global ordinals (an internal data structure used to speed up search)
  772. - Profiling statistics are currently not available for suggestions, highlighting, `dfs_query_then_fetch`
  773. - Profiling of the reduce phase of aggregation is currently not available
  774. - The Profiler is still highly experimental. The Profiler is instrumenting parts of Lucene that were
  775. never designed to be exposed in this manner, and so all results should be viewed as a best effort to provide detailed
  776. diagnostics. We hope to improve this over time. If you find obviously wrong numbers, strange query structures, or
  777. other bugs, please report them!