Loading TOC...

MarkLogic 12 EA 1 Product Documentation
vec:vector-score

vec:vector-score(
   $score as xs:unsignedInt,
   $similarity as xs:double,
   [$similarityWeight as xs:double?]
) as xs:unsignedLong

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 ovec:cosine-similarity(). In the case that the vectors are normalized, pass ovec:dot-product(). Note that vec:euclidean-distance() 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

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';
import module namespace ofn = 'http://marklogic.com/optic/expression/fn'
           at 'MarkLogic/optic/optic-fn.xqy';

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

(: define a word query to find relevant documents :)
let $query := cts:word-query('turtle')

(: define a view named 'from_search' that contains our search results :)
let $search   := op:from-search($query,('fragmentId','score'),'from_search')

(: join a TDE view named 'wiki_vectors' with which to calculate a hybrid score :)
let $result := op:from-view('vecs','wiki_vectors',(),op:fragment-id-col('view_frag'))
             => op:join-inner($search,op:on(op:view-col('wiki_vectors','view_frag'),op:view-col('from_search','fragmentId')))
             => op:join-doc-uri(op:col('uri'),op:fragment-id-col('view_frag'))
             => op:bind(op:as('cosineSim',ovec:cosine-similarity(op:view-col('wiki_vectors','embedding'),$qv)))
             => op:bind(op:as('hybridScore',ovec:vector-score(op:col('score'), op:col('cosineSim'), 0.1)))
             => op:select((op:col('score'),op:col('cosineSim'),op:col('hybridScore'),op:col('uri')))
             => op:order-by((op:desc(op:col('hybridScore')),op:col('uri')))
             => op:result()
return $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.