feature-query.asciidoc 5.7 KB

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