Module: Elasticsearch::Persistence::Repository::Search
- Included in:
- Class
- 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/elasticsearch/persistence/repository/search.rb', line 76 def count(query_or_definition=nil, ={}) query_or_definition ||= { query: { match_all: {} } } type = document_type || (klass ? __get_type_from_class(klass) : nil ) case when query_or_definition.respond_to?(:to_hash) response = client.count( { index: index_name, type: type, body: query_or_definition.to_hash }.merge() ) when query_or_definition.is_a?(String) response = client.count( { index: index_name, type: type, q: query_or_definition }.merge() ) else raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String, not as [#{query_or_definition.class}]" end response['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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/elasticsearch/persistence/repository/search.rb', line 42 def search(query_or_definition, ={}) type = document_type || (klass ? __get_type_from_class(klass) : nil ) case when query_or_definition.respond_to?(:to_hash) response = client.search( { index: index_name, type: type, body: query_or_definition.to_hash }.merge() ) when query_or_definition.is_a?(String) response = client.search( { index: index_name, type: type, q: query_or_definition }.merge() ) 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, response) end |