Solr:将 OR 查询中的匹配数过滤到多值字段 [英] Solr: Filtering on the number of matches in an OR query to a multivalued field

查看:26
本文介绍了Solr:将 OR 查询中的匹配数过滤到多值字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下示例 solr 文档:

Given the following example solr documents:

<doc>
  <field name="guid">1</field>
  <field name="name">Harry Potter</field>
  <field name="friends">ron</field>
  <field name="friends">hermione</field>
  <field name="friends">ginny</field>
  <field name="friends">dumbledore</field>
</doc>
<doc>
  <field name="guid">2</field>
  <field name="name">Ron Weasley</field>
  <field name="friends">harry</field>
  <field name="friends">hermione</field>
  <field name="friends">lavender</field>
</doc>
<doc>
  <field name="guid">3</field>
  <field name="name">Hermione Granger</field>
  <field name="friends">harry</field>
  <field name="friends">ron</field>
  <field name="friends">ginny</field>
  <field name="friends">dumbledore</field>
</doc>

以及以下查询(或过滤查询):

and the following query (or filter query):

friends:ron OR friends:hermione OR friends:ginny OR friends:dumbledore 

所有三个文档都将被返回,因为它们每个都至少有一个指定的朋友.

all three documents will be returned since they each have at least one of the specified friends.

但是,我想为匹配的朋友数量设置一个最小(和最大)阈值.例如,只返回至少有 2 个但不超过 3 个指定朋友的文档.

However, I'd like to set a minimum (and maximum) threshold for how many friends are matched. For example, only return documents that have at least 2 but no more than 3 of the specified friends.

这样的查询只会返回第三个文档 (Hermione Granger),因为她指定了 4 个朋友中的 3 个,而第一个 (Harry Potter) 匹配所有 4 个,第二个 (Ron Weasley) 仅匹配 1 个.

Such a query would only return the third document (Hermione Granger) as she has 3 of the 4 friends specified, while the first (Harry Potter) matches all 4 and the second (Ron Weasley) matches only 1.

这在 Solr 查询中是否可行?

Is this possible in a Solr query?

推荐答案

您需要使用 函数查询termfreq,并计算匹配的术语(在您的情况下也称为朋友")的数量.您可以总结结果,然后只返回阈值内的文档,使用 frange,如下所示:

You'll want to use a function query, termfreq, and count the number of terms (aka "friends" in your case) matched. You can sum up the results, then only return documents within your threshold, using frange, like this:

{!frange l=2 u=3}sum(termfreq(friends,'ron'),termfreq(friends,'hermione'),termfreq(friends,'ginny'),termfreq(friends,'dumbledore'))

termfreq(...) 将为找到的每个朋友返回 1,并且这些总和是您针对阈值(您的下限和上限)进行测试的结果在 !frange 语句的开头指定).

termfreq(...) will return 1 for each friend found, and the sum of those is what you test against your threshold (the lower and upper bounds you specified in the beginning of your !frange statement).

您可以将其放在 q: 字段或 fq: 字段中.这是在 Solr 管理面板中供您参考:

You can place this in the q: field or fq: field. Here it is in the Solr admin panel for your reference:

这篇关于Solr:将 OR 查询中的匹配数过滤到多值字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