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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
MatchClauses
Defined in:
lib/jay_api/elasticsearch/query_builder/query_clauses.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/term.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/terms.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/exists.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/regexp.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/negator.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/wildcard.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_all.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_none.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_phrase.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/query_clause.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/query_string.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_clauses.rb

Overview

Represents the set of query clauses in an Elasticsearch query. An empty set of clauses produces a “match all” query clause.

Defined Under Namespace

Modules: MatchClauses Classes: Bool, Exists, MatchAll, MatchNone, MatchPhrase, Negator, QueryClause, QueryString, Range, Regexp, Term, Terms, Wildcard

Instance Method Summary collapse

Methods included from MatchClauses

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

Instance Method Details

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

Adds the given query clause as top-level clause if none exists yet.

Parameters:

Returns:

Raises:



52
53
54
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 52

def <<(query_clause)
  replace_top_level_clause(query_clause)
end

#bool {|JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool| ... } ⇒ self, JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool

Turns the query into a Compound Boolean query by adding a bool clause and yields the latter so that sub-clauses can be added to it. If the query is already a boolean query the current boolean clause is yielded.

Yields:

Returns:

Raises:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 33

def bool
  clause ||= boolean_clause || ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool.new
  replace_top_level_clause(clause, force: boolean_query?)

  if block_given?
    yield clause
    self
  else
    clause
  end
end

#boolean_query?Boolean

Returns True if the current Query Clauses set includes a bool clause, false otherwise.

Returns:

  • (Boolean)

    True if the current Query Clauses set includes a bool clause, false otherwise.



67
68
69
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 67

def boolean_query?
  top_level_clause.is_a?(::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool)
end

#cloneself

Clones the receiver and the enclosed top-level clause (if any).

Returns:

  • (self)

    A copy of the receiver.



73
74
75
76
77
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 73

def clone
  self.class.new.tap do |copy|
    copy << top_level_clause.clone
  end
end

#merge(other) ⇒ self

Creates a new QueryClauses object by merging the receiver with the given object. The individual top-query clauses are merged together using a boolean clause.

Parameters:

  • other (self)

    The QueryClauses object the receiver should be merged with.

Returns:

  • (self)

    A new QueryClauses object which is a combination of the receiver and the given object.

Raises:

  • (TypeError)


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

def merge(other)
  klass = self.class
  raise TypeError, "Cannot merge #{klass} with #{other.class}" unless other.is_a?(klass)

  if other.empty?
    clone
  elsif empty?
    other.clone
  else
    klass.new.tap do |merged|
      merged.bool.merge!(top_level_clause).merge!(other.top_level_clause)
    end
  end
end

#negateself

Returns A negated version of the receiver (with its top-level query clause wrapped in a must_not boolean query or replaced by its inverse clause).

Returns:

  • (self)

    A negated version of the receiver (with its top-level query clause wrapped in a must_not boolean query or replaced by its inverse clause)



118
119
120
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 118

def negate
  clone.negate!
end

#negate!self

Negates the receiver by wrapping its top-level query clause in a must_not boolean clause or replacing it by its inverse clause.

Returns:

  • (self)

    Returns itself.



104
105
106
107
108
109
110
111
112
113
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses.rb', line 104

def negate!
  if top_level_clause
    @top_level_clause = ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Negator.new(top_level_clause)
                                                                                    .negate
  else
    match_none
  end

  self
end

#to_hHash

Returns The Hash representation of the Query Clauses set.

Returns:

  • (Hash)

    The Hash representation of the Query Clauses set.

Raises:



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

def to_h
  return self.class.new.match_all.to_h if empty?

  top_level_clause.to_h
end