[Elasticsearch] Remove a few more easy usages of __elasticsearch__

This commit is contained in:
Earlopain 2023-09-14 20:56:56 +02:00
parent a79e25dfbd
commit 855388b092
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
5 changed files with 24 additions and 4 deletions

View File

@ -25,6 +25,14 @@ module DocumentStore
document_store_client.indices.exists(index: __elasticsearch__.index_name) document_store_client.indices.exists(index: __elasticsearch__.index_name)
end 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 def document_store_client
DocumentStore.client DocumentStore.client
end end

View File

@ -2,5 +2,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) 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" } } } client.indices.put_mapping index: Post.index_name, body: { properties: { has_pending_replacements: { type: "boolean" } } }

View File

@ -4,5 +4,5 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config',
Post.find_each do |post| Post.find_each do |post|
puts post.id 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 end

View File

@ -86,8 +86,8 @@ class ActiveSupport::TestCase
def reset_post_index def reset_post_index
# This seems slightly faster than deleting and recreating the index # This seems slightly faster than deleting and recreating the index
Post.__elasticsearch__.client.delete_by_query(index: Post.index_name, q: "*", body: {}) Post.document_store_delete_by_query(query: "*", body: {})
Post.__elasticsearch__.refresh_index! Post.document_store_refresh_index!
end end
end end

View File

@ -54,6 +54,18 @@ class DocumentStoreTest < ActiveSupport::TestCase
assert_requested(put_request) assert_requested(put_request)
end 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 test "models share the same client" do
assert_equal(Post.document_store_client.object_id, PostVersion.document_store_client.object_id) assert_equal(Post.document_store_client.object_id, PostVersion.document_store_client.object_id)
end end