从搜索结果中将精确匹配放在最前面(Apache Solr 5.5)

To Bring the exact match at very top from Search Results (Apache Solr 5.5)

我有一个字段 "description",我正在根据具有默认相似度评分的该字段搜索文本。比如说,我搜索了 "hello man",得到的结果是 "hello mango"、"hello man how you"、"hello man10"、"hello man"、"hello mann"。所有这些都是预期的结果,但我想在部分匹配的最顶部显示完全匹配(这里是 "hello man")。为了获得针对拼写错误的结果,我在索引和查询时使用 nGramFilterFactory,这是必需的。我唯一担心的是顶部没有完全匹配。

请建议我们如何做到这一点或我需要采用哪种方法。请帮忙。

您应该在 schema.xml 中定义另一个不对您的数据进行任何分析的字段。在您的情况下执行此操作的最简单方法可能是执行以下操作:

<field name="exact_description" type="string" indexed="true" stored="false" multiValued="true" />

<copyField source="originalColumnName" dest="exact_description" docValues="true" />

使用 string 类型将阻止 Solr 标记化或对您的数据执行任何其他操作。

然后,在构建查询时,您可以在查询的其余部分之前添加如下内容:

exact_description:"hello man"^100.0

确保你在 exact_description 上添加了你选择的提升(^100.0),这样精确匹配将被强制排在结果的顶部。

当您创建新字段时,请确保它基于一个尚未对其执行任何分析的字段。例如,在我的架构中,我有一个名为 exact_match 的字段,它是从以下内容复制的:

<field name="match" type="string" indexed="false" stored="true" required="false" omitNorms="true" />

现在,我可以在搜索中使用 match 进行精确匹配,因为 match 只是一个字符串,但出于规范原因,我不得不像这样创建 exact_match

<field name="exact_match" type="string" indexed="true" stored="false" multiValued="true" />

<copyField source="match" dest="exact_match" docValues="true" />