Class: ElasticAdapter::Index
- Inherits:
-
Object
- Object
- ElasticAdapter::Index
- Defined in:
- lib/elastic_adapter/index.rb
Overview
This class encapsulates the access to a Elasticsearch::Transport::Client and provides an implementation of the repository pattern
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
the client who handles the communication with elasticsearch.
-
#document_type ⇒ DocumentType
readonly
a DocumentType with the document name and it’s mappings.
-
#log ⇒ Boolean
readonly
print degub log to $stdout.
-
#name ⇒ String
readonly
the name of the index.
-
#settings ⇒ Hash
readonly
the index settings.
-
#url ⇒ String
readonly
the url to elasticsearch.
Instance Method Summary collapse
-
#aggregate(query) ⇒ Hash
Executes a search request and returns the response.
-
#count(query = { query: { match_all: {} } }) ⇒ Hash
Returns the document count for the index.
-
#create_index ⇒ Hash
Creates the index with it’s settings and mappings.
-
#delete_index ⇒ Hash
Deletes the index.
-
#get(id) ⇒ ElasticAdapter::HitDecorator
Returns the document with the given id from the index.
-
#index(document) ⇒ Hash
Indexes a Hash or anything that responds to to_hash as a document in the index.
-
#initialize(params) ⇒ Index
constructor
A new instance of Index.
-
#search(query) ⇒ Hash
Searches the index for documents matching the passed query.
-
#suggest(query) ⇒ Hash
Searches the index for suggestions for the passed suggest query.
-
#validate(query) ⇒ Hash
Validates the passed query.
Constructor Details
#initialize(params) ⇒ Index
Returns a new instance of Index.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/elastic_adapter/index.rb', line 40 def initialize(params) @name = params.fetch(:name) @settings = params.fetch(:settings) @document_type = params.fetch(:document_type) @url = params.fetch(:url) @log = params.fetch(:log) @client = params.fetch :client do Elasticsearch::Client.new(url: url, log: log) end self end |
Instance Attribute Details
#client ⇒ Client (readonly)
the client who handles the communication with elasticsearch
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def client @client end |
#document_type ⇒ DocumentType (readonly)
a DocumentType with the document name and it’s mappings
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def document_type @document_type end |
#log ⇒ Boolean (readonly)
print degub log to $stdout
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def log @log end |
#name ⇒ String (readonly)
the name of the index
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def name @name end |
#settings ⇒ Hash (readonly)
the index settings
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def settings @settings end |
#url ⇒ String (readonly)
the url to elasticsearch
29 30 31 |
# File 'lib/elastic_adapter/index.rb', line 29 def url @url end |
Instance Method Details
#aggregate(query) ⇒ Hash
Executes a search request and returns the response
177 178 179 180 181 182 |
# File 'lib/elastic_adapter/index.rb', line 177 def aggregate(query) client.search( index: name, body: query ) end |
#count(query = { query: { match_all: {} } }) ⇒ Hash
Returns the document count for the index
83 84 85 |
# File 'lib/elastic_adapter/index.rb', line 83 def count(query = { query: { match_all: {} } }) client.count index: name, body: query end |
#create_index ⇒ Hash
Creates the index with it’s settings and mappings
58 59 60 61 62 63 64 65 66 |
# File 'lib/elastic_adapter/index.rb', line 58 def create_index client.indices.create( index: name, body: { mappings: document_type.mappings, settings: settings } ) end |
#delete_index ⇒ Hash
Deletes the index
73 74 75 |
# File 'lib/elastic_adapter/index.rb', line 73 def delete_index client.indices.delete index: name end |
#get(id) ⇒ ElasticAdapter::HitDecorator
Returns the document with the given id from the index
117 118 119 120 121 122 123 |
# File 'lib/elastic_adapter/index.rb', line 117 def get(id) client.get( index: name, type: document_type.name, id: id ) end |
#index(document) ⇒ Hash
Indexes a Hash or anything that responds to to_hash as a document in the index
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/elastic_adapter/index.rb', line 98 def index(document) doc = document.to_hash.merge({}) params = { index: name, id: doc.delete(:id), type: document_type.name, body: doc } client.index(params) end |
#search(query) ⇒ Hash
Searches the index for documents matching the passed query.
135 136 137 138 139 140 |
# File 'lib/elastic_adapter/index.rb', line 135 def search(query) client.search( index: name, body: query ) end |
#suggest(query) ⇒ Hash
Searches the index for suggestions for the passed suggest query
152 153 154 155 156 157 |
# File 'lib/elastic_adapter/index.rb', line 152 def suggest(query) client.suggest( index: name, body: query ) end |
#validate(query) ⇒ Hash
Validates the passed query
165 166 167 168 169 170 171 |
# File 'lib/elastic_adapter/index.rb', line 165 def validate(query) client.indices.validate_query( index: name, explain: true, body: query ) end |