vec:vector-score( $score as xs:unsignedInt, $similarity as xs:double, [$similarityWeight as xs:double?] ) as xs:unsignedLong
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.
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