module Elasticsearch module Persistence module Repository # Returns a collection of domain objects by an Elasticsearch query # module Search # Returns a collection of domain objects by an Elasticsearch query # # Pass the query either as a string or a Hash-like object # # @example Return objects matching a simple query # # repository.search('fox or dog') # # @example Return objects matching a query in the Elasticsearch DSL # # repository.search(query: { match: { title: 'fox dog' } }) # # @example Define additional search parameters, such as highlighted excerpts # # results = repository.search(query: { match: { title: 'fox dog' } }, highlight: { fields: { title: {} } }) # results.map_with_hit { |d,h| h.highlight.title.join } # # => ["quick brown fox", "fast white dog"] # # @example Perform aggregations as part of the request # # results = repository.search query: { match: { title: 'fox dog' } }, # aggregations: { titles: { terms: { field: 'title' } } } # results.response.aggregations.titles.buckets.map { |term| "#{term['key']}: #{term['doc_count']}" } # # => ["brown: 1", "dog: 1", ... ] # # @example Pass additional options to the search request, such as `size` # # repository.search query: { match: { title: 'fox dog' } }, size: 25 # # GET http://localhost:9200/notes/note/_search # # > {"query":{"match":{"title":"fox dog"}},"size":25} # # @return [Elasticsearch::Persistence::Repository::Response::Results] # def search(query_or_definition, options={}) 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(options) ) when query_or_definition.is_a?(String) response = client.search( { index: index_name, type: type, q: query_or_definition }.merge(options) ) 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 end end end end