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
14
# File 'lib/sunspot/query/more_like_this.rb', line 6

def initialize(document)
  @document_scope = Restriction::EqualTo.new(
    false,
    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)


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

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



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

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

#maximum_query_terms=(maxqt) ⇒ Object



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

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

#maximum_word_length=(maxwl) ⇒ Object



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

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

#minimum_document_frequency=(mindf) ⇒ Object



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

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

#minimum_term_frequency=(mintf) ⇒ Object



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

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

#minimum_word_length=(minwl) ⇒ Object



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

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

#to_paramsObject



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

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