Sarvesh
Sarvesh

Reputation: 1202

Sunspot Solr handling a boolean logic

Can sunspot handle a logic having AND and OR operators?

And Sunspot is using the dismax request handler where minimum_match field is used to implement OR queries/ AND queries

Now the problem is when we need to combine AND and OR operators together. How to implement the same.

text = "Hot Air Balloon"

search = Video.solr_search, :minimum_match => text.size do
  fulltext text do
    fields :title
  end
end

I need something like HOT AND AIR OR BALLOON

Upvotes: 1

Views: 639

Answers (3)

konyak
konyak

Reputation: 11706

As of version 2.2.0, sunspot_solr defaults to edismax parser, which supports queries using AND, OR, NOT, -, and +.

text = "HOT AND AIR OR BALLOON"

search = Video.search do
  fulltext text
end

Upvotes: 0

wawka
wawka

Reputation: 5144

Unfortunately, any_of and all_of only works for scalar fields (strings, integers), not for fulltext search (see documentation, it says about these two methods in context of scalar fields). If you want to achieve such effect on one field, you have to create your query manually:

fulltext "Hot AND Air OR Balloon" do
    fields :title
end

It gives you a query to Solr:

 q=Hot AND Air OR Balloon&qf=title&rows=1&start=0  

I didn't test it but this should also work:

"+hot +air ballon"

'+' sign means that word is mandatory. 'Balloon' can be left without any operator as the default for dismax is 'OR' operator (you can check your settings for default operator in schema.xml, solrQueryParser node).

Upvotes: 0

Ruby Racer
Ruby Racer

Reputation: 5740

If you have a limited number of possible text combination, you can force OR with any_of:

text1=["hot","air"]
text2=["balloon"]

search = Video.search do
    any_of do
        fulltext text1, fields: :title
        fulltext text2, fields: :title
    end
end

Upvotes: 1

Related Questions