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

Since:

  • 6.0.0

Instance Method Summary collapse

Instance Method Details

#count(query_or_definition = nil, options = {}) ⇒ Integer

Return the number of domain object in the index

Examples:

Return the number of all domain objects


repository.count
# => 2

Return the count of domain object matching a simple query


repository.count('fox or dog')
# => 1

Return the count of domain object matching a query in the Elasticsearch DSL


repository.count(query: { match: { title: 'fox dog' } })
# => 1

Parameters:

  • query_or_definition (Hash, String) (defaults to: nil)

    The query or search definition.

  • options (Hash) (defaults to: {})

    The search options.

Returns:

  • (Integer)

Since:

  • 6.0.0



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/elasticsearch/persistence/repository/search.rb', line 99

def count(query_or_definition=nil, options={})
  query_or_definition ||= { query: { match_all: {} } }
  request = { index: index_name,
              type: document_type }

  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(options))[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

Examples:

Return objects matching a simple query


repository.search('fox or dog')

Return objects matching a query in the Elasticsearch DSL


repository.search(query: { match: { title: 'fox dog' } })

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 <em>fox</em>", "fast white <em>dog</em>"]

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", ... ]

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}

Parameters:

  • query_or_definition (Hash, String)

    The query or search definition.

  • options (Hash) (defaults to: {})

    The search options.

Returns:

Since:

  • 6.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elasticsearch/persistence/repository/search.rb', line 62

def search(query_or_definition, options={})
  request = { index: index_name,
              type: document_type }
  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(options)))
end