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.



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

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

Instance Attribute Details

#search_definitionObject

Returns the value of attribute search_definition.



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

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.



79
80
81
# File 'lib/elasticity/search.rb', line 79

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.



54
55
56
57
# File 'lib/elasticity/search.rb', line 54

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



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

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



72
73
74
75
# File 'lib/elasticity/search.rb', line 72

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