rank-eval.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 temlate
  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. ==== Precision at K (P@k)
  117. 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
  118. 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.
  119. P@k works well as a simple evaluation metric that has the benefit of being easy to understand and explain.
  120. Documents in the collection need to be rated either as relevant or irrelevant with respect to the current query.
  121. P@k does not take into account where in the top k results the relevant documents occur, so a ranking of ten results that
  122. contains one relevant result in position 10 is equally good as a ranking of ten results that contains one relevant result in position 1.
  123. [source,js]
  124. --------------------------------
  125. GET /twitter/_rank_eval
  126. {
  127. "requests": [
  128. {
  129. "id": "JFK query",
  130. "request": { "query": { "match_all": {}}},
  131. "ratings": []
  132. }],
  133. "metric": {
  134. "precision": {
  135. "k" : 20,
  136. "relevant_rating_threshold": 1,
  137. "ignore_unlabeled": false
  138. }
  139. }
  140. }
  141. --------------------------------
  142. // CONSOLE
  143. // TEST[setup:twitter]
  144. The `precision` metric takes the following optional parameters
  145. [cols="<,<",options="header",]
  146. |=======================================================================
  147. |Parameter |Description
  148. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  149. in the query. Defaults to 10.
  150. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  151. "relevant". Defaults to `1`.
  152. |`ignore_unlabeled` |controls how unlabeled documents in the search results are counted.
  153. 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.
  154. |=======================================================================
  155. [float]
  156. ==== Mean reciprocal rank
  157. For every query in the test suite, this metric calculates the reciprocal of the rank of the
  158. first relevant document. For example finding the first relevant result
  159. in position 3 means the reciprocal rank is 1/3. The reciprocal rank for each query
  160. is averaged across all queries in the test suite to give the https://en.wikipedia.org/wiki/Mean_reciprocal_rank[mean reciprocal rank].
  161. [source,js]
  162. --------------------------------
  163. GET /twitter/_rank_eval
  164. {
  165. "requests": [
  166. {
  167. "id": "JFK query",
  168. "request": { "query": { "match_all": {}}},
  169. "ratings": []
  170. }],
  171. "metric": {
  172. "mean_reciprocal_rank": {
  173. "k" : 20,
  174. "relevant_rating_threshold" : 1
  175. }
  176. }
  177. }
  178. --------------------------------
  179. // CONSOLE
  180. // TEST[setup:twitter]
  181. The `mean_reciprocal_rank` metric takes the following optional parameters
  182. [cols="<,<",options="header",]
  183. |=======================================================================
  184. |Parameter |Description
  185. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  186. in the query. Defaults to 10.
  187. |`relevant_rating_threshold` |Sets the rating threshold above which documents are considered to be
  188. "relevant". Defaults to `1`.
  189. |=======================================================================
  190. [float]
  191. ==== Discounted cumulative gain (DCG)
  192. 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.
  193. 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.
  194. [source,js]
  195. --------------------------------
  196. GET /twitter/_rank_eval
  197. {
  198. "requests": [
  199. {
  200. "id": "JFK query",
  201. "request": { "query": { "match_all": {}}},
  202. "ratings": []
  203. }],
  204. "metric": {
  205. "dcg": {
  206. "k" : 20,
  207. "normalize": false
  208. }
  209. }
  210. }
  211. --------------------------------
  212. // CONSOLE
  213. // TEST[setup:twitter]
  214. The `dcg` metric takes the following optional parameters:
  215. [cols="<,<",options="header",]
  216. |=======================================================================
  217. |Parameter |Description
  218. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  219. in the query. Defaults to 10.
  220. |`normalize` | If set to `true`, this metric will calculate the https://en.wikipedia.org/wiki/Discounted_cumulative_gain#Normalized_DCG[Normalized DCG].
  221. |=======================================================================
  222. [float]
  223. === Response format
  224. The response of the `_rank_eval` endpoint contains the overall calculated result for the defined quality metric,
  225. a `details` section with a breakdown of results for each query in the test suite and an optional `failures` section
  226. that shows potential errors of individual queries. The response has the following format:
  227. [source,js]
  228. --------------------------------
  229. {
  230. "rank_eval": {
  231. "quality_level": 0.4, <1>
  232. "details": {
  233. "my_query_id1": { <2>
  234. "quality_level": 0.6, <3>
  235. "unknown_docs": [ <4>
  236. {
  237. "_index": "my_index",
  238. "_id": "1960795"
  239. }, [...]
  240. ],
  241. "hits": [
  242. {
  243. "hit": { <5>
  244. "_index": "my_index",
  245. "_type": "page",
  246. "_id": "1528558",
  247. "_score": 7.0556192
  248. },
  249. "rating": 1
  250. }, [...]
  251. ],
  252. "metric_details": { <6>
  253. "precision" : {
  254. "relevant_docs_retrieved": 6,
  255. "docs_retrieved": 10
  256. }
  257. }
  258. },
  259. "my_query_id2" : { [...] }
  260. },
  261. "failures": { [...] }
  262. }
  263. }
  264. --------------------------------
  265. // NOTCONSOLE
  266. <1> the overall evaluation quality calculated by the defined metric
  267. <2> the `details` section contains one entry for every query in the original `requests` section, keyed by the search request id
  268. <3> the `quality_level` in the `details` section shows the contribution of this query to the global quality score
  269. <4> the `unknown_docs` section contains an `_index` and `_id` entry for each document in the search result for this
  270. query that didn't have a ratings value. This can be used to ask the user to supply ratings for these documents
  271. <5> the `hits` section shows a grouping of the search results with their supplied rating
  272. <6> the `metric_details` give additional information about the calculated quality metric (e.g. how many of the retrieved
  273. documents where relevant). The content varies for each metric but allows for better interpretation of the results