Class: Stretchy::Queries::MoreLikeThisQuery

Inherits:
Base
  • Object
show all
Defined in:
lib/stretchy/queries/more_like_this_query.rb

Instance Method Summary collapse

Methods included from Utils::Validation

#errors, included, #json_attributes, #require_one!, #require_only_one!, #valid?, #validate!, #validator

Instance Method Details

#after_initialize(params = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/stretchy/queries/more_like_this_query.rb', line 25

def after_initialize(params = {})
  if params[:docs]
    @docs = coerce_docs(Array(params[:docs]))
  end
  
  require_one!(:like_text, :docs, :ids)
end

#coerce_docs(docs) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stretchy/queries/more_like_this_query.rb', line 33

def coerce_docs(docs)
  docs.map do |doc|
    coerced = {
      '_index'  => doc['_index']  || doc[:_index],
      '_type'   => doc['_type']   || doc[:_type],
    }
    
    source = doc[:doc] || doc['']
    if source
      coerced['doc'] = source
    else  
      coerced['_id'] = doc['_id'] || doc[:_id]
    end
    coerced
  end
end

#to_searchObject



50
51
52
53
54
# File 'lib/stretchy/queries/more_like_this_query.rb', line 50

def to_search
  {
    more_like_this: json_attributes
  }
end