How to do simple boolean query in sunspot solr

青春壹個敷衍的年華 提交于 2019-12-04 15:22:49

问题


>>> 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  


来源:https://stackoverflow.com/questions/12005661/how-to-do-simple-boolean-query-in-sunspot-solr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!