Loading TOC...

MarkLogic 12 EA 1 Product Documentation
vec.cosineSimilarity

vec.cosineSimilarity(
   vector1 as vec.vector,
   vector2 as vec.vector
) as Number

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

  const vec1 = vec.vector([3.14,1.59,2.65])
  const vec2 = vec.vector([3.58,9.79,3.23])

  vec.cosineSimilarity(vec1,vec2)

  => 0.73559182882309

Example

  const vec1 = vec.vector(xdmp.toJSON(fn.doc('pronethalol.json')).xpath('/data/array-node{embedding}'))
  const vec2 = vec.vector(fn.head(fn.doc('cell_renewal.json')).xpath('/data/array-node{embedding}'))

  vec.cosineSimilarity(vec1,vec2)

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

Example

'use strict';

const op = require('/MarkLogic/optic');

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

const view = op.fromView('vecs','wiki_vectors')
               .bind(op.as('cosineSim',op.vec.cosineSimilarity(op.col('embedding'),qv)))
               .select([op.col('title'),op.col('text'),op.col('cosineSim')])
               .orderBy(op.desc(op.col('cosineSim')))
               .limit(30)
               .result()

view
  =>
  // Performs a linear scan of vectors in the 'embedding' column in the 'wiki_vectors' view to return the top
  //  30 matches to the query vector 'qv'

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.