Class: Sunspot::Query::Dismax

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keywords) ⇒ Dismax

Returns a new instance of Dismax.



6
7
8
9
10
11
# File 'lib/sunspot/query/dismax.rb', line 6

def initialize(keywords)
  @keywords = keywords
  @fulltext_fields = {}
  @boost_queries = []
  @highlights = []
end

Instance Attribute Details

#minimum_match=(value) ⇒ Object (writeonly)

Sets the attribute minimum_match

Parameters:

  • value

    the value to set the attribute minimum_match to.



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

def minimum_match=(value)
  @minimum_match = value
end

#phrase_slop=(value) ⇒ Object (writeonly)

Sets the attribute phrase_slop

Parameters:

  • value

    the value to set the attribute phrase_slop to.



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

def phrase_slop=(value)
  @phrase_slop = value
end

#query_phrase_slop=(value) ⇒ Object (writeonly)

Sets the attribute query_phrase_slop

Parameters:

  • value

    the value to set the attribute query_phrase_slop to.



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

def query_phrase_slop=(value)
  @query_phrase_slop = value
end

#tie=(value) ⇒ Object (writeonly)

Sets the attribute tie

Parameters:

  • value

    the value to set the attribute tie to.



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

def tie=(value)
  @tie = value
end

Instance Method Details

#add_fulltext_field(field, boost = nil) ⇒ Object

Add a fulltext field to be searched, with optional boost



58
59
60
# File 'lib/sunspot/query/dismax.rb', line 58

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.



75
76
77
# File 'lib/sunspot/query/dismax.rb', line 75

def add_highlight(fields=[], options={})
  @highlights << Highlighting.new(fields, options)
end

#add_phrase_field(field, boost = nil) ⇒ Object

Add a phrase field for extra boost



65
66
67
68
# File 'lib/sunspot/query/dismax.rb', line 65

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.



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

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.

Returns:

  • (Boolean)


83
84
85
# File 'lib/sunspot/query/dismax.rb', line 83

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

#to_paramsObject

The query as Solr parameters



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sunspot/query/dismax.rb', line 16

def to_params
  params = { :q => @keywords }
  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
  unless @boost_queries.empty?
    params[:bq] = @boost_queries.map do |boost_query|
      boost_query.to_boolean_phrase
    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