Class: Elasticity::Search::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticity/search.rb

Overview

Elasticity::Search::Facade provides a simple interface for defining a search and provides different ways of executing it against Elasticsearch. This is usually the main entry point for search.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, search_definition) ⇒ Facade

Creates a new facade for the given search definition, providing a set of helper methods to trigger different type of searches and results interpretation.


35
36
37
38
# File 'lib/elasticity/search.rb', line 35

def initialize(client, search_definition)
  @client            = client
  @search_definition = search_definition
end

Instance Attribute Details

#search_definitionObject

Returns the value of attribute search_definition


31
32
33
# File 'lib/elasticity/search.rb', line 31

def search_definition
  @search_definition
end

Instance Method Details

#active_records(relation) ⇒ Object

Performs the search only fetching document ids using it to load ActiveRecord objects from the provided relation. It returns the relation matching the objects found on ElasticSearch.


64
65
66
# File 'lib/elasticity/search.rb', line 64

def active_records(relation)
  ActiveRecordProxy.new(@client, @search_definition, relation)
end

#document_hashesObject

Performs the search using the default search type and returning an iterator that will yield hash representations of the documents.


42
43
44
# File 'lib/elasticity/search.rb', line 42

def document_hashes
  LazySearch.new(@client, @search_definition)
end

#documents(document_klass) ⇒ Object

Performs the search using the default search type and returning an iterator that will yield each document, converted to the provided document_klass.


48
49
50
51
52
# File 'lib/elasticity/search.rb', line 48

def documents(document_klass)
  LazySearch.new(@client, @search_definition) do |hit|
    document_klass.from_hit(hit)
  end
end

#scan_documents(document_klass, **options) ⇒ Object

Performs the search using the scan search type and the scoll api to iterate over all the documents as fast as possible. The sort option will be discarded.

More info: www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html


58
59
60
# File 'lib/elasticity/search.rb', line 58

def scan_documents(document_klass, **options)
  ScanCursor.new(@client, @search_definition, document_klass, **options)
end