Module: Stretchy::Factory

Defined in:
lib/stretchy/factory.rb

Constant Summary collapse

DEFAULT_WEIGHT =
1.5
DEFAULT_SLOP =
50
BOOST_OPTIONS =
[
  :filter,
  :function,
  :weight
]
FUNCTION_SCORE_OPTIONS =
[
  :boost,
  :max_boost,
  :score_mode,
  :boost_mode,
  :min_score
]

Class Method Summary collapse

Class Method Details

.context_nodes(params, context = default_context) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/stretchy/factory.rb', line 52

def context_nodes(params, context = default_context)
  if context[:boost]
    params_to_boost(params, context)
  elsif context[:query]
    params_to_queries(params, context)
  else
    params_to_filters(params, context)
  end
end

.decay_function_node(params = {}, context = default_context) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/stretchy/factory.rb', line 165

def decay_function_node(params = {}, context = default_context)
  boost_params        = extract_boost_params!(params)
  context[:fn_score]  = extract_function_score_options!(params)
  decay_fn            = params.delete(:decay_function)
  field               = params.delete(:field)
  Node.new({decay_fn => { field => params}}.merge(boost_params), context)
end

.default_contextObject



21
22
23
# File 'lib/stretchy/factory.rb', line 21

def default_context
  {}
end

.extract_boost_params!(params) ⇒ Object



25
26
27
28
29
# File 'lib/stretchy/factory.rb', line 25

def extract_boost_params!(params)
  boost_params = Utils.extract_options!(params, BOOST_OPTIONS)
  boost_params = {weight: DEFAULT_WEIGHT} unless boost_params.any?
  boost_params
end

.extract_function_score_options!(params) ⇒ Object



31
32
33
# File 'lib/stretchy/factory.rb', line 31

def extract_function_score_options!(params)
  Utils.extract_options!(params, FUNCTION_SCORE_OPTIONS)
end

.field_value_function_node(params = {}, context = default_context) ⇒ Object



151
152
153
154
155
# File 'lib/stretchy/factory.rb', line 151

def field_value_function_node(params = {}, context = default_context)
  context[:fn_score] = extract_function_score_options!(params)
  boost_params       = extract_boost_params!(params)
  Node.new(boost_params.merge(field_value_factor: params), context)
end

.fulltext_nodes_from_string(params, context = default_context) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/stretchy/factory.rb', line 109

def fulltext_nodes_from_string(params, context = default_context)
  subcontext = context.merge(query: true)
  nodes = [raw_node({
    match: {
      _all: {
        query: params,
        minimum_should_match: 1
      }
    }
  }, subcontext)]

  subcontext = subcontext.merge(should: true)
  nodes << Factory.raw_node({
    match_phrase: {
      _all: {
        query: params,
        slop:  DEFAULT_SLOP
      }
    }
  }, subcontext)

  nodes
end

.geo_distance_node(params = {}, context = default_context) ⇒ Object



146
147
148
# File 'lib/stretchy/factory.rb', line 146

def geo_distance_node(params = {}, context = default_context)
  Node.new({geo_distance: params}, context)
end

.more_like_node(params = {}, context = default_context) ⇒ Object



134
135
136
# File 'lib/stretchy/factory.rb', line 134

def more_like_node(params = {}, context = default_context)
  Node.new({more_like_this: params}, context)
end

.params_to_boost(params, context = default_context) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stretchy/factory.rb', line 62

def params_to_boost(params, context = default_context)
  boost_params        = extract_boost_params!(params)
  context[:fn_score]  = extract_function_score_options!(params)
  subcontext          = context.merge(boost: nil)
  nodes               = context_nodes(params, subcontext)
  collector           = AndCollector.new(nodes, subcontext)

  if context[:query]
    Node.new(boost_params.merge(filter: {query: collector.json}), context)
  else
    Node.new(
      boost_params.merge(filter: collector.filter_node.json),
      context
    )
  end
end

.params_to_filters(params, context = default_context) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stretchy/factory.rb', line 95

def params_to_filters(params, context = default_context)
  params.map do |field, val|
    case val
    when Range
      Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
    when nil
      Node.new({missing: {field: field}}, context)
    else
      Node.new({terms: {field => Array(val)}}, context)
    end
  end
end

.params_to_queries(params, context = default_context) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/stretchy/factory.rb', line 79

def params_to_queries(params, context = default_context)
  params.map do |field, val|
    case val
    when Array
      Node.new({match: {
        field     => {query: val.join(' '),
        :operator => :or
      }}}, context)
    when Range
      Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
    else
      Node.new({match: {field => val}}, context)
    end
  end
end

.random_score_function_node(params, context = default_context) ⇒ Object



158
159
160
161
162
# File 'lib/stretchy/factory.rb', line 158

def random_score_function_node(params, context = default_context)
  json          = {random_score: {seed: params[:seed]}}
  json[:weight] = params[:weight] if params[:weight]
  Node.new(json, context)
end

.range_node(params = {}, context = default_context) ⇒ Object



141
142
143
# File 'lib/stretchy/factory.rb', line 141

def range_node(params = {}, context = default_context)
  Node.new({range: params}, context)
end

.raw_boost_node(params, context) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/stretchy/factory.rb', line 43

def raw_boost_node(params, context)
  boost_params       = extract_boost_params!(params)
  context[:fn_score] = extract_function_score_options!(params)
  context[:boost]    ||= true
  context[:filter]   ||= true
  json = context[:query] ? {query: params} : params
  Node.new(boost_params.merge(filter: json), context)
end

.raw_node(params, context) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/stretchy/factory.rb', line 35

def raw_node(params, context)
  if context[:boost]
    raw_boost_node(params, context)
  else
    Node.new(params, context)
  end
end