Class: JayAPI::Elasticsearch::QueryBuilder
- Inherits:
-
Object
- Object
- JayAPI::Elasticsearch::QueryBuilder
- 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
- #aggregations ⇒ JayAPI::Elasticsearch::QueryBuilder::Aggregations readonly
-
#collapse(field) ⇒ QueryBuilder
readonly
Adds a
collapseclause to the query. -
#from(from) ⇒ QueryBuilder
readonly
Adds a
fromclause to the query. -
#query ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses
readonly
The current set of query clauses.
-
#size(size) ⇒ QueryBuilder
readonly
Adds a
sizeclause to the query. -
#sort(sort) ⇒ QueryBuilder
readonly
Adds a
sortclause to the query. -
#source(filter_expr) ⇒ QueryBuilder
readonly
Adds a
_sourceclause to the query.
Instance Method Summary collapse
-
#initialize ⇒ QueryBuilder
constructor
Creates a new instance of the class.
-
#merge(other) ⇒ self
Returns a new
QueryBuilderobject which is the result of merging the receiver withother. -
#to_h ⇒ Hash
(also: #to_query)
The generated query.
Constructor Details
#initialize ⇒ QueryBuilder
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
#aggregations ⇒ JayAPI::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.
89 90 91 92 93 |
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 89 def collapse(field) check_argument(field, 'field', String) @collapse = field self end |
#from(from) ⇒ QueryBuilder
Adds a from clause to the query.
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 |
#query ⇒ JayAPI::Elasticsearch::QueryBuilder::QueryClauses
Returns The current set of query clauses.
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.
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.
It is also possible to pass a Hash with advanced sorting options, for example:
query_builder.sort(price: { order: :desc, missing: :_last })
78 79 80 81 82 83 84 |
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 78 def sort(sort) check_argument(sort, 'sort', Hash) @sort.merge!( sort.transform_values { |value| value.is_a?(Hash) ? value : { order: value } } ) self end |
#source(filter_expr) ⇒ QueryBuilder
Adds a _source clause to the query.
101 102 103 104 105 |
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 101 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.
120 121 122 123 124 125 126 127 128 |
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 120 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_h ⇒ Hash Also known as: to_query
Returns The generated query.
108 109 110 |
# File 'lib/jay_api/elasticsearch/query_builder.rb', line 108 def to_h build_query end |