Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SRCH-1754 update Elasticsearch initializer & client #128

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/classes/document_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def search
def execute_client_search
params = { index: indices, body: doc_query.body, from: offset, size: size }
Rails.logger.debug "Query: *****\n#{doc_query.body.to_json}\n*****"
result = Elasticsearch::Persistence.client.search(params)
result = ES.client.search(params)
DocumentSearchResults.new(result, offset)
end
end
10 changes: 7 additions & 3 deletions app/controllers/api/v1/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ def auth?(admin_user, admin_password)
error!(collection.errors.messages, 400) unless collection.valid?
es_documents_index_name = [Document.index_namespace(handle), 'v1'].join('-')
Document.create_index!(index: es_documents_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_documents_index_name,
name: Document.index_namespace(handle)
ES.client.indices.put_alias(
index: es_documents_index_name,
name: Document.index_namespace(handle)
)
ok("Your collection was successfully created.")
end

Expand All @@ -60,7 +62,9 @@ def auth?(admin_user, admin_password)
handle = params.delete(:handle)
collection = Collection.find(handle)
error!(collection.errors.messages, 400) unless collection.destroy
Elasticsearch::Persistence.client.indices.delete(index: [Document.index_namespace(handle), '*'].join('-'))
ES.client.indices.delete(
index: [Document.index_namespace(handle), '*'].join('-')
)
ok("Your collection was successfully deleted.")
end

Expand Down
26 changes: 17 additions & 9 deletions config/initializers/elasticsearch.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
yaml = YAML.load_file("#{Rails.root}/config/elasticsearch.yml").presence
# frozen_string_literal: true

Elasticsearch::Persistence.client = Elasticsearch::Client.new(log: Rails.env.development?,
hosts: yaml['hosts'],
user: yaml['user'],
password: yaml['password'],
randomize_hosts: true,
retry_on_failure: true,
reload_connections: true)
module ES
CONFIG = YAML.load_file("#{Rails.root}/config/elasticsearch.yml").presence.freeze

def self.client
Elasticsearch::Client.new(log: Rails.env.development?,
hosts: CONFIG['hosts'],
user: CONFIG['user'],
password: CONFIG['password'],
randomize_hosts: true,
retry_on_failure: true,
reload_connections: true)
end
end

if Rails.env.development?
logger = ActiveSupport::Logger.new(STDERR)
logger.level = Logger::DEBUG
logger.formatter = proc { |_s, _d, _p, m| "\e[2m#{m}\n\e[0m" }
Elasticsearch::Persistence.client.transport.logger = logger
ES.client.transport.logger = logger
end

Elasticsearch::Persistence.client = ES.client
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the assignment to Elasticsearch::Persistence.client necessary? Backwards compatibility for some code that this PR doesn't change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's for backwards compatibility. It sets the client for the persisted classes (Document and Collection), replicating the logic of the existing client assignment, but using the new ES.client method. That line is going to go away in a subsequent commit.

31 changes: 17 additions & 14 deletions lib/tasks/i14y.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ namespace :i14y do
entity_name = File.basename(template_generator, '.rb')
klass = entity_name.camelize.constantize
template_generator = klass.new
Elasticsearch::Persistence.client.indices.put_template(name: entity_name,
body: template_generator.body,
order: 0,
create: true)
ES.client.indices.put_template(name: entity_name,
body: template_generator.body,
order: 0,
create: true)
end
es_collections_index_name = [Collection.index_namespace, 'v1'].join('-')
Collection.create_index!(index: es_collections_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_collections_index_name, name: Collection.index_name
ES.client.indices.put_alias(
index: es_collections_index_name,
name: Collection.index_name
)
end

desc "Copies data from one version of the i14y index to the next (e.g., collections, documents) and updates the alias"
Expand All @@ -21,12 +24,12 @@ namespace :i14y do
persistence_model_klass = entity_name.singularize.camelize.constantize
klass = entity_name.camelize.constantize
template_generator = klass.new
Elasticsearch::Persistence.client.indices.put_template(name: entity_name,
body: template_generator.body,
order: 0)
ES.client.indices.put_template(name: entity_name,
body: template_generator.body,
order: 0)

wildcard = [persistence_model_klass.index_namespace, '*'].join
aliases = Elasticsearch::Persistence.client.indices.get_alias(name: wildcard)
aliases = ES.client.indices.get_alias(name: wildcard)
aliases.each do |old_es_index_name, alias_names|
alias_name = alias_names['aliases'].keys.first
persistence_model_klass.index_name = old_es_index_name
Expand All @@ -35,25 +38,25 @@ namespace :i14y do
persistence_model_klass.create_index!(index: new_es_index_name)
persistence_model_klass.index_name = new_es_index_name
since_timestamp = Time.now
host_hash = Elasticsearch::Persistence.client.transport.hosts.first
host_hash = ES.client.transport.hosts.first
base_url = "#{host_hash[:protocol]}://#{host_hash[:host]}:#{host_hash[:port]}/"
old_es_index_url = base_url + old_es_index_name
new_es_index_url = base_url + new_es_index_name
stream2es(old_es_index_url, new_es_index_url)
move_alias(alias_name, old_es_index_name, new_es_index_name)
stream2es(old_es_index_url, new_es_index_url, since_timestamp)
puts "New #{new_es_index_name} index now contains #{persistence_model_klass.count} #{entity_name}"
Elasticsearch::Persistence.client.indices.delete(index: old_es_index_name)
ES.client.indices.delete(index: old_es_index_name)
end
end

desc "Deletes templates, indexes, and reader/writer aliases for all i14y models. Useful for development."
task clear_all: :environment do
Dir[Rails.root.join('app', 'templates', '*.rb')].each do |template_generator|
entity_name = File.basename(template_generator, '.rb')
Elasticsearch::Persistence.client.indices.delete_template(name: entity_name) rescue Elasticsearch::Transport::Transport::Errors::NotFound
ES.client.indices.delete_template(name: entity_name) rescue Elasticsearch::Transport::Transport::Errors::NotFound
end
Elasticsearch::Persistence.client.indices.delete(index: [Rails.env, I14y::APP_NAME, '*'].join('-'))
ES.client.indices.delete(index: [Rails.env, I14y::APP_NAME, '*'].join('-'))
end

def next_version(index_name)
Expand All @@ -77,7 +80,7 @@ namespace :i14y do
{ remove: { index: old_index_name, alias: alias_name } },
{ add: { index: new_index_name, alias: alias_name } }
] } }
Elasticsearch::Persistence.client.indices.update_aliases(update_aliases_hash)
ES.client.indices.update_aliases(update_aliases_hash)
end

