module Stretchy::Factory

Constants

BOOST_OPTIONS
DEFAULT_SLOP
DEFAULT_WEIGHT
FUNCTION_SCORE_OPTIONS

Public Instance Methods

context_nodes(params, context = default_context) click to toggle source
# File lib/stretchy/factory.rb, line 58
def context_nodes(params, context = default_context)
  if context[:boost]
    params_to_boost(params, context)
  elsif context[:filter] && !context[:query]
    params_to_filters(dotify_params(params, context), context)
  else
    params_to_queries(dotify_params(params, context), context)
  end
end
decay_function_node(params = {}, context = default_context) click to toggle source

www.elastic.co/guide/en/elasticsearch/reference/current/querydslfunctionscorequery.html#functiondecay

# File lib/stretchy/factory.rb, line 191
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_context() click to toggle source
# File lib/stretchy/factory.rb, line 21
def default_context
  {}
end
dotify_params(params, context) click to toggle source
# File lib/stretchy/factory.rb, line 33
def dotify_params(params, context)
  if context[:nested]
    Utils.nestify(params)
  else
    Utils.dotify(params)
  end
end
extract_boost_params!(params) click to toggle source
# File lib/stretchy/factory.rb, line 25
def extract_boost_params!(params)
  Utils.extract_options!(params, BOOST_OPTIONS)
end
extract_function_score_options!(params) click to toggle source
# File lib/stretchy/factory.rb, line 29
def extract_function_score_options!(params)
  Utils.extract_options!(params, FUNCTION_SCORE_OPTIONS)
end
field_value_function_node(params = {}, context = default_context) click to toggle source

www.elastic.co/guide/en/elasticsearch/reference/current/querydslfunctionscorequery.html#functionfieldvaluefactor

# File lib/stretchy/factory.rb, line 177
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) click to toggle source

www.elastic.co/guide/en/elasticsearch/guide/current/proximity-relevance.html

# File lib/stretchy/factory.rb, line 137
def fulltext_nodes_from_string(params, context = default_context)
  subcontext = context.merge(query: true)
  nodes = [raw_node({
      multi_match: {
        query: params,
        minimum_should_match: 1
      }
  }, subcontext)]

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

  nodes
end
geo_distance_node(params = {}, context = default_context) click to toggle source

www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html

# File lib/stretchy/factory.rb, line 172
def geo_distance_node(params = {}, context = default_context)
  Node.new({geo_distance: params}, context)
end
more_like_node(params = {}, context = default_context) click to toggle source

www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html

# File lib/stretchy/factory.rb, line 160
def more_like_node(params = {}, context = default_context)
  Node.new({more_like_this: params}, context)
end
nested(params, path, context = default_context) click to toggle source
# File lib/stretchy/factory.rb, line 122
def nested(params, path, context = default_context)
  nodes = if context[:filter] && !context[:query]
    params_to_filters(params, context)
  else
    params_to_queries(params, context)
  end
  json  = AndCollector.new(nodes, context).json

  Node.new({nested: {
    path:   path,
    query:  json
  }}, context)
end
params_to_boost(params, context = default_context) click to toggle source
# File lib/stretchy/factory.rb, line 68
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)

  boost_params.merge!(filter: collector.json) if collector.any?
  if boost_params.count == 1 && boost_params.key?(:filter)
    boost_params[:weight] = DEFAULT_WEIGHT
  end

  Node.new(boost_params, context)
end
params_to_filters(params, context = default_context) click to toggle source
# File lib/stretchy/factory.rb, line 106
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
      nil_ctx = context.merge(must_not: true)
      Node.new({exists: {field: field}}, nil_ctx)
    when Hash
      nested(val, field, context)
    else
      Node.new({terms: {field => Array(val)}}, context)
    end
  end
end
params_to_queries(params, context = default_context) click to toggle source
# File lib/stretchy/factory.rb, line 83
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)
    when Hash
      nested(val, field, context)
    else
      if field == '_all' 
        Node.new({multi_match: {:query => val}}, context)
      else
        Node.new({match: {field => val}}, context)
      end
    end
  end
end
random_score_function_node(params, context = default_context) click to toggle source

www.elastic.co/guide/en/elasticsearch/reference/current/querydslfunctionscorequery.html#functionrandom

# File lib/stretchy/factory.rb, line 184
def random_score_function_node(params, context = default_context)
  json          = {random_score: {seed: params[:seed], field: :id}}
  json[:weight] = params[:weight] if params[:weight]
  Node.new(json, context)
end
range_node(params = {}, context = default_context) click to toggle source

query and filter use the same syntax www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-filter.html

# File lib/stretchy/factory.rb, line 167
def range_node(params = {}, context = default_context)
  Node.new({range: params}, context)
end
raw_boost_node(params, context) click to toggle source
# File lib/stretchy/factory.rb, line 49
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
  boost_params.merge!(filter: params) unless Utils.is_empty? params
  Node.new(boost_params, context)
end
raw_node(params, context) click to toggle source
# File lib/stretchy/factory.rb, line 41
def raw_node(params, context)
  if context[:boost]
    raw_boost_node(params, context)
  else
    Node.new(params, context)
  end
end