我想检查查询和文档标题之间的 Levenstein 距离,然后按其 strdist 分数过滤结果。
架构是:
<fields>
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="title" type="text_general" indexed="true" stored="true" required="true" />
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false" />
</fields>
在我的索引中,我有以下文档:
{
"id":"1",
"title":"iPhone 4S Battery Replacement"
}
所以当我发送以下查询时:
http://localhost:8983/solr/collection1/query?q=title:iPhone+4S+Battery+Replacement&fl=*,score,lev_dist:strdist("iPhone+4S+Battery+Replacement",title,edit)
我得到:
{
"id":"1",
"title":"iPhone 4S Battery Replacement",
"_version_":1452659974334316549,
"score":6.4907703,
"lev_dist":0.37931037
}
但我期待得到 lev_dist=1.0。为什么是 0.379?
我究竟做错了什么?
最佳答案
根据 to the docs strdist 函数需要两个字符串来比较它们。它在分析的字段上的工作方式有所不同。
Calculate the distance between two strings. Uses the Lucene spell checker StringDistance interface and supports all of the implementations available in that package, plus allows applications to plug in their own via Solr's resource loading capabilities. strdist takes (string1, string2, distance measure)
经过尝试和reading of a grokbase user谁有类似的问题,你需要添加一个字段,如title_raw
在您的架构中,请参见下文并重新编制索引。
<fields> <field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="title" type="text_general" indexed="true" stored="true" required="true" /> <field name="title_raw" type="string" indexed="true" stored="true" /> <field name="_version_" type="long" indexed="true" stored="true" multiValued="false" /> </fields>
然后你会查询query?q=title:iPhone+4S+Battery+Replacement&fl=*,score,lev_dist:strdist("iPhone 4S Battery Replacement",title_raw,edit)
如您所见,我删除了+
从要比较的第一个字符串开始,因为在计算距离时也会将它们考虑在内。
关于solr strdist 不返回 1.0 的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218538/