rank-eval.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. [[search-rank-eval]]
  2. == Ranking Evaluation API
  3. experimental["The ranking evaluation API is experimental and may be changed or removed completely in a future release, as well as change in non-backwards compatible ways on minor versions updates. Elastic will take a best effort approach to fix any issues, but experimental features are not subject to the support SLA of official GA features."]
  4. The ranking evaluation API allows to evaluate the quality of ranked search
  5. results over a set of typical search queries. Given this set of queries and a
  6. list of manually rated documents, the `_rank_eval` endpoint calculates and
  7. returns typical information retrieval metrics like _mean reciprocal rank_,
  8. _precision_ or _discounted cumulative gain_.
  9. [float]
  10. === Overview
  11. Search quality evaluation starts with looking at the users of your search application, and the things that they are searching for.
  12. Users have a specific _information need_, e.g. they are looking for gift in a web shop or want to book a flight for their next holiday.
  13. They usually enters some search terms into a search box or some other web form.
  14. All of this information, together with meta information about the user (e.g. the browser, location, earlier preferences etc...) then gets translated into a query to the underlying search system.
  15. The challenge for search engineers is to tweak this translation process from user entries to a concrete query in such a way, that the search results contain the most relevant information with respect to the users information need.
  16. This can only be done if the search result quality is evaluated constantly across a representative test suite of typical user queries, so that improvements in the rankings for one particular query doesn't negatively effect the ranking for other types of queries.
  17. In order to get started with search quality evaluation, three basic things are needed:
  18. . a collection of documents you want to evaluate your query performance against, usually one or more indices
  19. . a collection of typical search requests that users enter into your system
  20. . a set of document ratings that judge the documents relevance with respect to a search request+
  21. It is important to note that one set of document ratings is needed per test query, and that
  22. the relevance judgements are based on the information need of the user that entered the query.
  23. The ranking evaluation API provides a convenient way to use this information in a ranking evaluation request to calculate different search evaluation metrics. This gives a first estimation of your overall search quality and give you a measurement to optimize against when fine-tuning various aspect of the query generation in your application.
  24. [float]
  25. === Ranking evaluation request structure
  26. In its most basic form, a request to the `_rank_eval` endpoint has two sections:
  27. [source,js]
  28. -----------------------------
  29. GET /my_index/_rank_eval
  30. {
  31. "requests": [ ... ], <1>
  32. "metric": { <2>
  33. "mean_reciprocal_rank": { ... } <3>
  34. }
  35. }
  36. -----------------------------
  37. // NOTCONSOLE
  38. <1> a set of typical search requests, together with their provided ratings
  39. <2> definition of the evaluation metric to calculate
  40. <3> a specific metric and its parameters
  41. The request section contains several search requests typical to your application, along with the document ratings for each particular search request, e.g.
  42. [source,js]
  43. -----------------------------
  44. "requests": [
  45. {
  46. "id": "amsterdam_query", <1>
  47. "request": { <2>
  48. "query": { "match": { "text": "amsterdam" }}
  49. },
  50. "ratings": [ <3>
  51. { "_index": "my_index", "_id": "doc1", "rating": 0 },
  52. { "_index": "my_index", "_id": "doc2", "rating": 3},
  53. { "_index": "my_index", "_id": "doc3", "rating": 1 }
  54. ]
  55. },
  56. {
  57. "id": "berlin_query",
  58. "request": {
  59. "query": { "match": { "text": "berlin" }}
  60. },
  61. "ratings": [
  62. { "_index": "my_index", "_id": "doc1", "rating": 1 }
  63. ]
  64. }
  65. ]
  66. -----------------------------
  67. // NOTCONSOLE
  68. <1> the search requests id, used to group result details later
  69. <2> the query that is being evaluated
  70. <3> a list of document ratings, each entry containing the documents `_index` and `_id` together with
  71. the rating of the documents relevance with regards to this search request
  72. A document `rating` can be any integer value that expresses the relevance of the document on a user defined scale. For some of the metrics, just giving a binary rating (e.g. `0` for irrelevant and `1` for relevant) will be sufficient, other metrics can use a more fine grained scale.
  73. [float]
  74. === Template based ranking evaluation
  75. As an alternative to having to provide a single query per test request, it is possible to specify query templates in the evaluation request and later refer to them. Queries with similar structure that only differ in their parameters don't have to be repeated all the time in the `requests` section this way. In typical search systems where user inputs usually get filled into a small set of query templates, this helps making the evaluation request more succinct.
  76. [source,js]
  77. --------------------------------
  78. GET /my_index/_rank_eval
  79. {
  80. [...]
  81. "templates": [
  82. {
  83. "id": "match_one_field_query", <1>
  84. "template": { <2>
  85. "inline": {
  86. "query": {
  87. "match": { "{{field}}": { "query": "{{query_string}}" }}
  88. }
  89. }
  90. }
  91. }
  92. ],
  93. "requests": [
  94. {
  95. "id": "amsterdam_query",
  96. "ratings": [ ... ],
  97. "template_id": "match_one_field_query", <3>
  98. "params": { <4>
  99. "query_string": "amsterdam",
  100. "field": "text"
  101. }
  102. },
  103. [...]
  104. }
  105. --------------------------------
  106. // NOTCONSOLE
  107. <1> the template id
  108. <2> the template definition to use
  109. <3> a reference to a previously defined template
  110. <4> the parameters to use to fill the template
  111. [float]
  112. === Available evaluation metrics
  113. The `metric` section determines which of the available evaluation metrics is going to be used.
  114. Currently, the following metrics are supported:
  115. [float]
  116. [[k-precision]]
  117. ==== Precision at K (P@k)
  118. This metric measures the number of relevant results in the top k search results. Its a form of the well known https://en.wikipedia.org/wiki/Information_retrieval#Precision[Precision] metric that only looks at the top k documents. It is the fraction of relevant documents in those first k
  119. search. A precision at 10 (P@10) value of 0.6 then means six out of the 10 top hits are relevant with respect to the users information need.
  120. P@k works well as a simple evaluation metric that has the benefit of being easy to understand and explain.
  121. Documents in the collection need to be rated either as relevant or irrelevant with respect to the current query.
  122. P@k does not take into account where in the top k results the relevant documents occur, so a ranking of ten results that
  123. contains one relevant result in position 10 is equally good as a ranking of ten results that contains one relevant result in position 1.
  124. [source,js]
  125. --------------------------------
  126. GET /twitter/_rank_eval
  127. {
  128. "requests": [
  129. {
  130. "id": "JFK query",
  131. "request": { "query": { "match_all": {}}},
  132. "ratings": []
  133. }],
  134. "metric": {
  135. "precision": {
  136. "k" : 20,
  137. "relevant_rating_threshold": 1,
  138. "ignore_unlabeled": false
  139. }
  140. }
  141. }
  142. --------------------------------
  143. // CONSOLE
  144. // TEST[setup:twitter]
  145. The `precision` metric takes the following optional parameters
  146. [cols="<,<",options="header",]
  147. |=======================================================================
  148. |Parameter |Description
  149. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  150. in the query. Defaults to 10.
  151. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  152. "relevant". Defaults to `1`.
  153. |`ignore_unlabeled` |controls how unlabeled documents in the search results are counted.
  154. 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.
  155. |=======================================================================
  156. [float]
  157. ==== Mean reciprocal rank
  158. For every query in the test suite, this metric calculates the reciprocal of the rank of the
  159. first relevant document. For example finding the first relevant result
  160. in position 3 means the reciprocal rank is 1/3. The reciprocal rank for each query
  161. is averaged across all queries in the test suite to give the https://en.wikipedia.org/wiki/Mean_reciprocal_rank[mean reciprocal rank].
  162. [source,js]
  163. --------------------------------
  164. GET /twitter/_rank_eval
  165. {
  166. "requests": [
  167. {
  168. "id": "JFK query",
  169. "request": { "query": { "match_all": {}}},
  170. "ratings": []
  171. }],
  172. "metric": {
  173. "mean_reciprocal_rank": {
  174. "k" : 20,
  175. "relevant_rating_threshold" : 1
  176. }
  177. }
  178. }
  179. --------------------------------
  180. // CONSOLE
  181. // TEST[setup:twitter]
  182. The `mean_reciprocal_rank` metric takes the following optional parameters
  183. [cols="<,<",options="header",]
  184. |=======================================================================
  185. |Parameter |Description
  186. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  187. in the query. Defaults to 10.
  188. |`relevant_rating_threshold` |Sets the rating threshold above which documents are considered to be
  189. "relevant". Defaults to `1`.
  190. |=======================================================================
  191. [float]
  192. ==== Discounted cumulative gain (DCG)
  193. In contrast to the two metrics above, https://en.wikipedia.org/wiki/Discounted_cumulative_gain[discounted cumulative gain] takes both, the rank and the rating of the search results, into account.
  194. The assumption is that highly relevant documents are more useful for the user when appearing at the top of the result list. Therefore, the DCG formula reduces the contribution that high ratings for documents on lower search ranks have on the overall DCG metric.
  195. [source,js]
  196. --------------------------------
  197. GET /twitter/_rank_eval
  198. {
  199. "requests": [
  200. {
  201. "id": "JFK query",
  202. "request": { "query": { "match_all": {}}},
  203. "ratings": []
  204. }],
  205. "metric": {
  206. "dcg": {
  207. "k" : 20,
  208. "normalize": false
  209. }
  210. }
  211. }
  212. --------------------------------
  213. // CONSOLE
  214. // TEST[setup:twitter]
  215. The `dcg` metric takes the following optional parameters:
  216. [cols="<,<",options="header",]
  217. |=======================================================================
  218. |Parameter |Description
  219. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  220. in the query. Defaults to 10.
  221. |`normalize` | If set to `true`, this metric will calculate the https://en.wikipedia.org/wiki/Discounted_cumulative_gain#Normalized_DCG[Normalized DCG].
  222. |=======================================================================
  223. [float]
  224. ==== Expected Reciprocal Rank (ERR)
  225. Expected Reciprocal Rank (ERR) is an extension of the classical reciprocal rank for the graded relevance case
  226. (Olivier Chapelle, Donald Metzler, Ya Zhang, and Pierre Grinspan. 2009. http://olivier.chapelle.cc/pub/err.pdf[Expected reciprocal rank for graded relevance].)
  227. It is based on the assumption of a cascade model of search, in which a user scans through ranked search
  228. results in order and stops at the first document that satisfies the information need. For this reason, it
  229. is a good metric for question answering and navigation queries, but less so for survey oriented information
  230. needs where the user is interested in finding many relevant documents in the top k results.
  231. The metric models the expectation of the reciprocal of the position at which a user stops reading through
  232. the result list. This means that relevant document in top ranking positions will contribute much to the
  233. overall score. However, the same document will contribute much less to the score if it appears in a lower rank,
  234. even more so if there are some relevant (but maybe less relevant) documents preceding it.
  235. In this way, the ERR metric discounts documents which are shown after very relevant documents. This introduces
  236. a notion of dependency in the ordering of relevant documents that e.g. Precision or DCG don't account for.
  237. [source,js]
  238. --------------------------------
  239. GET /twitter/_rank_eval
  240. {
  241. "requests": [
  242. {
  243. "id": "JFK query",
  244. "request": { "query": { "match_all": {}}},
  245. "ratings": []
  246. }],
  247. "metric": {
  248. "expected_reciprocal_rank": {
  249. "maximum_relevance" : 3,
  250. "k" : 20
  251. }
  252. }
  253. }
  254. --------------------------------
  255. // CONSOLE
  256. // TEST[setup:twitter]
  257. The `expected_reciprocal_rank` metric takes the following parameters:
  258. [cols="<,<",options="header",]
  259. |=======================================================================
  260. |Parameter |Description
  261. | `maximum_relevance` | Mandatory parameter. The highest relevance grade used in the user supplied
  262. relevance judgments.
  263. |`k` | sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  264. in the query. Defaults to 10.
  265. |=======================================================================
  266. [float]
  267. === Response format
  268. The response of the `_rank_eval` endpoint contains the overall calculated result for the defined quality metric,
  269. a `details` section with a breakdown of results for each query in the test suite and an optional `failures` section
  270. that shows potential errors of individual queries. The response has the following format:
  271. [source,js]
  272. --------------------------------
  273. {
  274. "rank_eval": {
  275. "metric_score": 0.4, <1>
  276. "details": {
  277. "my_query_id1": { <2>
  278. "metric_score": 0.6, <3>
  279. "unrated_docs": [ <4>
  280. {
  281. "_index": "my_index",
  282. "_id": "1960795"
  283. }, [...]
  284. ],
  285. "hits": [
  286. {
  287. "hit": { <5>
  288. "_index": "my_index",
  289. "_type": "page",
  290. "_id": "1528558",
  291. "_score": 7.0556192
  292. },
  293. "rating": 1
  294. }, [...]
  295. ],
  296. "metric_details": { <6>
  297. "precision" : {
  298. "relevant_docs_retrieved": 6,
  299. "docs_retrieved": 10
  300. }
  301. }
  302. },
  303. "my_query_id2" : { [...] }
  304. },
  305. "failures": { [...] }
  306. }
  307. }
  308. --------------------------------
  309. // NOTCONSOLE
  310. <1> the overall evaluation quality calculated by the defined metric
  311. <2> the `details` section contains one entry for every query in the original `requests` section, keyed by the search request id
  312. <3> the `metric_score` in the `details` section shows the contribution of this query to the global quality metric score
  313. <4> the `unrated_docs` section contains an `_index` and `_id` entry for each document in the search result for this
  314. query that didn't have a ratings value. This can be used to ask the user to supply ratings for these documents
  315. <5> the `hits` section shows a grouping of the search results with their supplied rating
  316. <6> the `metric_details` give additional information about the calculated quality metric (e.g. how many of the retrieved
  317. documents where relevant). The content varies for each metric but allows for better interpretation of the results