Sunspot: how to make a full-text query for multiple fields with different values?

I would like to reproduce the following raw solr request with sunspot

q=exact_term_text:foo OR term_textv:foo* OR alternate_text:bar*

but I can’t understand if and how it is possible, through the standard sunspot interface, since it seems that:

  • The method fulltextdoes not seem to accept multiple text / search_fields arguments
  • I do not know which argument is passed as the first in fulltext, as if I were passing either "foo", or "bar", the results would not coincide with
  • If I pass an empty argument, I get a q=*:*
  • area filters (for example, with(:term).starting_with('foo*')are (as the name implies) used as filter queries, and therefore do not participate in scoring.

It seems possible to line up the string (or perhaps use it adjust_solr_params), but it seems hacky. Is there a better solution?

+5
source share
3 answers

Sunspot 2.0.0 has undocumented and unsupported behavior that really works. The author himself suggests that this should not be, and it probably will not be in future versions.

You can pass multiple full-text calls to a search definition

Post.search do
    fulltext "foo", {:fields => :exact_term}
    fulltext "bar", {:fields => :alternate}
end

This results in a solr request (from logs)

INFO: [] webapp=/solr path=/select 
params={fl=*+score&start=0&q=_query_:"{!dismax+qf%3D'exact_term'}foo"+_query_:"{!dismax+qf%3D'alternate'}bar"&wt=ruby&fq=type:Post&rows=30}
hits=1 status=0 QTime=7 

The relevant substrings are covered by https://github.com/sunspot/sunspot/wiki/Matching-substrings-in-fulltext-search

(AND/OR)   minimum_match 1 http://blog.tonycode.com/archives/192

+8

, fulltext. , .

a = Post.search do
    fulltext "foo", {:fields => :exact_term}
end

b = Post.search do
    fulltext "bar", {:fields => : alternate}
end

result = a & b

, , . . , .

+2

. , , . , , "foo", "bar" Post _ .

Post.search do
    fulltext "foo", {:fields => :exact_term}
    fulltext "bar", {:fields => :alternate}
end

The problem was that I could not change my schema.xml (defaultOperator) settings. It was set to "OR". If you have a similar problem, you can work around this like this:

Post.search do
    adjust_solr_params do |params|
        params[:q] = params[:q].gsub(/\s_query/, " AND _query") if params[:q].present?
    end
end
+1
source

All Articles