vec:vector-score( $score as xs:unsignedInt, $distance as xs:double, [$distanceWeight as xs:double?], [$weight as xs:double?] ) as xs:unsignedLong
A helper function that returns a hybrid score using a cts score and a vector distance calculation result.
You can tune the effect of the vector distance on the score using the distanceWeight option. The ideal
value for distanceWeight depends on your application.
The hybrid score is calculated using the formula:
score = weight * annScore + (1 - weight) * ctsScore
.
- annScore
is derived from the distance and distanceWeight, where a larger distanceWeight reduces the annScore for the same distance.
- weight
determines the contribution of the annScore and ctsScore to the final score. A weight of 0.5 balances both equally.
This formula allows you to combine traditional cts scoring with vector-based distance scoring, providing a flexible way to rank results.
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('cosDistance',ovec:cosine-distance(op:view-col('wiki_vectors','embedding'),$qv))) => op:bind(op:as('hybridScore',ovec:vector-score(op:col('score'), op:col('cosDistance'), 0.5, 1.0))) => op:select((op:col('score'),op:col('cosDistance'),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 distance
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.