rank-eval.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 users 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 doesn't negatively effect the ranking for
  30. other types of queries.
  31. In order to get started with search quality evaluation, three basic things are
  32. needed:
  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 judge the documents relevance with respect to a
  37. 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 a first estimation of your overall search quality and give you 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::{docdir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  54. include::{docdir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  55. +
  56. --
  57. Defaults to `open`.
  58. --
  59. include::{docdir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  60. [[search-rank-eval-api-example]]
  61. ==== {api-examples-title}
  62. In its most basic form, a request to the `_rank_eval` endpoint has two sections:
  63. [source,js]
  64. -----------------------------
  65. GET /my_index/_rank_eval
  66. {
  67. "requests": [ ... ], <1>
  68. "metric": { <2>
  69. "mean_reciprocal_rank": { ... } <3>
  70. }
  71. }
  72. -----------------------------
  73. // NOTCONSOLE
  74. <1> a set of typical search requests, together with their provided ratings
  75. <2> definition of the evaluation metric to calculate
  76. <3> a specific metric and its parameters
  77. The request section contains several search requests typical to your
  78. application, along with the document ratings for each particular search request.
  79. [source,js]
  80. -----------------------------
  81. GET /my_index/_rank_eval
  82. {
  83. "requests": [
  84. {
  85. "id": "amsterdam_query", <1>
  86. "request": { <2>
  87. "query": { "match": { "text": "amsterdam" }}
  88. },
  89. "ratings": [ <3>
  90. { "_index": "my_index", "_id": "doc1", "rating": 0 },
  91. { "_index": "my_index", "_id": "doc2", "rating": 3},
  92. { "_index": "my_index", "_id": "doc3", "rating": 1 }
  93. ]
  94. },
  95. {
  96. "id": "berlin_query",
  97. "request": {
  98. "query": { "match": { "text": "berlin" }}
  99. },
  100. "ratings": [
  101. { "_index": "my_index", "_id": "doc1", "rating": 1 }
  102. ]
  103. }
  104. ]
  105. }
  106. -----------------------------
  107. // NOTCONSOLE
  108. <1> the search requests id, used to group result details later
  109. <2> the query that is being evaluated
  110. <3> a list of document ratings, each entry containing the documents `_index` and
  111. `_id` together with the rating of the documents relevance with regards to this
  112. search request
  113. A document `rating` can be any integer value that expresses the relevance of the
  114. document on a user defined scale. For some of the metrics, just giving a binary
  115. rating (for example `0` for irrelevant and `1` for relevant) will be sufficient,
  116. other metrics can use a more fine grained scale.
  117. ===== Template based ranking evaluation
  118. As an alternative to having to provide a single query per test request, it is
  119. possible to specify query templates in the evaluation request and later refer to
  120. them. Queries with similar structure that only differ in their parameters don't
  121. have to be repeated all the time in the `requests` section this way. In typical
  122. search systems where user inputs usually get filled into a small set of query
  123. templates, this helps making the evaluation request more succinct.
  124. [source,js]
  125. --------------------------------
  126. GET /my_index/_rank_eval
  127. {
  128. [...]
  129. "templates": [
  130. {
  131. "id": "match_one_field_query", <1>
  132. "template": { <2>
  133. "inline": {
  134. "query": {
  135. "match": { "{{field}}": { "query": "{{query_string}}" }}
  136. }
  137. }
  138. }
  139. }
  140. ],
  141. "requests": [
  142. {
  143. "id": "amsterdam_query",
  144. "ratings": [ ... ],
  145. "template_id": "match_one_field_query", <3>
  146. "params": { <4>
  147. "query_string": "amsterdam",
  148. "field": "text"
  149. }
  150. },
  151. [...]
  152. }
  153. --------------------------------
  154. // NOTCONSOLE
  155. <1> the template id
  156. <2> the template definition to use
  157. <3> a reference to a previously defined template
  158. <4> the parameters to use to fill the template
  159. ===== Available evaluation metrics
  160. The `metric` section determines which of the available evaluation metrics is
  161. going to be used. The following metrics are supported:
  162. [float]
  163. [[k-precision]]
  164. ===== Precision at K (P@k)
  165. This metric measures the number of relevant results in the top k search results.
  166. Its a form of the well known
  167. https://en.wikipedia.org/wiki/Information_retrieval#Precision[Precision] metric
  168. that only looks at the top k documents. It is the fraction of relevant documents
  169. in those first k search. A precision at 10 (P@10) value of 0.6 then means six
  170. out of the 10 top hits are relevant with respect to the users information need.
  171. P@k works well as a simple evaluation metric that has the benefit of being easy
  172. to understand and explain. Documents in the collection need to be rated either
  173. as relevant or irrelevant with respect to the current query. P@k does not take
  174. into account where in the top k results the relevant documents occur, so a
  175. ranking of ten results that contains one relevant result in position 10 is
  176. equally good as a ranking of ten results that contains one relevant result in
  177. position 1.
  178. [source,console]
  179. --------------------------------
  180. GET /twitter/_rank_eval
  181. {
  182. "requests": [
  183. {
  184. "id": "JFK query",
  185. "request": { "query": { "match_all": {}}},
  186. "ratings": []
  187. }],
  188. "metric": {
  189. "precision": {
  190. "k" : 20,
  191. "relevant_rating_threshold": 1,
  192. "ignore_unlabeled": false
  193. }
  194. }
  195. }
  196. --------------------------------
  197. // TEST[setup:twitter]
  198. The `precision` metric takes the following optional parameters
  199. [cols="<,<",options="header",]
  200. |=======================================================================
  201. |Parameter |Description
  202. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  203. in the query. Defaults to 10.
  204. |`relevant_rating_threshold` |sets the rating threshold above which documents are considered to be
  205. "relevant". Defaults to `1`.
  206. |`ignore_unlabeled` |controls how unlabeled documents in the search results are counted.
  207. 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.
  208. |=======================================================================
  209. [float]
  210. ===== Mean reciprocal rank
  211. For every query in the test suite, this metric calculates the reciprocal of the
  212. rank of the first relevant document. For example finding the first relevant
  213. result in position 3 means the reciprocal rank is 1/3. The reciprocal rank for
  214. each query is averaged across all queries in the test suite to give the
  215. https://en.wikipedia.org/wiki/Mean_reciprocal_rank[mean reciprocal rank].
  216. [source,console]
  217. --------------------------------
  218. GET /twitter/_rank_eval
  219. {
  220. "requests": [
  221. {
  222. "id": "JFK query",
  223. "request": { "query": { "match_all": {}}},
  224. "ratings": []
  225. }],
  226. "metric": {
  227. "mean_reciprocal_rank": {
  228. "k" : 20,
  229. "relevant_rating_threshold" : 1
  230. }
  231. }
  232. }
  233. --------------------------------
  234. // TEST[setup:twitter]
  235. The `mean_reciprocal_rank` metric takes the following optional parameters
  236. [cols="<,<",options="header",]
  237. |=======================================================================
  238. |Parameter |Description
  239. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  240. in the query. Defaults to 10.
  241. |`relevant_rating_threshold` |Sets the rating threshold above which documents are considered to be
  242. "relevant". Defaults to `1`.
  243. |=======================================================================
  244. [float]
  245. ===== Discounted cumulative gain (DCG)
  246. In contrast to the two metrics above,
  247. https://en.wikipedia.org/wiki/Discounted_cumulative_gain[discounted cumulative gain]
  248. takes both, the rank and the rating of the search results, into account.
  249. The assumption is that highly relevant documents are more useful for the user
  250. when appearing at the top of the result list. Therefore, the DCG formula reduces
  251. the contribution that high ratings for documents on lower search ranks have on
  252. the overall DCG metric.
  253. [source,console]
  254. --------------------------------
  255. GET /twitter/_rank_eval
  256. {
  257. "requests": [
  258. {
  259. "id": "JFK query",
  260. "request": { "query": { "match_all": {}}},
  261. "ratings": []
  262. }],
  263. "metric": {
  264. "dcg": {
  265. "k" : 20,
  266. "normalize": false
  267. }
  268. }
  269. }
  270. --------------------------------
  271. // TEST[setup:twitter]
  272. The `dcg` metric takes the following optional parameters:
  273. [cols="<,<",options="header",]
  274. |=======================================================================
  275. |Parameter |Description
  276. |`k` |sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  277. in the query. Defaults to 10.
  278. |`normalize` | If set to `true`, this metric will calculate the https://en.wikipedia.org/wiki/Discounted_cumulative_gain#Normalized_DCG[Normalized DCG].
  279. |=======================================================================
  280. [float]
  281. ===== Expected Reciprocal Rank (ERR)
  282. Expected Reciprocal Rank (ERR) is an extension of the classical reciprocal rank
  283. for the graded relevance case (Olivier Chapelle, Donald Metzler, Ya Zhang, and
  284. Pierre Grinspan. 2009.
  285. http://olivier.chapelle.cc/pub/err.pdf[Expected reciprocal rank for graded relevance].)
  286. It is based on the assumption of a cascade model of search, in which a user
  287. scans through ranked search results in order and stops at the first document
  288. that satisfies the information need. For this reason, it is a good metric for
  289. question answering and navigation queries, but less so for survey oriented
  290. information needs where the user is interested in finding many relevant
  291. documents in the top k results.
  292. The metric models the expectation of the reciprocal of the position at which a
  293. user stops reading through the result list. This means that relevant document in
  294. top ranking positions will contribute much to the overall score. However, the
  295. same document will contribute much less to the score if it appears in a lower
  296. rank, even more so if there are some relevant (but maybe less relevant)
  297. documents preceding it. In this way, the ERR metric discounts documents which
  298. are shown after very relevant documents. This introduces a notion of dependency
  299. in the ordering of relevant documents that e.g. Precision or DCG don't account
  300. for.
  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. "expected_reciprocal_rank": {
  313. "maximum_relevance" : 3,
  314. "k" : 20
  315. }
  316. }
  317. }
  318. --------------------------------
  319. // TEST[setup:twitter]
  320. The `expected_reciprocal_rank` metric takes the following parameters:
  321. [cols="<,<",options="header",]
  322. |=======================================================================
  323. |Parameter |Description
  324. | `maximum_relevance` | Mandatory parameter. The highest relevance grade used in the user supplied
  325. relevance judgments.
  326. |`k` | sets the maximum number of documents retrieved per query. This value will act in place of the usual `size` parameter
  327. in the query. Defaults to 10.
  328. |=======================================================================
  329. ===== Response format
  330. The response of the `_rank_eval` endpoint contains the overall calculated result
  331. for the defined quality metric, a `details` section with a breakdown of results
  332. for each query in the test suite and an optional `failures` section that shows
  333. potential errors of individual queries. The response has the following format:
  334. [source,js]
  335. --------------------------------
  336. {
  337. "rank_eval": {
  338. "metric_score": 0.4, <1>
  339. "details": {
  340. "my_query_id1": { <2>
  341. "metric_score": 0.6, <3>
  342. "unrated_docs": [ <4>
  343. {
  344. "_index": "my_index",
  345. "_id": "1960795"
  346. }, [...]
  347. ],
  348. "hits": [
  349. {
  350. "hit": { <5>
  351. "_index": "my_index",
  352. "_type": "page",
  353. "_id": "1528558",
  354. "_score": 7.0556192
  355. },
  356. "rating": 1
  357. }, [...]
  358. ],
  359. "metric_details": { <6>
  360. "precision" : {
  361. "relevant_docs_retrieved": 6,
  362. "docs_retrieved": 10
  363. }
  364. }
  365. },
  366. "my_query_id2" : { [...] }
  367. },
  368. "failures": { [...] }
  369. }
  370. }
  371. --------------------------------
  372. // NOTCONSOLE
  373. <1> the overall evaluation quality calculated by the defined metric
  374. <2> the `details` section contains one entry for every query in the original `requests` section, keyed by the search request id
  375. <3> the `metric_score` in the `details` section shows the contribution of this query to the global quality metric score
  376. <4> the `unrated_docs` section contains an `_index` and `_id` entry for each document in the search result for this
  377. query that didn't have a ratings value. This can be used to ask the user to supply ratings for these documents
  378. <5> the `hits` section shows a grouping of the search results with their supplied rating
  379. <6> the `metric_details` give additional information about the calculated quality metric (e.g. how many of the retrieved
  380. documents where relevant). The content varies for each metric but allows for better interpretation of the results