Class: Sunspot::Query::Dismax

Inherits:
AbstractFulltext show all
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

Attributes inherited from AbstractFulltext

#fulltext_fields

Instance Method Summary collapse

Methods inherited from AbstractFulltext

#add_additive_boost_function, #add_boost_query, #add_highlight, #add_multiplicative_boost_function, #add_phrase_field, #has_fulltext_field?

Constructor Details

#initialize(keywords) ⇒ Dismax

Returns a new instance of Dismax.



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

def initialize(keywords)
  @keywords = keywords
  @fulltext_fields = {}
  @boost_queries = []
  @additive_boost_functions = []
  @multiplicative_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

Parameters:

  • value

    the value to set the attribute minimum_match to.



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

Parameters:

  • value

    the value to set the attribute phrase_slop to.



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

Parameters:

  • value

    the value to set the attribute query_phrase_slop to.



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

Parameters:

  • value

    the value to set the attribute tie to.



10
11
12
# File 'lib/sunspot/query/dismax.rb', line 10

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.



92
93
94
# File 'lib/sunspot/query/dismax.rb', line 92

def add_fulltext_field(field, boost = nil)
  super unless field.is_a?(Sunspot::JoinField)
end

#to_paramsObject

The query as Solr parameters



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
64
65
66
67
68
69
70
71
72
73
# File 'lib/sunspot/query/dismax.rb', line 30

def to_params
  params = {
    :q => @keywords,
    :defType => 'edismax',
    :fl => '* score'
  }

  if @fulltext_fields.any?
    params[:qf] = @fulltext_fields.values.map { |field| field.to_boosted_field }.join(' ')
  end

  params[:mm] = @minimum_match if @minimum_match
  params[:ps] = @phrase_slop if @phrase_slop
  params[:qs] = @query_phrase_slop if @query_phrase_slop
  params[:tie] = @tie if @tie

  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 @additive_boost_functions.empty?
    params[:bf] = @additive_boost_functions.map do |additive_boost_function|
      additive_boost_function.to_s
    end
  end

  unless @multiplicative_boost_functions.empty?
    params[:boost] = @multiplicative_boost_functions.map do |multiplicative_boost_function|
      multiplicative_boost_function.to_s
    end
  end

  @highlights.each do |highlight|
    Sunspot::Util.deep_merge!(params, highlight.to_params)
  end

  params
end

#to_subqueryObject

Serialize the query as a Solr nested subquery.



78
79
80
81
82
83
84
85
86
87
# File 'lib/sunspot/query/dismax.rb', line 78

def to_subquery
  params = self.to_params
  params.delete :defType
  params.delete :fl

  keywords = escape_quotes(params.delete(:q))
  options = params.map { |key, value| escape_param(key, value) }.join(' ')

  { :q => "_query_:\"{!edismax #{options}}#{keywords}\"" }
end