MarkLogic 12 EA 1 Product Documentation
vec:cosine-similarity

vec:cosine-similarity(
   $vector1 as vec:vector,
   $vector2 as vec:vector
) as xs:double

Summary

Returns the cosine similarity between two vectors. The vectors must be of the same dimension.

Parameters
vector1 The vector from which to calculate the cosine similarity with vector2.
vector2 The vector from which to calculate the cosine similarity with vector1.

Example

  xquery version "1.0-ml";
  let $vec1 := vec:vector((3.14,1.59,2.65))
  let $vec2 := vec:vector((3.58,9.79,3.23))

  return vec:cosine-similarity($vec1,$vec2);

  => 0.73559182882309

Example

  xquery version "1.0-ml";

  let $vec1 := vec:vector(fn:head(fn:doc('pronethalol.json'))/data/array-node{embedding})
  let $vec2 := vec:vector(fn:head(fn:doc('cell_renewal.json'))/data/array-node{embedding})

  return vec:cosine-similarity($vec1,$vec2)

  => The cosine similarity between vectors in JSON arrays named 'embedding'
      in documents 'pronethalol.json' and 'cell_renewal.json'

Example

xquery version "1.0-ml";
import module namespace op = 'http://marklogic.com/optic'
           at 'MarkLogic/optic.xqy';
import module namespace ovec = 'http://marklogic.com/optic/expression/vec'
           at 'MarkLogic/optic/optic-vec.xqy';

(: construct a query vector from the JSON array node 'emb' in document 'embedding104576.json' :)
let $qv := vec:vector(fn:head(fn:doc('embedding104576.json'))/array-node('emb'))

let $view := op:from-view('vecs','wiki_vectors')
           =>op:bind(op:as('cosineSim',ovec:cosine-similarity(op:col('embedding'),$qv)))
           =>op:select((op:col('title'),op:col('text'),op:col('cosineSim')))
           =>op:order-by(op:desc(op:col('cosineSim')))
           =>op:limit(30)
           =>op:result()
return $view;

=>
(:
  Performs a linear scan of vectors in the 'embedding' column in the 'wiki_vectors' view to find the top
   30 matches to the query vector $qv
:)
Powered by MarkLogic Server | Terms of Use | Privacy Policy