Class: JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Inherits:
QueryClause
  • Object
show all
Includes:
MatchClauses
Defined in:
lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb

Overview

Represents an Elasticsearch boolean query clause. For more information about this type of clause check: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

Instance Method Summary collapse

Methods included from MatchClauses

#exists, #match_all, #match_none, #match_phrase, #query_string, #range, #regexp, #term, #terms, #wildcard

Constructor Details

#initializeBool

Returns a new instance of Bool.



16
17
18
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 16

def initialize
  @query_clauses = {}
end

Instance Method Details

#<<(query_clause) ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Adds a clause to the current sub-clause of the boolean clause.

Parameters:

Returns:

Raises:



71
72
73
74
75
76
77
78
79
80
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 71

def <<(query_clause)
  unless current_bool
    raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
          'Please call #must, #filter, #should or #must_not in order ' \
          'to add query clauses inside a boolean clause'
  end

  current_bool << query_clause
  self
end

#cloneself

Returns A copy of the receiver and all match clauses attached to it.

Returns:

  • (self)

    A copy of the receiver and all match clauses attached to it.



101
102
103
104
105
106
107
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 101

def clone
  self.class.new.tap do |copy|
    copy.query_clauses = query_clauses.transform_values do |array|
      array.map(&:clone)
    end
  end
end

#filter {|JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool| ... } ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Adds a filter clause to an already existing bool clause.

Yields:

Returns:

Raises:



38
39
40
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 38

def filter(&block)
  add_boolean_clause(:filter, &block)
end

#merge(other) ⇒ self

Returns A new instance of the class with the merging of the receiver abd the given QueryClause object.

Parameters:

  • other (Other)

    Another QueryClause object.

Returns:

  • (self)

    A new instance of the class with the merging of the receiver abd the given QueryClause object.



127
128
129
130
131
132
133
134
135
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 127

def merge(other)
  # This +if+ prevents the creation of a clone (which can be costly)
  # only for an +ArgumentError+ to be raised when +#merge!+ is called.
  if [self.class, self.class.superclass].any? { |klass| other.is_a?(klass) }
    clone.merge!(other)
  else
    raise_cannot_merge_error(other)
  end
end

#merge!(other) ⇒ self

Returns The receiver, after having merged the given QueryClause into itself.

Parameters:

  • other (Other)

    Another QueryClause object.

Returns:

  • (self)

    The receiver, after having merged the given QueryClause into itself.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 112

def merge!(other)
  if other.is_a?(self.class)
    merge_clauses(other)
  elsif other.is_a?(self.class.superclass)
    must << other.clone
  else
    raise_cannot_merge_error(other)
  end

  self
end

#must {|JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool| ... } ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Adds a must clause to the bool clause.

Yields:

Returns:

Raises:



27
28
29
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 27

def must(&block)
  add_boolean_clause(:must, &block)
end

#must_not {|JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool| ... } ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Adds a must_not clause to an already existing bool clause.

Yields:

Returns:

Raises:



60
61
62
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 60

def must_not(&block)
  add_boolean_clause(:must_not, &block)
end

#should {|JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool| ... } ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Adds a should clause to an already existing bool clause.

Yields:

Returns:

Raises:



49
50
51
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 49

def should(&block)
  add_boolean_clause(:should, &block)
end

#to_hHash

Returns The Hash representation of the Query Clause.

Returns:

  • (Hash)

    The Hash representation of the Query Clause



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb', line 83

def to_h
  unless query_clauses.any?
    raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
          'A boolean clause has been defined but no boolean sub-clauses were added'
  end

  unless query_clauses.values.all?(&:any?)
    raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
          'A boolean clause and a sub-clause were defined but no match clauses were added'
  end

  {
    bool: query_clauses.transform_values { |value| value.map(&:to_h) }
  }
end