From ddcb7c1af16755cfe953e27eb6eefa334414f7b1 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sat, 16 Sep 2023 15:39:49 +0200 Subject: [PATCH] [Elasticsearch] Implement index_name --- app/indexes/indexable.rb | 2 -- app/indexes/post_index.rb | 2 +- app/indexes/post_version_index.rb | 2 +- app/logical/document_store/model.rb | 24 ++++++++++++------- ...4_1_add_elasticsearch_replacement_index.rb | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/indexes/indexable.rb b/app/indexes/indexable.rb index a78d0abbf..f28a15f93 100644 --- a/app/indexes/indexable.rb +++ b/app/indexes/indexable.rb @@ -8,7 +8,5 @@ module Indexable def self.included(base) base.include Elasticsearch::Model base.include DocumentStore::Model - - base.index_name("#{base.model_name.plural}_#{Rails.env}") end end diff --git a/app/indexes/post_index.rb b/app/indexes/post_index.rb index 56b77f856..6455456bd 100644 --- a/app/indexes/post_index.rb +++ b/app/indexes/post_index.rb @@ -207,7 +207,7 @@ module PostIndex end document_store_client.bulk({ - index: index_name, + index: document_store_index_name, body: batch, }) end diff --git a/app/indexes/post_version_index.rb b/app/indexes/post_version_index.rb index 9e526ea94..933f8e0f3 100644 --- a/app/indexes/post_version_index.rb +++ b/app/indexes/post_version_index.rb @@ -63,7 +63,7 @@ module PostVersionIndex end document_store_client.bulk({ - index: index_name, + index: document_store_index_name, body: batch }) end diff --git a/app/logical/document_store/model.rb b/app/logical/document_store/model.rb index 200c310d6..ea17a0254 100644 --- a/app/logical/document_store/model.rb +++ b/app/logical/document_store/model.rb @@ -3,6 +3,8 @@ module DocumentStore def self.included(klass) klass.extend(ClassMethods) + klass.document_store_index_name = "#{klass.model_name.plural}_#{Rails.env}" + klass.after_commit on: %i[create update] do update_index end @@ -20,11 +22,15 @@ module DocumentStore end def document_store_update_index(refresh: "false") - document_store_client.index(index: __elasticsearch__.index_name, id: id, body: as_indexed_json, refresh: refresh) + document_store_client.index(index: document_store_index_name, id: id, body: as_indexed_json, refresh: refresh) end def document_store_delete_document(refresh: "false") - document_store_client.delete(index: __elasticsearch__.index_name, id: id, refresh: refresh) + document_store_client.delete(index: document_store_index_name, id: id, refresh: refresh) + end + + def document_store_index_name + self.class.document_store_index_name end def document_store_client @@ -32,10 +38,10 @@ module DocumentStore end module ClassMethods - attr_accessor :document_store_index + attr_accessor :document_store_index, :document_store_index_name def document_store_search(body) - search = SearchRequest.new({ index: __elasticsearch__.index_name, body: body }, document_store_client) + search = SearchRequest.new({ index: document_store_index_name, body: body }, document_store_client) Response.new(self, search) end @@ -45,23 +51,23 @@ module DocumentStore document_store_delete_index! if exists && delete_existing - document_store_client.indices.create(index: __elasticsearch__.index_name, body: document_store_index) + document_store_client.indices.create(index: document_store_index_name, body: document_store_index) end def document_store_delete_index! - document_store_client.indices.delete(index: __elasticsearch__.index_name, ignore: 404) + document_store_client.indices.delete(index: document_store_index_name, ignore: 404) end def document_store_index_exist? - document_store_client.indices.exists(index: __elasticsearch__.index_name) + document_store_client.indices.exists(index: document_store_index_name) end def document_store_refresh_index! - document_store_client.indices.refresh(index: __elasticsearch__.index_name) + document_store_client.indices.refresh(index: document_store_index_name) end def document_store_delete_by_query(query:, body:) - document_store_client.delete_by_query(index: __elasticsearch__.index_name, q: query, body: body) + document_store_client.delete_by_query(index: document_store_index_name, q: query, body: body) end def document_store_client diff --git a/db/fixes/104_1_add_elasticsearch_replacement_index.rb b/db/fixes/104_1_add_elasticsearch_replacement_index.rb index a4687743a..d4aca9667 100755 --- a/db/fixes/104_1_add_elasticsearch_replacement_index.rb +++ b/db/fixes/104_1_add_elasticsearch_replacement_index.rb @@ -3,4 +3,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) client = Post.document_store_client -client.indices.put_mapping index: Post.index_name, body: { properties: { has_pending_replacements: { type: "boolean" } } } +client.indices.put_mapping index: Post.document_store_index_name, body: { properties: { has_pending_replacements: { type: "boolean" } } }