rank-feature-query.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. [[query-dsl-rank-feature-query]]
  2. === Rank Feature Query
  3. The `rank_feature` query is a specialized query that only works on
  4. <<rank-feature,`rank_feature`>> fields and <<rank-features,`rank_features`>> fields.
  5. Its goal is to boost the score of documents based on the values of numeric
  6. features. It is typically put in a `should` clause of a
  7. <<query-dsl-bool-query,`bool`>> query so that its score is added to the score
  8. of the query.
  9. Compared to using <<query-dsl-function-score-query,`function_score`>> or other
  10. ways to modify the score, this query has the benefit of being able to
  11. efficiently skip non-competitive hits when
  12. <<search-uri-request,`track_total_hits`>> is not set to `true`. Speedups may be
  13. spectacular.
  14. Here is an example that indexes various features:
  15. - https://en.wikipedia.org/wiki/PageRank[`pagerank`], a measure of the
  16. importance of a website,
  17. - `url_length`, the length of the url, which typically correlates negatively
  18. with relevance,
  19. - `topics`, which associates a list of topics with every document alongside a
  20. measure of how well the document is connected to this topic.
  21. Then the example includes an example query that searches for `"2016"` and boosts
  22. based or `pagerank`, `url_length` and the `sports` topic.
  23. [source,js]
  24. --------------------------------------------------
  25. PUT test
  26. {
  27. "mappings": {
  28. "properties": {
  29. "pagerank": {
  30. "type": "rank_feature"
  31. },
  32. "url_length": {
  33. "type": "rank_feature",
  34. "positive_score_impact": false
  35. },
  36. "topics": {
  37. "type": "rank_features"
  38. }
  39. }
  40. }
  41. }
  42. PUT test/_doc/1
  43. {
  44. "url": "http://en.wikipedia.org/wiki/2016_Summer_Olympics",
  45. "content": "Rio 2016",
  46. "pagerank": 50.3,
  47. "url_length": 42,
  48. "topics": {
  49. "sports": 50,
  50. "brazil": 30
  51. }
  52. }
  53. PUT test/_doc/2
  54. {
  55. "url": "http://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
  56. "content": "Formula One motor race held on 13 November 2016 at the Autódromo José Carlos Pace in São Paulo, Brazil",
  57. "pagerank": 50.3,
  58. "url_length": 47,
  59. "topics": {
  60. "sports": 35,
  61. "formula one": 65,
  62. "brazil": 20
  63. }
  64. }
  65. PUT test/_doc/3
  66. {
  67. "url": "http://en.wikipedia.org/wiki/Deadpool_(film)",
  68. "content": "Deadpool is a 2016 American superhero film",
  69. "pagerank": 50.3,
  70. "url_length": 37,
  71. "topics": {
  72. "movies": 60,
  73. "super hero": 65
  74. }
  75. }
  76. POST test/_refresh
  77. GET test/_search
  78. {
  79. "query": {
  80. "bool": {
  81. "must": [
  82. {
  83. "match": {
  84. "content": "2016"
  85. }
  86. }
  87. ],
  88. "should": [
  89. {
  90. "rank_feature": {
  91. "field": "pagerank"
  92. }
  93. },
  94. {
  95. "rank_feature": {
  96. "field": "url_length",
  97. "boost": 0.1
  98. }
  99. },
  100. {
  101. "rank_feature": {
  102. "field": "topics.sports",
  103. "boost": 0.4
  104. }
  105. }
  106. ]
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. // CONSOLE
  112. [float]
  113. === Supported functions
  114. The `rank_feature` query supports 3 functions in order to boost scores using the
  115. values of rank features. If you do not know where to start, we recommend that you
  116. start with the `saturation` function, which is the default when no function is
  117. provided.
  118. [float]
  119. ==== Saturation
  120. This function gives a score that is equal to `S / (S + pivot)` where `S` is the
  121. value of the rank feature and `pivot` is a configurable pivot value so that the
  122. result will be less than +0.5+ if `S` is less than pivot and greater than +0.5+
  123. otherwise. Scores are always is +(0, 1)+.
  124. If the rank feature has a negative score impact then the function will be computed as
  125. `pivot / (S + pivot)`, which decreases when `S` increases.
  126. [source,js]
  127. --------------------------------------------------
  128. GET test/_search
  129. {
  130. "query": {
  131. "rank_feature": {
  132. "field": "pagerank",
  133. "saturation": {
  134. "pivot": 8
  135. }
  136. }
  137. }
  138. }
  139. --------------------------------------------------
  140. // CONSOLE
  141. // TEST[continued]
  142. If +pivot+ is not supplied then Elasticsearch will compute a default value that
  143. will be approximately equal to the geometric mean of all feature values that
  144. exist in the index. We recommend this if you haven't had the opportunity to
  145. train a good pivot value.
  146. [source,js]
  147. --------------------------------------------------
  148. GET test/_search
  149. {
  150. "query": {
  151. "rank_feature": {
  152. "field": "pagerank",
  153. "saturation": {}
  154. }
  155. }
  156. }
  157. --------------------------------------------------
  158. // CONSOLE
  159. // TEST[continued]
  160. [float]
  161. ==== Logarithm
  162. This function gives a score that is equal to `log(scaling_factor + S)` where
  163. `S` is the value of the rank feature and `scaling_factor` is a configurable scaling
  164. factor. Scores are unbounded.
  165. This function only supports rank features that have a positive score impact.
  166. [source,js]
  167. --------------------------------------------------
  168. GET test/_search
  169. {
  170. "query": {
  171. "rank_feature": {
  172. "field": "pagerank",
  173. "log": {
  174. "scaling_factor": 4
  175. }
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. // CONSOLE
  181. // TEST[continued]
  182. [float]
  183. ==== Sigmoid
  184. This function is an extension of `saturation` which adds a configurable
  185. exponent. Scores are computed as `S^exp^ / (S^exp^ + pivot^exp^)`. Like for the
  186. `saturation` function, `pivot` is the value of `S` that gives a score of +0.5+
  187. and scores are in +(0, 1)+.
  188. `exponent` must be positive, but is typically in +[0.5, 1]+. A good value should
  189. be computed via training. If you don't have the opportunity to do so, we recommend
  190. that you stick to the `saturation` function instead.
  191. [source,js]
  192. --------------------------------------------------
  193. GET test/_search
  194. {
  195. "query": {
  196. "rank_feature": {
  197. "field": "pagerank",
  198. "sigmoid": {
  199. "pivot": 7,
  200. "exponent": 0.6
  201. }
  202. }
  203. }
  204. }
  205. --------------------------------------------------
  206. // CONSOLE
  207. // TEST[continued]