vec.vectorScore( score as Number, similarity as Number, [similarityWeight as Number?] ) as (Number|String)
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.
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: Get the most useful answers to questions from the MarkLogic community, or ask your own question.