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.



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

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

Instance Attribute Details

#search_definitionObject

Returns the value of attribute search_definition.



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

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.



82
83
84
# File 'lib/elasticity/search.rb', line 82

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

#document_hashes(search_args = {}) ⇒ Object

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



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

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

#documents(mapper, search_args = {}) ⇒ Object

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



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

def documents(mapper, search_args = {})
  return @documents if defined?(@documents)
  @documents = LazySearch.new(@client, @search_definition, search_args) 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



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

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