Class: Sunspot::Query::Dismax
- Inherits:
-
Object
- Object
- Sunspot::Query::Dismax
- Defined in:
- lib/sunspot/query/dismax.rb
Overview
Solr full-text queries use Solr’s DisMaxRequestHandler, a search handler designed to process user-entered phrases, and search for individual words across a union of several fields.
Instance Attribute Summary collapse
-
#minimum_match ⇒ Object
writeonly
Sets the attribute minimum_match.
-
#phrase_slop ⇒ Object
writeonly
Sets the attribute phrase_slop.
-
#query_phrase_slop ⇒ Object
writeonly
Sets the attribute query_phrase_slop.
-
#tie ⇒ Object
writeonly
Sets the attribute tie.
Instance Method Summary collapse
-
#add_boost_function(function_query) ⇒ Object
Add a boost function.
-
#add_fulltext_field(field, boost = nil) ⇒ Object
Add a fulltext field to be searched, with optional boost.
-
#add_highlight(fields = [], options = {}) ⇒ Object
Set highlighting options for the query.
-
#add_phrase_field(field, boost = nil) ⇒ Object
Add a phrase field for extra boost.
-
#create_boost_query(factor) ⇒ Object
Assign a new boost query and return it.
-
#has_fulltext_field?(field) ⇒ Boolean
Determine if a given field is being searched.
-
#initialize(keywords) ⇒ Dismax
constructor
A new instance of Dismax.
-
#to_params ⇒ Object
The query as Solr parameters.
-
#to_subquery ⇒ Object
Serialize the query as a Solr nested subquery.
Constructor Details
#initialize(keywords) ⇒ Dismax
Returns a new instance of Dismax.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sunspot/query/dismax.rb', line 12 def initialize(keywords) @keywords = keywords @fulltext_fields = {} @boost_queries = [] @boost_functions = [] @highlights = [] @minimum_match = nil @phrase_fields = nil @phrase_slop = nil @query_phrase_slop = nil @tie = nil end |
Instance Attribute Details
#minimum_match=(value) ⇒ Object (writeonly)
Sets the attribute minimum_match
10 11 12 |
# File 'lib/sunspot/query/dismax.rb', line 10 def minimum_match=(value) @minimum_match = value end |
#phrase_slop=(value) ⇒ Object (writeonly)
Sets the attribute phrase_slop
10 11 12 |
# File 'lib/sunspot/query/dismax.rb', line 10 def phrase_slop=(value) @phrase_slop = value end |
#query_phrase_slop=(value) ⇒ Object (writeonly)
Sets the attribute query_phrase_slop
10 11 12 |
# File 'lib/sunspot/query/dismax.rb', line 10 def query_phrase_slop=(value) @query_phrase_slop = value end |
#tie=(value) ⇒ Object (writeonly)
Sets the attribute tie
10 11 12 |
# File 'lib/sunspot/query/dismax.rb', line 10 def tie=(value) @tie = value end |
Instance Method Details
#add_boost_function(function_query) ⇒ Object
Add a boost function
88 89 90 |
# File 'lib/sunspot/query/dismax.rb', line 88 def add_boost_function(function_query) @boost_functions << function_query end |
#add_fulltext_field(field, boost = nil) ⇒ Object
Add a fulltext field to be searched, with optional boost.
95 96 97 |
# File 'lib/sunspot/query/dismax.rb', line 95 def add_fulltext_field(field, boost = nil) @fulltext_fields[field.indexed_name] = TextFieldBoost.new(field, boost) end |
#add_highlight(fields = [], options = {}) ⇒ Object
Set highlighting options for the query. If fields is empty, the Highlighting object won’t pass field names at all, which means the dismax’s :qf parameter will be used by Solr.
112 113 114 |
# File 'lib/sunspot/query/dismax.rb', line 112 def add_highlight(fields=[], ={}) @highlights << Highlighting.new(fields, ) end |
#add_phrase_field(field, boost = nil) ⇒ Object
Add a phrase field for extra boost.
102 103 104 105 |
# File 'lib/sunspot/query/dismax.rb', line 102 def add_phrase_field(field, boost = nil) @phrase_fields ||= [] @phrase_fields << TextFieldBoost.new(field, boost) end |
#create_boost_query(factor) ⇒ Object
Assign a new boost query and return it.
80 81 82 83 |
# File 'lib/sunspot/query/dismax.rb', line 80 def create_boost_query(factor) @boost_queries << boost_query = BoostQuery.new(factor) boost_query end |
#has_fulltext_field?(field) ⇒ Boolean
Determine if a given field is being searched. Used by DSL to avoid overwriting boost parameters when injecting defaults.
120 121 122 |
# File 'lib/sunspot/query/dismax.rb', line 120 def has_fulltext_field?(field) @fulltext_fields.has_key?(field.indexed_name) end |
#to_params ⇒ Object
The query as Solr parameters
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sunspot/query/dismax.rb', line 29 def to_params params = { :q => @keywords } params[:fl] = '* score' params[:qf] = @fulltext_fields.values.map { |field| field.to_boosted_field }.join(' ') params[:defType] = 'edismax' if @phrase_fields params[:pf] = @phrase_fields.map { |field| field.to_boosted_field }.join(' ') end unless @boost_queries.empty? params[:bq] = @boost_queries.map do |boost_query| boost_query.to_boolean_phrase end end unless @boost_functions.empty? params[:bf] = @boost_functions.map do |boost_function| boost_function.to_s end end if @minimum_match params[:mm] = @minimum_match end if @phrase_slop params[:ps] = @phrase_slop end if @query_phrase_slop params[:qs] = @query_phrase_slop end if @tie params[:tie] = @tie end @highlights.each do |highlight| Sunspot::Util.deep_merge!(params, highlight.to_params) end params end |
#to_subquery ⇒ Object
Serialize the query as a Solr nested subquery.
68 69 70 71 72 73 74 75 |
# File 'lib/sunspot/query/dismax.rb', line 68 def to_subquery params = self.to_params params.delete :defType params.delete :fl keywords = params.delete(:q) = params.map { |key, value| escape_param(key, value) }.join(' ') "_query_:\"{!edismax #{}}#{escape_quotes(keywords)}\"" end |