vector-functions.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[vector-functions]]
  4. ===== Functions for vector fields
  5. experimental[]
  6. These functions are used for
  7. for <<dense-vector,`dense_vector`>> and
  8. <<sparse-vector,`sparse_vector`>> fields.
  9. NOTE: During vector functions' calculation, all matched documents are
  10. linearly scanned. Thus, expect the query time grow linearly
  11. with the number of matched documents. For this reason, we recommend
  12. to limit the number of matched documents with a `query` parameter.
  13. Let's create an index with the following mapping and index a couple
  14. of documents into it.
  15. [source,js]
  16. --------------------------------------------------
  17. PUT my_index
  18. {
  19. "mappings": {
  20. "properties": {
  21. "my_dense_vector": {
  22. "type": "dense_vector",
  23. "dims": 3
  24. },
  25. "my_sparse_vector" : {
  26. "type" : "sparse_vector"
  27. }
  28. }
  29. }
  30. }
  31. PUT my_index/_doc/1
  32. {
  33. "my_dense_vector": [0.5, 10, 6],
  34. "my_sparse_vector": {"2": 1.5, "15" : 2, "50": -1.1, "4545": 1.1}
  35. }
  36. PUT my_index/_doc/2
  37. {
  38. "my_dense_vector": [-0.5, 10, 10],
  39. "my_sparse_vector": {"2": 2.5, "10" : 1.3, "55": -2.3, "113": 1.6}
  40. }
  41. --------------------------------------------------
  42. // CONSOLE
  43. // TESTSETUP
  44. For dense_vector fields, `cosineSimilarity` calculates the measure of
  45. cosine similarity between a given query vector and document vectors.
  46. [source,js]
  47. --------------------------------------------------
  48. GET my_index/_search
  49. {
  50. "query": {
  51. "script_score": {
  52. "query": {
  53. "match_all": {}
  54. },
  55. "script": {
  56. "source": "cosineSimilarity(params.query_vector, doc['my_dense_vector']) + 1.0", <1>
  57. "params": {
  58. "query_vector": [4, 3.4, -0.2] <2>
  59. }
  60. }
  61. }
  62. }
  63. }
  64. --------------------------------------------------
  65. // CONSOLE
  66. <1> The script adds 1.0 to the cosine similarity to prevent the score from being negative.
  67. <2> To take advantage of the script optimizations, provide a query vector as a script parameter.
  68. NOTE: If a document's dense vector field has a number of dimensions
  69. different from the query's vector, an error will be thrown.
  70. Similarly, for sparse_vector fields, `cosineSimilaritySparse` calculates cosine similarity
  71. between a given query vector and document vectors.
  72. [source,js]
  73. --------------------------------------------------
  74. GET my_index/_search
  75. {
  76. "query": {
  77. "script_score": {
  78. "query": {
  79. "match_all": {}
  80. },
  81. "script": {
  82. "source": "cosineSimilaritySparse(params.query_vector, doc['my_sparse_vector']) + 1.0",
  83. "params": {
  84. "query_vector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  85. }
  86. }
  87. }
  88. }
  89. }
  90. --------------------------------------------------
  91. // CONSOLE
  92. For dense_vector fields, `dotProduct` calculates the measure of
  93. dot product between a given query vector and document vectors.
  94. [source,js]
  95. --------------------------------------------------
  96. GET my_index/_search
  97. {
  98. "query": {
  99. "script_score": {
  100. "query": {
  101. "match_all": {}
  102. },
  103. "script": {
  104. "source": """
  105. double value = dotProduct(params.query_vector, doc['my_dense_vector']);
  106. return sigmoid(1, Math.E, -value); <1>
  107. """,
  108. "params": {
  109. "query_vector": [4, 3.4, -0.2]
  110. }
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // CONSOLE
  117. <1> Using the standard sigmoid function prevents scores from being negative.
  118. Similarly, for sparse_vector fields, `dotProductSparse` calculates dot product
  119. between a given query vector and document vectors.
  120. [source,js]
  121. --------------------------------------------------
  122. GET my_index/_search
  123. {
  124. "query": {
  125. "script_score": {
  126. "query": {
  127. "match_all": {}
  128. },
  129. "script": {
  130. "source": """
  131. double value = dotProductSparse(params.query_vector, doc['my_sparse_vector']);
  132. return sigmoid(1, Math.E, -value);
  133. """,
  134. "params": {
  135. "query_vector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  136. }
  137. }
  138. }
  139. }
  140. }
  141. --------------------------------------------------
  142. // CONSOLE
  143. For dense_vector fields, `l1norm` calculates L^1^ distance
  144. (Manhattan distance) between a given query vector and
  145. document vectors.
  146. [source,js]
  147. --------------------------------------------------
  148. GET my_index/_search
  149. {
  150. "query": {
  151. "script_score": {
  152. "query": {
  153. "match_all": {}
  154. },
  155. "script": {
  156. "source": "1 / (1 + l1norm(params.queryVector, doc['my_dense_vector']))", <1>
  157. "params": {
  158. "queryVector": [4, 3.4, -0.2]
  159. }
  160. }
  161. }
  162. }
  163. }
  164. --------------------------------------------------
  165. // CONSOLE
  166. <1> Unlike `cosineSimilarity` that represent similarity, `l1norm` and
  167. `l2norm` shown below represent distances or differences. This means, that
  168. the more similar the vectors are, the lower the scores will be that are
  169. produced by the `l1norm` and `l2norm` functions.
  170. Thus, as we need more similar vectors to score higher,
  171. we reversed the output from `l1norm` and `l2norm`. Also, to avoid
  172. division by 0 when a document vector matches the query exactly,
  173. we added `1` in the denominator.
  174. For sparse_vector fields, `l1normSparse` calculates L^1^ distance
  175. between a given query vector and document vectors.
  176. [source,js]
  177. --------------------------------------------------
  178. GET my_index/_search
  179. {
  180. "query": {
  181. "script_score": {
  182. "query": {
  183. "match_all": {}
  184. },
  185. "script": {
  186. "source": "1 / (1 + l1normSparse(params.queryVector, doc['my_sparse_vector']))",
  187. "params": {
  188. "queryVector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  189. }
  190. }
  191. }
  192. }
  193. }
  194. --------------------------------------------------
  195. // CONSOLE
  196. For dense_vector fields, `l2norm` calculates L^2^ distance
  197. (Euclidean distance) between a given query vector and
  198. document vectors.
  199. [source,js]
  200. --------------------------------------------------
  201. GET my_index/_search
  202. {
  203. "query": {
  204. "script_score": {
  205. "query": {
  206. "match_all": {}
  207. },
  208. "script": {
  209. "source": "1 / (1 + l2norm(params.queryVector, doc['my_dense_vector']))",
  210. "params": {
  211. "queryVector": [4, 3.4, -0.2]
  212. }
  213. }
  214. }
  215. }
  216. }
  217. --------------------------------------------------
  218. // CONSOLE
  219. Similarly, for sparse_vector fields, `l2normSparse` calculates L^2^ distance
  220. between a given query vector and document vectors.
  221. [source,js]
  222. --------------------------------------------------
  223. GET my_index/_search
  224. {
  225. "query": {
  226. "script_score": {
  227. "query": {
  228. "match_all": {}
  229. },
  230. "script": {
  231. "source": "1 / (1 + l2normSparse(params.queryVector, doc['my_sparse_vector']))",
  232. "params": {
  233. "queryVector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  234. }
  235. }
  236. }
  237. }
  238. }
  239. --------------------------------------------------
  240. // CONSOLE
  241. NOTE: If a document doesn't have a value for a vector field on which
  242. a vector function is executed, an error will be thrown.
  243. You can check if a document has a value for the field `my_vector` by
  244. `doc['my_vector'].size() == 0`. Your overall script can look like this:
  245. [source,js]
  246. --------------------------------------------------
  247. "source": "doc['my_vector'].size() == 0 ? 0 : cosineSimilarity(params.queryVector, doc['my_vector'])"
  248. --------------------------------------------------
  249. // NOTCONSOLE