Class: Sunspot::Query::Dismax

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/query/dismax.rb

Instance Method Summary collapse

Constructor Details

#initialize(keywords) ⇒ Dismax

Returns a new instance of Dismax.



4
5
6
7
# File 'lib/sunspot/query/dismax.rb', line 4

def initialize(keywords)
  @keywords = keywords
  @fulltext_fields = {}
end

Instance Method Details

#add_fulltext_field(field, boost = nil) ⇒ Object

Add a fulltext field to be searched, with optional boost



43
44
45
# File 'lib/sunspot/query/dismax.rb', line 43

def add_fulltext_field(field, boost = nil)
  @fulltext_fields[field.indexed_name] = TextFieldBoost.new(field, boost)
end

#add_phrase_field(field, boost = nil) ⇒ Object

Add a phrase field for extra boost



50
51
52
53
# File 'lib/sunspot/query/dismax.rb', line 50

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.



36
37
38
# File 'lib/sunspot/query/dismax.rb', line 36

def create_boost_query(factor)
  @boost_query = BoostQuery.new(factor)
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.

Returns:

  • (Boolean)


68
69
70
# File 'lib/sunspot/query/dismax.rb', line 68

def has_fulltext_field?(field)
  @fulltext_fields.has_key?(field.indexed_name)
end

#set_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.



60
61
62
# File 'lib/sunspot/query/dismax.rb', line 60

def set_highlight(fields=[], options={})
  @highlight = Highlighting.new(fields, options)
end

#to_paramsObject

The query as Solr parameters



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sunspot/query/dismax.rb', line 12

def to_params
  params = { :q => @keywords }
  if @keywords.match(/\*\)?\Z/)
    params['q.alt'] = @keywords
    params.delete(:q)
  end
  params[:fl] = '* score'
  params[:qf] = @fulltext_fields.values.map { |field| field.to_boosted_field }.join(' ')
  params[:defType] = 'dismax'
  if @phrase_fields
    params[:pf] = @phrase_fields.map { |field| field.to_boosted_field }.join(' ')
  end
  if @boost_query
    params[:bq] = @boost_query.to_boolean_phrase
  end
  if @highlight
    Sunspot::Util.deep_merge!(params, @highlight.to_params)
  end
  params
end