Class: ArtirixDataModels::FakeResponseFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/artirix_data_models/fake_response_factory.rb

Class Method Summary collapse

Class Method Details

.response(model_factory, factory_params: {}, from: 0, size: ArtirixDataModels::EsCollection::DEFAULT_SIZE, max_page: 10, traits: [], index_name: nil, document_type: nil, aggregations: []) ⇒ Object



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
# File 'lib/artirix_data_models/fake_response_factory.rb', line 33

def self.response(model_factory, factory_params: {}, from: 0, size: ArtirixDataModels::EsCollection::DEFAULT_SIZE, max_page: 10, traits: [], index_name: nil, document_type: nil, aggregations: [])
  max_page ||= 10

  current_page = (from / size) + 1

  total_hits = (size * (max_page - 0.5)).to_i

  if current_page > max_page
    # no results (page too high)
    current_hits = 0
  elsif current_page == max_page
    # final page
    current_hits = (size / 2).to_i
  else
    # other page
    current_hits = size
  end

  result_hits       = current_hits.times.collect { FactoryGirl.attributes_for(model_factory, *traits, factory_params) }

  # ensure that each element has a decreasing score

  # max score (1st element in search)
  total_max_score   = total_hits * 10.2

  # first element in this batch => has to be lower than any score from the previous batches
  present_max_score = (total_hits - from) * 10.2

  build_response document_type:     document_type,
                 index_name:        index_name,
                 present_max_score: present_max_score,
                 result_hits:       result_hits,
                 total_hits:        total_hits,
                 total_max_score:   total_max_score,
                 aggregations:      aggregations
end

.response_by_results(result_hits, index_name: nil, document_type: nil, total_hits: nil, aggregations: []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/artirix_data_models/fake_response_factory.rb', line 17

def self.response_by_results(result_hits, index_name: nil, document_type: nil, total_hits: nil, aggregations: [])

  total_hits ||= result_hits.size

  max_score = result_hits.map { |r| r.fetch(:_score, 0) }.max
  max_score = max_score > 0 ? max_score : total_hits * 10.2

  build_response document_type:     document_type,
                 index_name:        index_name,
                 present_max_score: max_score,
                 result_hits:       result_hits,
                 total_hits:        total_hits,
                 total_max_score:   max_score,
                 aggregations:      aggregations
end

.response_single_model(model, score: 14.5, aggregations: []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/artirix_data_models/fake_response_factory.rb', line 3

def self.response_single_model(model, score: 14.5, aggregations: [])
  index_name    = model.class.to_s.demodulize.pluralize.underscore
  document_type = model.class.to_s.demodulize.singularize.underscore


  build_response document_type:     document_type,
                 index_name:        index_name,
                 present_max_score: score,
                 result_hits:       [model.compact_data_hash],
                 total_hits:        1,
                 total_max_score:   score,
                 aggregations:      aggregations
end