Module: Elasticsearch::Persistence::Repository::Search
- Included in:
- Elasticsearch::Persistence::Repository
- Defined in:
- lib/elasticsearch/persistence/repository/search.rb
Overview
Returns a collection of domain objects by an Elasticsearch query
Instance Method Summary collapse
-
#count(query_or_definition = nil, options = {}) ⇒ Integer
Return the number of domain object in the index.
-
#search(query_or_definition, options = {}) ⇒ Elasticsearch::Persistence::Repository::Response::Results
Returns a collection of domain objects by an Elasticsearch query.
Instance Method Details
#count(query_or_definition = nil, options = {}) ⇒ Integer
Return the number of domain object in the index
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/elasticsearch/persistence/repository/search.rb', line 98 def count(query_or_definition=nil, ={}) query_or_definition ||= { query: { match_all: {} } } request = { index: index_name } if query_or_definition.respond_to?(:to_hash) request[:body] = query_or_definition.to_hash elsif query_or_definition.is_a?(String) request[:q] = query_or_definition else raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" + " -- #{query_or_definition.class} given." end client.count(request.merge())[COUNT] end |
#search(query_or_definition, options = {}) ⇒ Elasticsearch::Persistence::Repository::Response::Results
Returns a collection of domain objects by an Elasticsearch query
Pass the query either as a string or a Hash-like object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/elasticsearch/persistence/repository/search.rb', line 62 def search(query_or_definition, ={}) request = { index: index_name } if query_or_definition.respond_to?(:to_hash) request[:body] = query_or_definition.to_hash elsif query_or_definition.is_a?(String) request[:q] = query_or_definition else raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" + " -- #{query_or_definition.class} given." end Response::Results.new(self, client.search(request.merge())) end |