Sha256: 809e08607dc4021218bbcbc9bc7d3ddb89dba21ab562a5c2a028a77d8f546fb2

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

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

    def self.build_and(_nodes)
      new.tap { |n| n.musts = _nodes }
    end

    def self.build_or(_nodes)
      new.tap { |n| n.shoulds = _nodes }
    end

    attr_accessor :minimum_should_match, :disable_coord

    def initialize
      super
      @musts = []
      @shoulds = []
    end

    def must(_node)
      @musts << _node
    end

    def should(_node)
      @shoulds << _node
    end

    def musts=(_nodes)
      @musts = _nodes.dup.to_a
    end

    def musts
      @musts.each
    end

    def shoulds=(_nodes)
      @shoulds = _nodes.dup.to_a
    end

    def shoulds
      @shoulds.each
    end

    def traverse(&_block)
      super
      @shoulds.each { |c| c.traverse(&_block) }
      @musts.each { |c| c.traverse(&_block) }
    end

    def render
      options = {}.tap do |boolean|
        boolean['must'] = @musts.map(&:render) if !@musts.empty?
        boolean['should'] = @shoulds.map(&:render) if !@shoulds.empty?
        boolean['minimum_should_match'] = minimum_should_match unless minimum_should_match.nil?
        boolean['disable_coord'] = true if disable_coord
        render_boost(boolean)
      end

      { "bool" => options }
    end

    def clone
      prepare_clone super, @musts.map(&:clone), @shoulds.map(&:clone)
    end

    def simplify
      new_must = @musts.map(&:simplify)
      new_should = @shoulds.map(&:simplify)

      return new_must.first if new_must.length == 1 && new_should.empty?
      return new_should.first if new_should.length == 1 && new_must.empty? # at least 1 should match

      prepare_clone(super, new_must, new_should)
    end

    private

    def prepare_clone(_clone, _musts, _shoulds)
      _clone.musts = _musts
      _clone.shoulds = _shoulds
      _clone.minimum_should_match = @minimum_should_match
      _clone.disable_coord = @disable_coord
      _clone
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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