end
18 changes: 12 additions & 6 deletions spec/classes/document_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
let(:document_search_results) { document_search.search }

before do
Elasticsearch::Persistence.client.indices.delete(index: [Document.index_namespace('agency_blogs'), '*'].join('-'))
ES.client.indices.delete(
index: [Document.index_namespace('agency_blogs'), '*'].join('-')
)
es_documents_index_name = [Document.index_namespace('agency_blogs'), 'v1'].join('-')
#Using a single shard prevents intermittent relevancy issues in tests
#https://www.elastic.co/guide/en/elasticsearch/guide/current/relevance-is-broken.html
Document.settings(index: { number_of_shards: 1 })
Document.create_index!(index: es_documents_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_documents_index_name,
name: Document.index_namespace('agency_blogs')
ES.client.indices.put_alias(
index: es_documents_index_name,
name: Document.index_namespace('agency_blogs')
)
Document.index_name = Document.index_namespace('agency_blogs')
end

Expand Down Expand Up @@ -97,7 +101,7 @@
let(:query) { 'uh oh' }
let(:error) { StandardError.new('something went wrong') }

before { allow(Elasticsearch::Persistence.client).to receive(:search).and_raise(error) }
before { allow(ES).to receive(:client).and_raise(error) }

it 'returns a no results response' do
expect(document_search_results.total).to eq(0)
Expand Down Expand Up @@ -152,8 +156,10 @@
Document.refresh_index!
es_documents_index_name = [Document.index_namespace('other_agency_blogs'), 'v1'].join('-')
Document.create_index!(index: es_documents_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_documents_index_name,
name: Document.index_namespace('other_agency_blogs')
ES.client.indices.put_alias(
index: es_documents_index_name,
name: Document.index_namespace('other_agency_blogs')
)
Document.index_name = Document.index_namespace('other_agency_blogs')
Document.create(language: 'en', title: 'other title 1 common content', description: 'other description 1 common content', created: DateTime.now, path: 'http://www.otheragency.gov/page1.html')
Document.refresh_index!
Expand Down
10 changes: 6 additions & 4 deletions spec/models/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@

before(:all) do
handle = 'test_index'
Elasticsearch::Persistence.client.indices.delete(
ES.client.indices.delete(
index: [Document.index_namespace(handle), '*'].join('-')
)
es_documents_index_name = [Document.index_namespace(handle), 'v1'].join('-')
Document.create_index!(index: es_documents_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_documents_index_name,
name: Document.index_namespace(handle)
ES.client.indices.put_alias(
index: es_documents_index_name,
name: Document.index_namespace(handle)
)
Document.index_name = Document.index_namespace(handle)
end

after(:all) do
Elasticsearch::Persistence.client.indices.delete(
ES.client.indices.delete(
index: [Document.index_namespace('test_index'), '*'].join('-')
)
end
Expand Down
7 changes: 5 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'test_services'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand All @@ -28,11 +31,11 @@
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.include DocumentCrud
config.include TestServices

config.infer_spec_type_from_file_location!

config.before(:suite) do
require 'test_services'
TestServices::delete_es_indexes
TestServices::create_es_indexes
end
Expand All @@ -54,6 +57,6 @@
end

config.after :each, elasticsearch: true do
Elasticsearch::Persistence.client.indices.delete index: '*documents*'
ES.client.indices.delete index: '*documents*'
end
end
22 changes: 9 additions & 13 deletions spec/requests/api/v1/collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
describe 'POST /api/v1/collections' do
context 'success case' do
before do
Elasticsearch::Persistence.client.delete_by_query(
index: Collection.index_name,
q: '*:*',
conflicts: 'proceed'
)
clear_index(collections_index_name)
post '/api/v1/collections', params: valid_params, headers: valid_session
end

Expand Down Expand Up @@ -125,7 +121,7 @@
describe 'DELETE /api/v1/collections/{handle}' do
context 'success case' do
before do
Elasticsearch::Persistence.client.delete_by_query index: Collection.index_name, q: '*:*', conflicts: 'proceed'
clear_index(collections_index_name)
Collection.create(_id: 'agency_blogs', token: 'secret')
delete '/api/v1/collections/agency_blogs', headers: valid_session
end
Expand All @@ -150,10 +146,10 @@
describe 'GET /api/v1/collections/{handle}' do
context 'success case' do
before do
Elasticsearch::Persistence.client.delete_by_query index: Collection.index_name, q: '*:*', conflicts: 'proceed'
clear_index(collections_index_name)
post '/api/v1/collections', params: valid_params, headers: valid_session
Document.index_name = Document.index_namespace('agency_blogs')
Elasticsearch::Persistence.client.delete_by_query index: Document.index_name, q: '*:*', conflicts: 'proceed'
clear_index(Document.index_name)
end

let(:datetime) { DateTime.now.utc }
Expand Down Expand Up @@ -202,10 +198,10 @@
describe 'GET /api/v1/collections/search' do
context 'success case' do
before do
Elasticsearch::Persistence.client.delete_by_query index: Collection.index_name, q: '*:*', conflicts: 'proceed'
clear_index(collections_index_name)
post '/api/v1/collections', params: valid_params, headers: valid_session
Document.index_name = Document.index_namespace('agency_blogs')
Elasticsearch::Persistence.client.delete_by_query index: Document.index_name, q: '*:*', conflicts: 'proceed'
clear_index(Document.index_name)
end

let(:datetime) { DateTime.now.utc.to_s }
Expand Down Expand Up @@ -300,10 +296,10 @@

context 'no results' do
before do
Elasticsearch::Persistence.client.delete_by_query index: Collection.index_name, q: '*:*', conflicts: 'proceed'
clear_index(collections_index_name)
post '/api/v1/collections', params: valid_params, headers: valid_session
Document.index_name = Document.index_namespace('agency_blogs')
Elasticsearch::Persistence.client.delete_by_query index: Document.index_name, q: '*:*', conflicts: 'proceed'
clear_index(Document.index_name)
end

it 'returns JSON no hits results' do
Expand Down Expand Up @@ -343,7 +339,7 @@
end

before do
Elasticsearch::Persistence.client.delete_by_query index: Collection.index_name, q: '*:*', conflicts: 'proceed'
clear_index(collections_index_name)
Collection.create(_id: 'agency_blogs', token: 'secret')
get '/api/v1/collections/search', params: bad_handle_params, headers: valid_session
end
Expand Down
24 changes: 20 additions & 4 deletions spec/test_services.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# frozen_string_literal: true

module TestServices
module_function

def create_es_indexes
es_collections_index_name = [Rails.env, I14y::APP_NAME, 'collections', 'v1'].join('-')
Collection.create_index!(index: es_collections_index_name)
Elasticsearch::Persistence.client.indices.put_alias index: es_collections_index_name, name: Collection.index_name
Collection.create_index!(index: collections_index_name)
ES.client.indices.put_alias(
index: collections_index_name,
name: Collection.index_name
)
end

def delete_es_indexes
Elasticsearch::Persistence.client.indices.delete(index: [Rails.env, I14y::APP_NAME, '*'].join('-'))
ES.client.indices.delete(index: [Rails.env, I14y::APP_NAME, '*'].join('-'))
end

def clear_index(index_name)
ES.client.delete_by_query(
index: index_name,
q: '*:*',
conflicts: 'proceed'
)
end

def collections_index_name
[Rails.env, I14y::APP_NAME, 'collections', 'v1'].join('-')
end
end