Class: Sunspot::Query::MoreLikeThis

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ MoreLikeThis

Returns a new instance of MoreLikeThis.


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

def initialize(document)
  @document_scope = Restriction::EqualTo.new(
    IdField.instance,
    Adapters::InstanceAdapter.adapt(document).index_id
  )
  @fields = {}
  @params = {}
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields


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

def fields
  @fields
end

Instance Method Details

#add_field(field, boost = nil) ⇒ Object

Raises:

  • (ArgumentError)

15
16
17
18
# File 'lib/sunspot/query/more_like_this.rb', line 15

def add_field(field, boost = nil)
  raise(ArgumentError, "Field #{field.name} is not set up for more_like_this") unless field.more_like_this?
  @fields[field.indexed_name] = TextFieldBoost.new(field, boost)
end

#boost_by_relevance=(should_boost) ⇒ Object


40
41
42
# File 'lib/sunspot/query/more_like_this.rb', line 40

def boost_by_relevance=(should_boost)
  @params[:"mlt.boost"] = should_boost
end

#maximum_query_terms=(maxqt) ⇒ Object


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

def maximum_query_terms=(maxqt)
  @params[:"mlt.maxqt"] = maxqt
end

#maximum_word_length=(maxwl) ⇒ Object


32
33
34
# File 'lib/sunspot/query/more_like_this.rb', line 32

def maximum_word_length=(maxwl)
  @params[:"mlt.maxwl"] = maxwl
end

#minimum_document_frequency=(mindf) ⇒ Object


24
25
26
# File 'lib/sunspot/query/more_like_this.rb', line 24

def minimum_document_frequency=(mindf)
  @params[:"mlt.mindf"] = mindf
end

#minimum_term_frequency=(mintf) ⇒ Object


20
21
22
# File 'lib/sunspot/query/more_like_this.rb', line 20

def minimum_term_frequency=(mintf)
  @params[:"mlt.mintf"] = mintf
end

#minimum_word_length=(minwl) ⇒ Object


28
29
30
# File 'lib/sunspot/query/more_like_this.rb', line 28

def minimum_word_length=(minwl)
  @params[:"mlt.minwl"] = minwl
end

#to_paramsObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sunspot/query/more_like_this.rb', line 44

def to_params
  params = Sunspot::Util.deep_merge(
    @params,
    :q => @document_scope.to_boolean_phrase
  )
  params[:"mlt.fl"] = @fields.keys.join(",")
  boosted_fields = @fields.values.select { |field| field.boost }
  unless boosted_fields.empty?
    params[:qf] = boosted_fields.map do |field|
      field.to_boosted_field
    end.join(' ')
  end
  params
end