From 855388b092edae5616fe1b052e5b12d83c4b3f66 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 14 Sep 2023 20:56:56 +0200 Subject: [PATCH] [Elasticsearch] Remove a few more easy usages of __elasticsearch__ --- app/logical/document_store.rb | 8 ++++++++ .../104_1_add_elasticsearch_replacement_index.rb | 2 +- db/fixes/104_2_add_elasticsearch_replacement_data.rb | 2 +- test/test_helper.rb | 4 ++-- test/unit/document_store_test.rb | 12 ++++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/logical/document_store.rb b/app/logical/document_store.rb index 2f7139b1e..41667c5ac 100644 --- a/app/logical/document_store.rb +++ b/app/logical/document_store.rb @@ -25,6 +25,14 @@ module DocumentStore document_store_client.indices.exists(index: __elasticsearch__.index_name) end + def document_store_refresh_index! + document_store_client.indices.refresh(index: __elasticsearch__.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) + end + def document_store_client DocumentStore.client end diff --git a/db/fixes/104_1_add_elasticsearch_replacement_index.rb b/db/fixes/104_1_add_elasticsearch_replacement_index.rb index 5b06383ad..a4687743a 100755 --- a/db/fixes/104_1_add_elasticsearch_replacement_index.rb +++ b/db/fixes/104_1_add_elasticsearch_replacement_index.rb @@ -2,5 +2,5 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) -client = Post.__elasticsearch__.client +client = Post.document_store_client client.indices.put_mapping index: Post.index_name, body: { properties: { has_pending_replacements: { type: "boolean" } } } diff --git a/db/fixes/104_2_add_elasticsearch_replacement_data.rb b/db/fixes/104_2_add_elasticsearch_replacement_data.rb index 0c2ab0f61..adb1d7ffb 100755 --- a/db/fixes/104_2_add_elasticsearch_replacement_data.rb +++ b/db/fixes/104_2_add_elasticsearch_replacement_data.rb @@ -4,5 +4,5 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', Post.find_each do |post| puts post.id - post.__elasticsearch__.update_document_attributes has_pending_replacements: post.replacements.pending.any? + post.document_store_client.update_document_attributes has_pending_replacements: post.replacements.pending.any? end diff --git a/test/test_helper.rb b/test/test_helper.rb index 15f05b51f..93bcb8543 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -86,8 +86,8 @@ class ActiveSupport::TestCase def reset_post_index # This seems slightly faster than deleting and recreating the index - Post.__elasticsearch__.client.delete_by_query(index: Post.index_name, q: "*", body: {}) - Post.__elasticsearch__.refresh_index! + Post.document_store_delete_by_query(query: "*", body: {}) + Post.document_store_refresh_index! end end diff --git a/test/unit/document_store_test.rb b/test/unit/document_store_test.rb index 27d8448a5..76ceddd20 100644 --- a/test/unit/document_store_test.rb +++ b/test/unit/document_store_test.rb @@ -54,6 +54,18 @@ class DocumentStoreTest < ActiveSupport::TestCase assert_requested(put_request) end + test "it deletes by query" do + post_request = stub_elastic(:post, "/posts_test/_delete_by_query?q=*").with(body: "{}") + Post.document_store_delete_by_query(query: "*", body: {}) + assert_requested(post_request) + end + + test "it refreshes the index" do + post_request = stub_elastic(:post, "/posts_test/_refresh") + Post.document_store_refresh_index! + assert_requested(post_request) + end + test "models share the same client" do assert_equal(Post.document_store_client.object_id, PostVersion.document_store_client.object_id) end