Solr 和 Rails:[* TO *] 值而不是 nil(星号到星号)

Solr and Rails: [* TO *] value instead of nil (asterisk TO asterisk)

在我的模型中 searchable 块我有索引 time added_at.

在用于搜索的 search 块中,我添加了 with(:added_at, nil),进行了重建索引,现在在 search 对象中,我有:

<Sunspot::Search:{:fq=>["-added_at_d:[* TO *]"]...}>

这个 [* TO *] 是什么意思?出了什么问题?

通过添加 with(:added_at, nil) 可以将搜索结果缩小到字段 added_at 中没有值的文档,因此我们可以期待相应的查询过滤器定义为:

fq=>["added_at_d:null"] # not valid

问题是 Solr 标准查询解析器不支持在字段中搜索 empty/null 值。在这种情况下,需要取消过滤器(排除字段中具有任何值的文档),以便查询保持有效。

运算符-可以用来排除字段,通配符*可以用来匹配任何值,现在我们可以期望查询过滤器看起来像:

fq=>["-added_at_d:*"] 

但是,尽管上述内容对查询解析器有效,但在否定子查询中使用通配符时,应首选使用范围查询以防止不一致的行为。

Range Queries allow one to match documents whose field(s) values are between the lower and upper bound specified by the Range Query. Range Queries can be inclusive or exclusive of the upper and lower bounds.

A * may be used for either or both endpoints to specify an open-ended range query.

最终这个过滤器没有任何问题,最终看起来像:

fq=>["-added_at_d:[* TO *]"]

比照。 Lucene Range Queries, Solr Standard Query Parser