Class: Sunspot::Query::MoreLikeThis

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MoreLikeThis) initialize(document)

A new instance of MoreLikeThis



6
7
8
9
10
11
12
13
14
# File 'sunspot/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

- (Object) fields (readonly)

Returns the value of attribute fields



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

def fields
  @fields
end

Instance Method Details

- (Object) add_field(field, boost = nil)

Raises:

  • (ArgumentError)


16
17
18
19
# File 'sunspot/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

- (Object) boost_by_relevance=(should_boost)



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

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

- (Object) maximum_query_terms=(maxqt)



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

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

- (Object) maximum_word_length=(maxwl)



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

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

- (Object) minimum_document_frequency=(mindf)



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

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

- (Object) minimum_term_frequency=(mintf)



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

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

- (Object) minimum_word_length=(minwl)



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

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

- (Object) to_params



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'sunspot/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