How to do simple boolean query in sunspot solr

2019-03-21 23:26发布

问题:

>>> marketing = User.search do |s|
>>>     s.fulltext "Marketing"
>>> end
>>> marketing.total
1448

>>> sales = User.search do |s|
>>>     s.fulltext "Sales"
>>> end
>>> sales.total
567

>>> marketing_and_sales = User.search do |s|
>>>     s.fulltext "Marketing AND Sales"
>>> end
>>> marketing_and_sales.total
945

>>> marketing_or_sales = User.search do |s|
>>>     s.fulltext "Marketing OR Sales"
>>> end
>>> marketing_or_sales.total
945  

<Sunspot::Search:{:fq=>["type:User"], :q=>"Marketing AND Sales", :fl=>"* score",      :qf=>"textfield1 textfield2 textfield3", :defType=>"dismax", :start=>0, :rows=>30}>

I want simple boolean queries to get working on sunspot-rails, solr I tried many possibilities its not simply taking it.

The AND and NOT seems to be working as per the dismax configuration. How can i make OR query working.

Thanks in advance.

回答1:

I figured it out. You can specify the scope of the search criterias using any_of and all_of . though all_of doesnt work unless used inside any_of . here is the link http://sunspot.github.com/docs/Sunspot/DSL/Scope.html#all_of-instance_method

>>> marketing_or_sales = User.search do |s|
>>>     s.any_of do 
>>>         s.fulltext "Marketing"
>>>         s.fulltext "Sales"
>>>     end
>>> end
>>> marketing_or_sales.total
945