TiSer TiSer - 1 year ago 104
Ruby Question

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

Inside my model at

block I have index
time added_at

block for searching I added
with(:added_at, nil)
, made reindex and now inside
object I have:

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

What is the meaning of this [* TO *] ? Something went wrong?

Answer Source

By adding with(:added_at, nil) you narrow down the search results to documents having no values in the field added_at, so we can expect the corresponding query filter to be defined as :

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

The problem is that Solr Standard Query Parser does not support searching a field for empty/null value. In this situation the filter needs to be negated (exluding documents having any value in the field) so that the query remains valid.

The operator - can be used to exclude the field, and the wildcard character * can be used to match any value, now we can expect the query filter to look like :


However, although the above is valid for the query parser, using a range query should be preferred to prevent inconsitent behaviors when using wildcard within negative subqueries.

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.

Eventually there is nothing wrong with this filter that ends up looking like :

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

cf. Lucene Range Queries, Solr Standard Query Parser

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download