rank-eval.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. It is also possible to use <<pre-registered-templates,templates already stored>> in the cluster state by referencing their id in the templates section.
  171. [source,js]
  172. --------------------------------
  173. GET /my_index/_rank_eval
  174. {
  175. [...]
  176. "templates": [
  177. {
  178. "id": "match_one_field_query", <1>
  179. "template": { <2>
  180. "id": "match_one_field_query"
  181. }
  182. }
  183. ],
  184. "requests": [...]
  185. }
  186. --------------------------------
  187. // NOTCONSOLE
  188. <1> the template id used for requests
  189. <2> the template id stored in the cluster state
  190. ===== Available evaluation metrics
  191. The `metric` section determines which of the available evaluation metrics
  192. will be used. The following metrics are supported:
  193. [discrete]
  194. [[k-precision]]
  195. ===== Precision at K (P@k)
  196. This metric measures the proportion of relevant results in the top k search results.
  197. It's a form of the well-known
  198. {wikipedia}/Evaluation_measures_(information_retrieval)#Precision[Precision]
  199. metric that only looks at the top k documents. It is the fraction of relevant
  200. documents in those first k results. A precision at 10 (P@10) value of 0.6 then
  201. means 6 out of the 10 top hits are relevant with respect to the user's
  202. information need.
  203. P@k works well as a simple evaluation metric that has the benefit of being easy
  204. to understand and explain. Documents in the collection need to be rated as either
  205. relevant or irrelevant with respect to the current query. P@k is a set-based
  206. metric and does not take into account the position of the relevant documents
  207. within the top k results, so a ranking of ten results that contains one
  208. relevant result in position 10 is equally as good as a ranking of ten results
  209. that contains one relevant result in position 1.
  210. [source,console]
  211. --------------------------------
  212. GET /my-index-000001/_rank_eval
  213. {
  214. "requests": [
  215. {
  216. "id": "JFK query",
  217. "request": { "query": { "match_all": {} } },
  218. "ratings": []
  219. } ],
  220. "metric": {
  221. "precision": {
  222. "k": 20,
  223. "relevant_rating_threshold": 1,
  224. "ignore_unlabeled": false
  225. }
  226. }
  227. }
  228. --------------------------------
  229. // TEST[setup:my_index]
  230. The `precision` metric takes the following optional parameters
  231. [cols="<,<",options="header",]
  232. |=======================================================================
  233. |Parameter |Description
  234. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  235. in the query. Defaults to 10.
  236. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  237. "relevant". Defaults to `1`.
  238. |`ignore_unlabeled` |controls how unlabeled documents in the search results are counted.
  239. 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.
  240. |=======================================================================
  241. [discrete]
  242. [[k-recall]]
  243. ===== Recall at K (R@k)
  244. This metric measures the total number of relevant results in the top k search
  245. results. It's a form of the well-known
  246. {wikipedia}/Evaluation_measures_(information_retrieval)#Recall[Recall]
  247. metric. It is the fraction of relevant documents in those first k results
  248. relative to all possible relevant results. A recall at 10 (R@10) value of 0.5 then
  249. means 4 out of 8 relevant documents, with respect to the user's information
  250. need, were retrieved in the 10 top hits.
  251. R@k works well as a simple evaluation metric that has the benefit of being easy
  252. to understand and explain. Documents in the collection need to be rated as either
  253. relevant or irrelevant with respect to the current query. R@k is a set-based
  254. metric and does not take into account the position of the relevant documents
  255. within the top k results, so a ranking of ten results that contains one
  256. relevant result in position 10 is equally as good as a ranking of ten results
  257. that contains one relevant result in position 1.
  258. [source,console]
  259. --------------------------------
  260. GET /my-index-000001/_rank_eval
  261. {
  262. "requests": [
  263. {
  264. "id": "JFK query",
  265. "request": { "query": { "match_all": {} } },
  266. "ratings": []
  267. } ],
  268. "metric": {
  269. "recall": {
  270. "k": 20,
  271. "relevant_rating_threshold": 1
  272. }
  273. }
  274. }
  275. --------------------------------
  276. // TEST[setup:my_index]
  277. The `recall` metric takes the following optional parameters
  278. [cols="<,<",options="header",]
  279. |=======================================================================
  280. |Parameter |Description
  281. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  282. in the query. Defaults to 10.
  283. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  284. "relevant". Defaults to `1`.
  285. |=======================================================================
  286. [discrete]
  287. ===== Mean reciprocal rank
  288. For every query in the test suite, this metric calculates the reciprocal of the
  289. rank of the first relevant document. For example, finding the first relevant
  290. result in position 3 means the reciprocal rank is 1/3. The reciprocal rank for
  291. each query is averaged across all queries in the test suite to give the
  292. {wikipedia}/Mean_reciprocal_rank[mean reciprocal rank].
  293. [source,console]
  294. --------------------------------
  295. GET /my-index-000001/_rank_eval
  296. {
  297. "requests": [
  298. {
  299. "id": "JFK query",
  300. "request": { "query": { "match_all": {} } },
  301. "ratings": []
  302. } ],
  303. "metric": {
  304. "mean_reciprocal_rank": {
  305. "k": 20,
  306. "relevant_rating_threshold": 1
  307. }
  308. }
  309. }
  310. --------------------------------
  311. // TEST[setup:my_index]
  312. The `mean_reciprocal_rank` metric takes the following optional parameters
  313. [cols="<,<",options="header",]
  314. |=======================================================================
  315. |Parameter |Description
  316. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  317. in the query. Defaults to 10.
  318. |`relevant_rating_threshold` |Sets the rating threshold above which documents are considered to be
  319. "relevant". Defaults to `1`.
  320. |=======================================================================
  321. [discrete]
  322. ===== Discounted cumulative gain (DCG)
  323. In contrast to the two metrics above,
  324. {wikipedia}/Discounted_cumulative_gain[discounted cumulative gain]
  325. takes both the rank and the rating of the search results into account.
  326. The assumption is that highly relevant documents are more useful for the user
  327. when appearing at the top of the result list. Therefore, the DCG formula reduces
  328. the contribution that high ratings for documents on lower search ranks have on
  329. the overall DCG metric.
  330. [source,console]
  331. --------------------------------
  332. GET /my-index-000001/_rank_eval
  333. {
  334. "requests": [
  335. {
  336. "id": "JFK query",
  337. "request": { "query": { "match_all": {} } },
  338. "ratings": []
  339. } ],
  340. "metric": {
  341. "dcg": {
  342. "k": 20,
  343. "normalize": false
  344. }
  345. }
  346. }
  347. --------------------------------
  348. // TEST[setup:my_index]
  349. The `dcg` metric takes the following optional parameters:
  350. [cols="<,<",options="header",]
  351. |=======================================================================
  352. |Parameter |Description
  353. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  354. in the query. Defaults to 10.
  355. |`normalize` | If set to `true`, this metric will calculate the {wikipedia}/Discounted_cumulative_gain#Normalized_DCG[Normalized DCG].
  356. |=======================================================================
  357. [discrete]
  358. ===== Expected Reciprocal Rank (ERR)
  359. Expected Reciprocal Rank (ERR) is an extension of the classical reciprocal rank
  360. for the graded relevance case (Olivier Chapelle, Donald Metzler, Ya Zhang, and
  361. Pierre Grinspan. 2009.
  362. https://olivier.chapelle.cc/pub/err.pdf[Expected reciprocal rank for graded relevance].)
  363. It is based on the assumption of a cascade model of search, in which a user
  364. scans through ranked search results in order and stops at the first document
  365. that satisfies the information need. For this reason, it is a good metric for
  366. question answering and navigation queries, but less so for survey-oriented
  367. information needs where the user is interested in finding many relevant
  368. documents in the top k results.
  369. The metric models the expectation of the reciprocal of the position at which a
  370. user stops reading through the result list. This means that a relevant document
  371. in a top ranking position will have a large contribution to the overall score.
  372. However, the same document will contribute much less to the score if it appears
  373. in a lower rank; even more so if there are some relevant (but maybe less relevant)
  374. documents preceding it. In this way, the ERR metric discounts documents that
  375. are shown after very relevant documents. This introduces a notion of dependency
  376. in the ordering of relevant documents that e.g. Precision or DCG don't account
  377. for.
  378. [source,console]
  379. --------------------------------
  380. GET /my-index-000001/_rank_eval
  381. {
  382. "requests": [
  383. {
  384. "id": "JFK query",
  385. "request": { "query": { "match_all": {} } },
  386. "ratings": []
  387. } ],
  388. "metric": {
  389. "expected_reciprocal_rank": {
  390. "maximum_relevance": 3,
  391. "k": 20
  392. }
  393. }
  394. }
  395. --------------------------------
  396. // TEST[setup:my_index]
  397. The `expected_reciprocal_rank` metric takes the following parameters:
  398. [cols="<,<",options="header",]
  399. |=======================================================================
  400. |Parameter |Description
  401. | `maximum_relevance` | Mandatory parameter. The highest relevance grade used in the user-supplied
  402. relevance judgments.
  403. |`k` | sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  404. in the query. Defaults to 10.
  405. |=======================================================================
  406. ===== Response format
  407. The response of the `_rank_eval` endpoint contains the overall calculated result
  408. for the defined quality metric, a `details` section with a breakdown of results
  409. for each query in the test suite and an optional `failures` section that shows
  410. potential errors of individual queries. The response has the following format:
  411. [source,js]
  412. --------------------------------
  413. {
  414. "rank_eval": {
  415. "metric_score": 0.4, <1>
  416. "details": {
  417. "my_query_id1": { <2>
  418. "metric_score": 0.6, <3>
  419. "unrated_docs": [ <4>
  420. {
  421. "_index": "my-index-000001",
  422. "_id": "1960795"
  423. }, ...
  424. ],
  425. "hits": [
  426. {
  427. "hit": { <5>
  428. "_index": "my-index-000001",
  429. "_type": "page",
  430. "_id": "1528558",
  431. "_score": 7.0556192
  432. },
  433. "rating": 1
  434. }, ...
  435. ],
  436. "metric_details": { <6>
  437. "precision": {
  438. "relevant_docs_retrieved": 6,
  439. "docs_retrieved": 10
  440. }
  441. }
  442. },
  443. "my_query_id2": { [... ] }
  444. },
  445. "failures": { [... ] }
  446. }
  447. }
  448. --------------------------------
  449. // NOTCONSOLE
  450. <1> the overall evaluation quality calculated by the defined metric
  451. <2> the `details` section contains one entry for every query in the original `requests` section, keyed by the search request id
  452. <3> the `metric_score` in the `details` section shows the contribution of this query to the global quality metric score
  453. <4> the `unrated_docs` section contains an `_index` and `_id` entry for each document in the search result for this
  454. query that didn't have a ratings value. This can be used to ask the user to supply ratings for these documents
  455. <5> the `hits` section shows a grouping of the search results with their supplied ratings
  456. <6> the `metric_details` give additional information about the calculated quality metric (e.g. how many of the retrieved
  457. documents were relevant). The content varies for each metric but allows for better interpretation of the results