rank-eval.asciidoc 18 KB

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