Loading TOC...

MarkLogic 12 EA 1 Product Documentation
vec.vectorScore

vec.vectorScore(
   score as Number,
   similarity as Number,
   [similarityWeight as Number?]
) as (Number|String)

Summary

A helper function that returns a hybrid score using a cts score and a vector similarity calculation result. You can tune the effect of the vector similarity on the score using the similarityWeight option. The ideal value for similarityWeight depends on your application.

Parameters
score The cts.score of the matching document.
similarity The similarity between the vector in the matching document and the query vector. The result of a call to op.vec.cosineSimilarity(). In the case that the vectors are normalized, pass op.vec.dotProduct(). Note that vec.euclideanDistance() should not be used here.
similarityWeight The weight of the vector similarity on the score. The default value is 0.1. If 0.0 is passed in, vector similarity has no effect. If passed a value less than 0.0 or greater than 1.0, throw VEC-VECTORSCORE.

See Also

Example

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

// grab a query vector from the document below at the array node named 'emb'
const qv = vec.vector(fn.head(fn.doc('embedding104206.json')).xpath("/array-node('emb')"))

// define a word query to find relevant documents
const query = cts.wordQuery('turtle')

// define a view named 'from_search' that contains our search results
const search = op.fromSearch(query,['fragmentId','score'],'from_search')

// join a TDE view named 'wiki_vectors' with which to calculate a hybrid score :)
const result = op.fromView('vecs','wiki_vectors',null,op.fragmentIdCol('view_frag'))
                 .joinInner(search,op.on(op.viewCol('wiki_vectors','view_frag'),op.viewCol('from_search','fragmentId')))
                 .joinDocUri(op.col('uri'),op.fragmentIdCol('view_frag'))
                 .bind(op.as('cosineSim',op.vec.cosineSimilarity(op.viewCol('wiki_vectors','embedding'),qv)))
                 .bind(op.as('hybridScore',op.vec.vectorScore(op.col('score'),op.col('cosineSim'),0.1)))
                 .select([op.col('score'),op.col('cosineSim'),op.col('hybridScore'),op.col('uri')])
                 .orderBy([op.desc(op.col('hybridScore')),op.col('uri')])
                 .result()
result;

=> A table with the result of scoring the document, combining the cts score with vector similarity

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