vector-functions.asciidoc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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,console]
  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. "status" : {
  29. "type" : "keyword"
  30. }
  31. }
  32. }
  33. }
  34. PUT my_index/_doc/1
  35. {
  36. "my_dense_vector": [0.5, 10, 6],
  37. "my_sparse_vector": {"2": 1.5, "15" : 2, "50": -1.1, "4545": 1.1},
  38. "status" : "published"
  39. }
  40. PUT my_index/_doc/2
  41. {
  42. "my_dense_vector": [-0.5, 10, 10],
  43. "my_sparse_vector": {"2": 2.5, "10" : 1.3, "55": -2.3, "113": 1.6},
  44. "status" : "published"
  45. }
  46. --------------------------------------------------
  47. // TESTSETUP
  48. For dense_vector fields, `cosineSimilarity` calculates the measure of
  49. cosine similarity between a given query vector and document vectors.
  50. [source,console]
  51. --------------------------------------------------
  52. GET my_index/_search
  53. {
  54. "query": {
  55. "script_score": {
  56. "query" : {
  57. "bool" : {
  58. "filter" : {
  59. "term" : {
  60. "status" : "published" <1>
  61. }
  62. }
  63. }
  64. },
  65. "script": {
  66. "source": "cosineSimilarity(params.query_vector, doc['my_dense_vector']) + 1.0", <2>
  67. "params": {
  68. "query_vector": [4, 3.4, -0.2] <3>
  69. }
  70. }
  71. }
  72. }
  73. }
  74. --------------------------------------------------
  75. <1> To restrict the number of documents on which script score calculation is applied, provide a filter.
  76. <2> The script adds 1.0 to the cosine similarity to prevent the score from being negative.
  77. <3> To take advantage of the script optimizations, provide a query vector as a script parameter.
  78. NOTE: If a document's dense vector field has a number of dimensions
  79. different from the query's vector, an error will be thrown.
  80. Similarly, for sparse_vector fields, `cosineSimilaritySparse` calculates cosine similarity
  81. between a given query vector and document vectors.
  82. [source,console]
  83. --------------------------------------------------
  84. GET my_index/_search
  85. {
  86. "query": {
  87. "script_score": {
  88. "query" : {
  89. "bool" : {
  90. "filter" : {
  91. "term" : {
  92. "status" : "published"
  93. }
  94. }
  95. }
  96. },
  97. "script": {
  98. "source": "cosineSimilaritySparse(params.query_vector, doc['my_sparse_vector']) + 1.0",
  99. "params": {
  100. "query_vector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  101. }
  102. }
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. For dense_vector fields, `dotProduct` calculates the measure of
  108. dot product between a given query vector and document vectors.
  109. [source,console]
  110. --------------------------------------------------
  111. GET my_index/_search
  112. {
  113. "query": {
  114. "script_score": {
  115. "query" : {
  116. "bool" : {
  117. "filter" : {
  118. "term" : {
  119. "status" : "published"
  120. }
  121. }
  122. }
  123. },
  124. "script": {
  125. "source": """
  126. double value = dotProduct(params.query_vector, doc['my_dense_vector']);
  127. return sigmoid(1, Math.E, -value); <1>
  128. """,
  129. "params": {
  130. "query_vector": [4, 3.4, -0.2]
  131. }
  132. }
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. <1> Using the standard sigmoid function prevents scores from being negative.
  138. Similarly, for sparse_vector fields, `dotProductSparse` calculates dot product
  139. between a given query vector and document vectors.
  140. [source,console]
  141. --------------------------------------------------
  142. GET my_index/_search
  143. {
  144. "query": {
  145. "script_score": {
  146. "query" : {
  147. "bool" : {
  148. "filter" : {
  149. "term" : {
  150. "status" : "published"
  151. }
  152. }
  153. }
  154. },
  155. "script": {
  156. "source": """
  157. double value = dotProductSparse(params.query_vector, doc['my_sparse_vector']);
  158. return sigmoid(1, Math.E, -value);
  159. """,
  160. "params": {
  161. "query_vector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  162. }
  163. }
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. For dense_vector fields, `l1norm` calculates L^1^ distance
  169. (Manhattan distance) between a given query vector and
  170. document vectors.
  171. [source,console]
  172. --------------------------------------------------
  173. GET my_index/_search
  174. {
  175. "query": {
  176. "script_score": {
  177. "query" : {
  178. "bool" : {
  179. "filter" : {
  180. "term" : {
  181. "status" : "published"
  182. }
  183. }
  184. }
  185. },
  186. "script": {
  187. "source": "1 / (1 + l1norm(params.queryVector, doc['my_dense_vector']))", <1>
  188. "params": {
  189. "queryVector": [4, 3.4, -0.2]
  190. }
  191. }
  192. }
  193. }
  194. }
  195. --------------------------------------------------
  196. <1> Unlike `cosineSimilarity` that represent similarity, `l1norm` and
  197. `l2norm` shown below represent distances or differences. This means, that
  198. the more similar the vectors are, the lower the scores will be that are
  199. produced by the `l1norm` and `l2norm` functions.
  200. Thus, as we need more similar vectors to score higher,
  201. we reversed the output from `l1norm` and `l2norm`. Also, to avoid
  202. division by 0 when a document vector matches the query exactly,
  203. we added `1` in the denominator.
  204. For sparse_vector fields, `l1normSparse` calculates L^1^ distance
  205. between a given query vector and document vectors.
  206. [source,console]
  207. --------------------------------------------------
  208. GET my_index/_search
  209. {
  210. "query": {
  211. "script_score": {
  212. "query" : {
  213. "bool" : {
  214. "filter" : {
  215. "term" : {
  216. "status" : "published"
  217. }
  218. }
  219. }
  220. },
  221. "script": {
  222. "source": "1 / (1 + l1normSparse(params.queryVector, doc['my_sparse_vector']))",
  223. "params": {
  224. "queryVector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  225. }
  226. }
  227. }
  228. }
  229. }
  230. --------------------------------------------------
  231. For dense_vector fields, `l2norm` calculates L^2^ distance
  232. (Euclidean distance) between a given query vector and
  233. document vectors.
  234. [source,console]
  235. --------------------------------------------------
  236. GET my_index/_search
  237. {
  238. "query": {
  239. "script_score": {
  240. "query" : {
  241. "bool" : {
  242. "filter" : {
  243. "term" : {
  244. "status" : "published"
  245. }
  246. }
  247. }
  248. },
  249. "script": {
  250. "source": "1 / (1 + l2norm(params.queryVector, doc['my_dense_vector']))",
  251. "params": {
  252. "queryVector": [4, 3.4, -0.2]
  253. }
  254. }
  255. }
  256. }
  257. }
  258. --------------------------------------------------
  259. Similarly, for sparse_vector fields, `l2normSparse` calculates L^2^ distance
  260. between a given query vector and document vectors.
  261. [source,console]
  262. --------------------------------------------------
  263. GET my_index/_search
  264. {
  265. "query": {
  266. "script_score": {
  267. "query" : {
  268. "bool" : {
  269. "filter" : {
  270. "term" : {
  271. "status" : "published"
  272. }
  273. }
  274. }
  275. },
  276. "script": {
  277. "source": "1 / (1 + l2normSparse(params.queryVector, doc['my_sparse_vector']))",
  278. "params": {
  279. "queryVector": {"2": 0.5, "10" : 111.3, "50": -1.3, "113": 14.8, "4545": 156.0}
  280. }
  281. }
  282. }
  283. }
  284. }
  285. --------------------------------------------------
  286. NOTE: If a document doesn't have a value for a vector field on which
  287. a vector function is executed, an error will be thrown.
  288. You can check if a document has a value for the field `my_vector` by
  289. `doc['my_vector'].size() == 0`. Your overall script can look like this:
  290. [source,js]
  291. --------------------------------------------------
  292. "source": "doc['my_vector'].size() == 0 ? 0 : cosineSimilarity(params.queryVector, doc['my_vector'])"
  293. --------------------------------------------------
  294. // NOTCONSOLE