Sha256: adf7375e91c7862f4ee915e2ccc7e2f08992396e809c1c6fffa0b62937d66efc

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Elastic::Dsl
  module BoolQueryBuilder
    def must(*_queries)
      with_bool_query do |query|
        query.must build_query_from_params(_queries)
      end
    end

    def should(*_queries)
      with_bool_query do |query|
        query.should build_query_from_params(_queries)
      end
    end

    def with(_modifier, &_block)
      raise ArgumentError, 'block missing' if _block.nil?
      raise ArgumentError, 'node is not a modifier' unless _modifier.respond_to? :clone_with_query

      with_bool_query do |query|
        ctx = BoolQueryContext.new index, query, _modifier
        ctx.instance_exec(&_block)
        ctx
      end
    end

    def boost(_amount = nil, field: nil, fixed: false, factor: 1, modifier: :none, missing: 1,
      &_block)
      raise ArgumentError, 'must provide at least a boost amount' if _amount.nil? && field.nil?

      node = Elastic::Nodes::FunctionScore.new
      node.boost_mode = :replace if fixed

      if !field.nil?
        node.add_field_function(field, factor: factor, modifier: modifier, missing: missing)
      elsif fixed
        node.add_weight_function(_amount)
      else
        node.boost = _amount
      end

      # TODO: add decay function support.

      with(node, &_block)
    end

    private

    def build_query_from_params(_params)
      Elastic::Commands::BuildQueryFromParams.for(index: index, params: _params)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elastic-rails-0.5.0 lib/elastic/dsl/bool_query_builder.rb