rank-eval.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. [[search-rank-eval]]
  2. === Ranking evaluation API
  3. ++++
  4. <titleabbrev>Ranking evaluation</titleabbrev>
  5. ++++
  6. Allows you to evaluate the quality of ranked search results over a set of
  7. typical search queries.
  8. [[search-rank-eval-api-request]]
  9. ==== {api-request-title}
  10. `GET /<target>/_rank_eval`
  11. `POST /<target>/_rank_eval`
  12. [[search-rank-eval-api-desc]]
  13. ==== {api-description-title}
  14. The ranking evaluation API allows you to evaluate the quality of ranked search
  15. results over a set of typical search queries. Given this set of queries and a
  16. list of manually rated documents, the `_rank_eval` endpoint calculates and
  17. returns typical information retrieval metrics like _mean reciprocal rank_,
  18. _precision_ or _discounted cumulative gain_.
  19. Search quality evaluation starts with looking at the users of your search
  20. application, and the things that they are searching for. Users have a specific
  21. _information need_; for example, they are looking for gift in a web shop or want
  22. to book a flight for their next holiday. They usually enter some search terms
  23. into a search box or some other web form. All of this information, together with
  24. meta information about the user (for example the browser, location, earlier
  25. preferences and so on) then gets translated into a query to the underlying
  26. search system.
  27. The challenge for search engineers is to tweak this translation process from
  28. user entries to a concrete query, in such a way that the search results contain
  29. the most relevant information with respect to the user's information need. This
  30. can only be done if the search result quality is evaluated constantly across a
  31. representative test suite of typical user queries, so that improvements in the
  32. rankings for one particular query don't negatively affect the ranking for
  33. other types of queries.
  34. In order to get started with search quality evaluation, you need three basic
  35. things:
  36. . A collection of documents you want to evaluate your query performance against,
  37. usually one or more data streams or indices.
  38. . A collection of typical search requests that users enter into your system.
  39. . A set of document ratings that represent the documents' relevance with respect
  40. to a search request.
  41. It is important to note that one set of document ratings is needed per test
  42. query, and that the relevance judgements are based on the information need of
  43. the user that entered the query.
  44. The ranking evaluation API provides a convenient way to use this information in
  45. a ranking evaluation request to calculate different search evaluation metrics.
  46. This gives you a first estimation of your overall search quality, as well as a
  47. measurement to optimize against when fine-tuning various aspect of the query
  48. generation in your application.
  49. [[search-rank-eval-api-path-params]]
  50. ==== {api-path-parms-title}
  51. `<target>`::
  52. (Optional, string)
  53. Comma-separated list of data streams, indices, and index aliases used to limit
  54. the request. Wildcard (`*`) expressions are supported.
  55. +
  56. To target all data streams and indices in a cluster, omit this parameter or use
  57. `_all` or `*`.
  58. [[search-rank-eval-api-query-params]]
  59. ==== {api-query-parms-title}
  60. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  61. +
  62. Defaults to `true`.
  63. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  64. +
  65. --
  66. Defaults to `open`.
  67. --
  68. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  69. [[search-rank-eval-api-example]]
  70. ==== {api-examples-title}
  71. In its most basic form, a request to the `_rank_eval` endpoint has two sections:
  72. [source,js]
  73. -----------------------------
  74. GET /my-index-000001/_rank_eval
  75. {
  76. "requests": [ ... ], <1>
  77. "metric": { <2>
  78. "mean_reciprocal_rank": { ... } <3>
  79. }
  80. }
  81. -----------------------------
  82. // NOTCONSOLE
  83. <1> a set of typical search requests, together with their provided ratings
  84. <2> definition of the evaluation metric to calculate
  85. <3> a specific metric and its parameters
  86. The request section contains several search requests typical to your
  87. application, along with the document ratings for each particular search request.
  88. [source,js]
  89. -----------------------------
  90. GET /my-index-000001/_rank_eval
  91. {
  92. "requests": [
  93. {
  94. "id": "amsterdam_query", <1>
  95. "request": { <2>
  96. "query": { "match": { "text": "amsterdam" } }
  97. },
  98. "ratings": [ <3>
  99. { "_index": "my-index-000001", "_id": "doc1", "rating": 0 },
  100. { "_index": "my-index-000001", "_id": "doc2", "rating": 3 },
  101. { "_index": "my-index-000001", "_id": "doc3", "rating": 1 }
  102. ]
  103. },
  104. {
  105. "id": "berlin_query",
  106. "request": {
  107. "query": { "match": { "text": "berlin" } }
  108. },
  109. "ratings": [
  110. { "_index": "my-index-000001", "_id": "doc1", "rating": 1 }
  111. ]
  112. }
  113. ]
  114. }
  115. -----------------------------
  116. // NOTCONSOLE
  117. <1> The search request's ID, used to group result details later.
  118. <2> The query being evaluated.
  119. <3> A list of document ratings. Each entry contains the following arguments:
  120. - `_index`: The document's index. For data streams, this should be the
  121. document's backing index.
  122. - `_id`: The document ID.
  123. - `rating`: The document's relevance with regard to this search request.
  124. A document `rating` can be any integer value that expresses the relevance of the
  125. document on a user-defined scale. For some of the metrics, just giving a binary
  126. rating (for example `0` for irrelevant and `1` for relevant) will be sufficient,
  127. while other metrics can use a more fine-grained scale.
  128. ===== Template-based ranking evaluation
  129. As an alternative to having to provide a single query per test request, it is
  130. possible to specify query templates in the evaluation request and later refer to
  131. them. This way, queries with a similar structure that differ only in their
  132. parameters don't have to be repeated all the time in the `requests` section.
  133. In typical search systems, where user inputs usually get filled into a small
  134. set of query templates, this helps make the evaluation request more succinct.
  135. [source,js]
  136. --------------------------------
  137. GET /my-index-000001/_rank_eval
  138. {
  139. [...]
  140. "templates": [
  141. {
  142. "id": "match_one_field_query", <1>
  143. "template": { <2>
  144. "inline": {
  145. "query": {
  146. "match": { "{{field}}": { "query": "{{query_string}}" }}
  147. }
  148. }
  149. }
  150. }
  151. ],
  152. "requests": [
  153. {
  154. "id": "amsterdam_query",
  155. "ratings": [ ... ],
  156. "template_id": "match_one_field_query", <3>
  157. "params": { <4>
  158. "query_string": "amsterdam",
  159. "field": "text"
  160. }
  161. },
  162. [...]
  163. }
  164. --------------------------------
  165. // NOTCONSOLE
  166. <1> the template id
  167. <2> the template definition to use
  168. <3> a reference to a previously defined template
  169. <4> the parameters to use to fill the template
  170. ===== Available evaluation metrics
  171. The `metric` section determines which of the available evaluation metrics
  172. will be used. The following metrics are supported:
  173. [discrete]
  174. [[k-precision]]
  175. ===== Precision at K (P@k)
  176. This metric measures the proportion of relevant results in the top k search results.
  177. It's a form of the well-known
  178. {wikipedia}/Evaluation_measures_(information_retrieval)#Precision[Precision]
  179. metric that only looks at the top k documents. It is the fraction of relevant
  180. documents in those first k results. A precision at 10 (P@10) value of 0.6 then
  181. means 6 out of the 10 top hits are relevant with respect to the user's
  182. information need.
  183. P@k works well as a simple evaluation metric that has the benefit of being easy
  184. to understand and explain. Documents in the collection need to be rated as either
  185. relevant or irrelevant with respect to the current query. P@k is a set-based
  186. metric and does not take into account the position of the relevant documents
  187. within the top k results, so a ranking of ten results that contains one
  188. relevant result in position 10 is equally as good as a ranking of ten results
  189. that contains one relevant result in position 1.
  190. [source,console]
  191. --------------------------------
  192. GET /my-index-000001/_rank_eval
  193. {
  194. "requests": [
  195. {
  196. "id": "JFK query",
  197. "request": { "query": { "match_all": {} } },
  198. "ratings": []
  199. } ],
  200. "metric": {
  201. "precision": {
  202. "k": 20,
  203. "relevant_rating_threshold": 1,
  204. "ignore_unlabeled": false
  205. }
  206. }
  207. }
  208. --------------------------------
  209. // TEST[setup:my_index]
  210. The `precision` metric takes the following optional parameters
  211. [cols="<,<",options="header",]
  212. |=======================================================================
  213. |Parameter |Description
  214. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  215. in the query. Defaults to 10.
  216. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  217. "relevant". Defaults to `1`.
  218. |`ignore_unlabeled` |controls how unlabeled documents in the search results are counted.
  219. If set to 'true', unlabeled documents are ignored and neither count as relevant or irrelevant. Set to 'false' (the default), they are treated as irrelevant.
  220. |=======================================================================
  221. [discrete]
  222. [[k-recall]]
  223. ===== Recall at K (R@k)
  224. This metric measures the total number of relevant results in the top k search
  225. results. It's a form of the well-known
  226. {wikipedia}/Evaluation_measures_(information_retrieval)#Recall[Recall]
  227. metric. It is the fraction of relevant documents in those first k results
  228. relative to all possible relevant results. A recall at 10 (R@10) value of 0.5 then
  229. means 4 out of 8 relevant documents, with respect to the user's information
  230. need, were retrieved in the 10 top hits.
  231. R@k works well as a simple evaluation metric that has the benefit of being easy
  232. to understand and explain. Documents in the collection need to be rated as either
  233. relevant or irrelevant with respect to the current query. R@k is a set-based
  234. metric and does not take into account the position of the relevant documents
  235. within the top k results, so a ranking of ten results that contains one
  236. relevant result in position 10 is equally as good as a ranking of ten results
  237. that contains one relevant result in position 1.
  238. [source,console]
  239. --------------------------------
  240. GET /my-index-000001/_rank_eval
  241. {
  242. "requests": [
  243. {
  244. "id": "JFK query",
  245. "request": { "query": { "match_all": {} } },
  246. "ratings": []
  247. } ],
  248. "metric": {
  249. "recall": {
  250. "k": 20,
  251. "relevant_rating_threshold": 1
  252. }
  253. }
  254. }
  255. --------------------------------
  256. // TEST[setup:my_index]
  257. The `recall` metric takes the following optional parameters
  258. [cols="<,<",options="header",]
  259. |=======================================================================
  260. |Parameter |Description
  261. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  262. in the query. Defaults to 10.
  263. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  264. "relevant". Defaults to `1`.
  265. |=======================================================================
  266. [discrete]
  267. ===== Mean reciprocal rank
  268. For every query in the test suite, this metric calculates the reciprocal of the
  269. rank of the first relevant document. For example, finding the first relevant
  270. result in position 3 means the reciprocal rank is 1/3. The reciprocal rank for
  271. each query is averaged across all queries in the test suite to give the
  272. {wikipedia}/Mean_reciprocal_rank[mean reciprocal rank].
  273. [source,console]
  274. --------------------------------
  275. GET /my-index-000001/_rank_eval
  276. {
  277. "requests": [
  278. {
  279. "id": "JFK query",
  280. "request": { "query": { "match_all": {} } },
  281. "ratings": []
  282. } ],
  283. "metric": {
  284. "mean_reciprocal_rank": {
  285. "k": 20,
  286. "relevant_rating_threshold": 1
  287. }
  288. }
  289. }
  290. --------------------------------
  291. // TEST[setup:my_index]
  292. The `mean_reciprocal_rank` metric takes the following optional parameters
  293. [cols="<,<",options="header",]
  294. |=======================================================================
  295. |Parameter |Description
  296. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  297. in the query. Defaults to 10.
  298. |`relevant_rating_threshold` |Sets the rating threshold above which documents are considered to be
  299. "relevant". Defaults to `1`.
  300. |=======================================================================
  301. [discrete]
  302. ===== Discounted cumulative gain (DCG)
  303. In contrast to the two metrics above,
  304. {wikipedia}/Discounted_cumulative_gain[discounted cumulative gain]
  305. takes both the rank and the rating of the search results into account.
  306. The assumption is that highly relevant documents are more useful for the user
  307. when appearing at the top of the result list. Therefore, the DCG formula reduces
  308. the contribution that high ratings for documents on lower search ranks have on
  309. the overall DCG metric.
  310. [source,console]
  311. --------------------------------
  312. GET /my-index-000001/_rank_eval
  313. {
  314. "requests": [
  315. {
  316. "id": "JFK query",
  317. "request": { "query": { "match_all": {} } },
  318. "ratings": []
  319. } ],
  320. "metric": {
  321. "dcg": {
  322. "k": 20,
  323. "normalize": false
  324. }
  325. }
  326. }
  327. --------------------------------
  328. // TEST[setup:my_index]
  329. The `dcg` metric takes the following optional parameters:
  330. [cols="<,<",options="header",]
  331. |=======================================================================
  332. |Parameter |Description
  333. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  334. in the query. Defaults to 10.
  335. |`normalize` | If set to `true`, this metric will calculate the {wikipedia}/Discounted_cumulative_gain#Normalized_DCG[Normalized DCG].
  336. |=======================================================================
  337. [discrete]
  338. ===== Expected Reciprocal Rank (ERR)
  339. Expected Reciprocal Rank (ERR) is an extension of the classical reciprocal rank
  340. for the graded relevance case (Olivier Chapelle, Donald Metzler, Ya Zhang, and
  341. Pierre Grinspan. 2009.
  342. https://olivier.chapelle.cc/pub/err.pdf[Expected reciprocal rank for graded relevance].)
  343. It is based on the assumption of a cascade model of search, in which a user
  344. scans through ranked search results in order and stops at the first document
  345. that satisfies the information need. For this reason, it is a good metric for
  346. question answering and navigation queries, but less so for survey-oriented
  347. information needs where the user is interested in finding many relevant
  348. documents in the top k results.
  349. The metric models the expectation of the reciprocal of the position at which a
  350. user stops reading through the result list. This means that a relevant document
  351. in a top ranking position will have a large contribution to the overall score.
  352. However, the same document will contribute much less to the score if it appears
  353. in a lower rank; even more so if there are some relevant (but maybe less relevant)
  354. documents preceding it. In this way, the ERR metric discounts documents that
  355. are shown after very relevant documents. This introduces a notion of dependency
  356. in the ordering of relevant documents that e.g. Precision or DCG don't account
  357. for.
  358. [source,console]
  359. --------------------------------
  360. GET /my-index-000001/_rank_eval
  361. {
  362. "requests": [
  363. {
  364. "id": "JFK query",
  365. "request": { "query": { "match_all": {} } },
  366. "ratings": []
  367. } ],
  368. "metric": {
  369. "expected_reciprocal_rank": {
  370. "maximum_relevance": 3,
  371. "k": 20
  372. }
  373. }
  374. }
  375. --------------------------------
  376. // TEST[setup:my_index]
  377. The `expected_reciprocal_rank` metric takes the following parameters:
  378. [cols="<,<",options="header",]
  379. |=======================================================================
  380. |Parameter |Description
  381. | `maximum_relevance` | Mandatory parameter. The highest relevance grade used in the user-supplied
  382. relevance judgments.
  383. |`k` | sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  384. in the query. Defaults to 10.
  385. |=======================================================================
  386. ===== Response format
  387. The response of the `_rank_eval` endpoint contains the overall calculated result
  388. for the defined quality metric, a `details` section with a breakdown of results
  389. for each query in the test suite and an optional `failures` section that shows
  390. potential errors of individual queries. The response has the following format:
  391. [source,js]
  392. --------------------------------
  393. {
  394. "rank_eval": {
  395. "metric_score": 0.4, <1>
  396. "details": {
  397. "my_query_id1": { <2>
  398. "metric_score": 0.6, <3>
  399. "unrated_docs": [ <4>
  400. {
  401. "_index": "my-index-000001",
  402. "_id": "1960795"
  403. }, ...
  404. ],
  405. "hits": [
  406. {
  407. "hit": { <5>
  408. "_index": "my-index-000001",
  409. "_type": "page",
  410. "_id": "1528558",
  411. "_score": 7.0556192
  412. },
  413. "rating": 1
  414. }, ...
  415. ],
  416. "metric_details": { <6>
  417. "precision": {
  418. "relevant_docs_retrieved": 6,
  419. "docs_retrieved": 10
  420. }
  421. }
  422. },
  423. "my_query_id2": { [... ] }
  424. },
  425. "failures": { [... ] }
  426. }
  427. }
  428. --------------------------------
  429. // NOTCONSOLE
  430. <1> the overall evaluation quality calculated by the defined metric
  431. <2> the `details` section contains one entry for every query in the original `requests` section, keyed by the search request id
  432. <3> the `metric_score` in the `details` section shows the contribution of this query to the global quality metric score
  433. <4> the `unrated_docs` section contains an `_index` and `_id` entry for each document in the search result for this
  434. query that didn't have a ratings value. This can be used to ask the user to supply ratings for these documents
  435. <5> the `hits` section shows a grouping of the search results with their supplied ratings
  436. <6> the `metric_details` give additional information about the calculated quality metric (e.g. how many of the retrieved
  437. documents were relevant). The content varies for each metric but allows for better interpretation of the results