如何在DenseVectorField上搜索Solr 9时获得距离分数

2sbarzqh  于 12个月前  发布在  Solr
关注(0)|答案(1)|浏览(171)

我为一些诗歌和童谣创建了一个solr索引(版本9.3.0)。我试图搜索相关的诗歌和童谣,并希望获取每个匹配文档的点积距离。我找不到任何方法来获取该信息。以下是我在managed-schema文件中添加到solr的字段:

<fieldType name="knn_vector" class="solr.DenseVectorField" vectorDimension="384"
  similarityFunction="dot_product"  knnAlgorithm="hnsw" 
  hnswMaxConnections="16" hnswBeamWidth="50"/>
<field name="bge_small_vector" type="knn_vector" indexed="true" stored="true"/>

字符串
下面是我用来查询solr索引的python代码:

import pysolr
from encoder import Encoder
from sentence_transformers import SentenceTransformer
import pprint

pp = pprint.PrettyPrinter(indent=4, width=100)

solr = pysolr.Solr('http://localhost:8983/solr/docindex')
model = SentenceTransformer('BAAI/bge-small-en-v1.5')
document = '''Three blind mice. Three blind mice.
See how they run. See how they run.
They all ran after the farmer's wife,
Who cut off their tails with a carving knife.
Did you ever see such a sight in your life
As three blind mice?'''
embedding = model.encode(document, normalize_embeddings=True, convert_to_numpy=True)

solr_response=solr.search(
                    q=r'{!knn f=bge_small_vector topK=10}[' + ",".join([f'{a:.12f}' for a in embedding]) + ']',
                    rows=10,
                    start=0,
                    debugQuery="true",
                    wt='json')

for item in solr_response:
   pp.pprint(item)

pp.pprint(solr_response.debug)


我能找到的关于距离的唯一参考是在调试响应中,它并不特定于任何文档:

{   'QParser': 'KnnQParser',
    'explain': {'': '\n**0.81944466 = within top 10**\n'},
    'parsedquery': 'KnnVectorQuery(KnnVectorQuery:bge_small_vector[-0.02721269,...][10])',
    'parsedquery_toString': 'KnnVectorQuery:bge_small_vector[-0.02721269,...][10]',
    ...
}


有谁知道如何让solr在DenseVectorField查询中返回每个文档的距离?

9bfwbjaz

9bfwbjaz1#

https://opensearch.org/docs/latest/search-plugins/knn/approximate-knn/的论文中,它展示了在OpenSearch中将距离转换为分数的方法。我刚刚测试了L2距离分数= 1 /(1 + distance),因此距离=(1 / score)- 1。对于欧几里得距离,您可能需要取结果的平方根。

相关问题