Sha256: 50be15a5852d77d536ba49428580c1a712b954aba3856270be697713d82d46f9

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Elastic::Nodes
  class Term < Base
    include Concerns::Boostable

    BOOLEAN_MODE = [:any, :all]

    attr_accessor :field, :mode

    def terms=(_terms)
      @terms = _terms.dup.to_a
    end

    def mode=(_value)
      if !_value.nil? && !BOOLEAN_MODE.include?(_value)
        raise ArgumentError, "invalid mode #{_value}"
      end

      @mode = _value
    end

    def terms
      @terms.each
    end

    def clone
      prepare_clone(super)
    end

    def simplify
      prepare_clone(super)
    end

    def render
      raise ArgumentError, 'must provide at least one term' if !@terms || @terms.empty?

      if @terms.length == 1
        { 'term' => { @field.to_s => render_boost('value' => @terms.first) } }
      elsif @mode == :all
        {
          'bool' => render_boost(
            'must' => @terms.map { |t| { 'term' => { @field.to_s => t } } }
          )
        }
      else
        { 'terms' => render_boost(@field.to_s => @terms) }
      end
    end

    private

    def prepare_clone(_clone)
      _clone.field = @field
      _clone.terms = @terms
      _clone.mode = @mode
      _clone
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elastic-rails-0.5.0 lib/elastic/nodes/term.rb