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.



44
45
46
47
# File 'lib/elasticity/search.rb', line 44

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

Instance Attribute Details

#search_definitionObject

Returns the value of attribute search_definition.



40
41
42
# File 'lib/elasticity/search.rb', line 40

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.



76
77
78
# File 'lib/elasticity/search.rb', line 76

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.



51
52
53
54
# File 'lib/elasticity/search.rb', line 51

def document_hashes
  return @document_hashes if defined?(@document_hashes)
  @document_hashes = LazySearch.new(@client, @search_definition)
end

#documents(mapper) ⇒ Object

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



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

def documents(mapper)
  return @documents if defined?(@documents)
  @documents = LazySearch.new(@client, @search_definition) do |hit|
    mapper.(hit)
  end
end

#scan_documents(mapper, **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



69
70
71
72
# File 'lib/elasticity/search.rb', line 69

def scan_documents(mapper, **options)
  return @scan_documents if defined?(@scan_documents)
  @scan_documents = ScanCursor.new(@client, @search_definition, mapper, **options)
end