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
- .context_nodes(params, context = default_context) ⇒ Object
- .decay_function_node(params = {}, context = default_context) ⇒ Object
- .default_context ⇒ Object
- .extract_boost_params!(params) ⇒ Object
- .extract_function_score_options!(params) ⇒ Object
- .field_value_function_node(params = {}, context = default_context) ⇒ Object
- .fulltext_nodes_from_string(params, context = default_context) ⇒ Object
- .geo_distance_node(params = {}, context = default_context) ⇒ Object
- .more_like_node(params = {}, context = default_context) ⇒ Object
- .params_to_boost(params, context = default_context) ⇒ Object
- .params_to_filters(params, context = default_context) ⇒ Object
- .params_to_queries(params, context = default_context) ⇒ Object
- .random_score_function_node(params, context = default_context) ⇒ Object
- .range_node(params = {}, context = default_context) ⇒ Object
- .raw_boost_node(params, context) ⇒ Object
- .raw_node(params, context) ⇒ Object
Class Method Details
.context_nodes(params, context = default_context) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/stretchy/factory.rb', line 50 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
162 163 164 165 166 167 168 |
# File 'lib/stretchy/factory.rb', line 162 def decay_function_node(params = {}, context = default_context) boost_params = extract_boost_params!(params) context[:fn_score] = (params) decay_fn = params.delete(:decay_function) field = params.delete(:field) Node.new({decay_fn => { field => params}}.merge(boost_params), context) end |
.default_context ⇒ Object
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.(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 (params) Utils.(params, FUNCTION_SCORE_OPTIONS) end |
.field_value_function_node(params = {}, context = default_context) ⇒ Object
148 149 150 151 152 |
# File 'lib/stretchy/factory.rb', line 148 def field_value_function_node(params = {}, context = default_context) context[:fn_score] = (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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/stretchy/factory.rb', line 106 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
143 144 145 |
# File 'lib/stretchy/factory.rb', line 143 def geo_distance_node(params = {}, context = default_context) Node.new({geo_distance: params}, context) end |
.more_like_node(params = {}, context = default_context) ⇒ Object
131 132 133 |
# File 'lib/stretchy/factory.rb', line 131 def more_like_node(params = {}, context = default_context) Node.new({more_like_this: params}, context) end |
.params_to_boost(params, context = default_context) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/stretchy/factory.rb', line 60 def params_to_boost(params, context = default_context) boost_params = extract_boost_params!(params) context[:fn_score] = (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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/stretchy/factory.rb', line 83 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
77 78 79 80 81 |
# File 'lib/stretchy/factory.rb', line 77 def params_to_queries(params, context = default_context) params.map do |field, val| Node.new({match: {field => val}}, context) end end |
.random_score_function_node(params, context = default_context) ⇒ Object
155 156 157 158 159 |
# File 'lib/stretchy/factory.rb', line 155 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
138 139 140 |
# File 'lib/stretchy/factory.rb', line 138 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 |
# File 'lib/stretchy/factory.rb', line 43 def raw_boost_node(params, context) context[:fn_score] = (params) context[:boost] ||= true context[:filter] ||= true Node.new(params, 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 |