rank-eval.asciidoc 19 KB

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