Class: JayAPI::Elasticsearch::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_api/elasticsearch/query_builder.rb,
lib/jay_api/elasticsearch/query_builder/errors.rb,
lib/jay_api/elasticsearch/query_builder/script.rb,
lib/jay_api/elasticsearch/query_builder/aggregations.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/avg.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/max.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sum.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/terms.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/aggregations/errors.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/filter.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/aggregations/top_hits.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/negator.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/composite.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/aggregations/aggregation.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/cardinality.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/value_count.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_none.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sources/terms.rb,
lib/jay_api/elasticsearch/query_builder/errors/query_builder_error.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/aggregations/date_histogram.rb,
lib/jay_api/elasticsearch/query_builder/query_clauses/match_clauses.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/scripted_metric.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sources/sources.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/errors/aggregations_error.rb

Overview

A helper class to build simple and common queries for Elasticsearch. Queries are created with the Elasticsearch Query DSL: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

Defined Under Namespace

Modules: Errors Classes: Aggregations, QueryClauses, Script

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Creates a new instance of the class. A new instance of the class will produce an empty query.



23
24
25
26
27
28
29
30
31
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 23

def initialize
  @from = nil
  @size = nil
  @source = nil
  @sort = {}
  @collapse = nil
  @query = ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses.new
  @aggregations = JayAPI::Elasticsearch::QueryBuilder::Aggregations.new
end

Instance Attribute Details

#aggregationsJayAPI::Elasticsearch::QueryBuilder::Aggregations



15
16
17
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 15

def aggregations
  @aggregations
end

#collapse(field) ⇒ QueryBuilder

Adds a collapse clause to the query.

Parameters:

  • field (String)

    The field to use for collapsing the results.

Returns:

  • (QueryBuilder)

    itself so that other methods can be chained.



80
81
82
83
84
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 80

def collapse(field)
  check_argument(field, 'field', String)
  @collapse = field
  self
end

#from(from) ⇒ QueryBuilder

Adds a from clause to the query.

Parameters:

  • from (Integer)

    The value for the from clause.

Returns:

  • (QueryBuilder)

    itself so that other methods can be chained.



36
37
38
39
40
41
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 36

def from(from)
  check_argument(from, 'from', Integer)
  check_positive_argument(from, 'from')
  @from = from
  self
end

#queryJayAPI::Elasticsearch::QueryBuilder::QueryClauses

Returns The current set of query clauses.

Returns:



19
20
21
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 19

def query
  @query
end

#size(size) ⇒ QueryBuilder

Adds a size clause to the query.

Parameters:

  • size (Integer)

    The value for the size clause.

Returns:

  • (QueryBuilder)

    itself so that other methods can be chained.



46
47
48
49
50
51
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 46

def size(size)
  check_argument(size, 'size', Integer)
  check_positive_argument(size, 'size')
  @size = size
  self
end

#sort(sort) ⇒ QueryBuilder

Adds a sort clause to the query. This method can be called with multiple fields at once or called multiple times.

Example:

query_builder.sort(name: 'asc', age: 'desc')

or

query_builder.sort(name: 'asc')
query_builder.sort(age: 'desc')

Both will produce the same sort clause.

Parameters:

  • sort (Hash)

    A Hash whose keys are the name of the fields and the keys are the direction of the sorting, either asc or desc.

Returns:

  • (QueryBuilder)

    itself so that other methods can be chained.



71
72
73
74
75
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 71

def sort(sort)
  check_argument(sort, 'sort', Hash)
  @sort.merge!(sort)
  self
end

#source(filter_expr) ⇒ QueryBuilder

Adds a _source clause to the query.

Parameters:

  • filter_expr (FalseClass, String, Array<String>, Hash)

    Expression used for source filtering.

Returns:

  • (QueryBuilder)

    itself so that other methods can be chained.

See Also:



92
93
94
95
96
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 92

def source(filter_expr)
  check_argument(filter_expr, 'source', FalseClass, String, Array, Hash)
  @source = filter_expr
  self
end

Instance Method Details

#merge(other) ⇒ self

Returns a new QueryBuilder object which is the result of merging the receiver with other.

Parameters:

  • other (self)

    Another instance of QueryBuilder.

Returns:

  • (self)

    A new QueryBuilder, the result of the merge of both objects.

Raises:

  • (TypeError)

    If the given object is not a QueryBuilder.



111
112
113
114
115
116
117
118
119
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 111

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

  other.combine(
    from: @from, size: @size, source: @source, collapse: @collapse,
    sort: @sort, query: @query, aggregations: @aggregations
  )
end

#to_hHash Also known as: to_query

Returns The generated query.

Returns:

  • (Hash)

    The generated query.



99
100
101
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 99

def to_h
  build_query
end