forked from e621ng/e621ng
[Elasticsearch] Start implementing own elasticsearch-model
This implements create_index!
This commit is contained in:
parent
17b9797657
commit
a79e25dfbd
@ -7,6 +7,7 @@
|
||||
module Indexable
|
||||
def self.included(base)
|
||||
base.include Elasticsearch::Model
|
||||
base.include DocumentStore::Model
|
||||
|
||||
base.index_name("#{base.model_name.plural}_#{Rails.env}")
|
||||
|
||||
|
37
app/logical/document_store.rb
Normal file
37
app/logical/document_store.rb
Normal file
@ -0,0 +1,37 @@
|
||||
module DocumentStore
|
||||
module Model
|
||||
def self.included(klass)
|
||||
klass.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def document_store_create_index!(delete_existing: false)
|
||||
exists = document_store_index_exist?
|
||||
return if exists && !delete_existing
|
||||
|
||||
document_store_delete_index! if exists && delete_existing
|
||||
|
||||
document_store_client.indices.create(index: __elasticsearch__.index_name, body: {
|
||||
settings: __elasticsearch__.settings.to_hash,
|
||||
mappings: __elasticsearch__.mappings.to_hash,
|
||||
})
|
||||
end
|
||||
|
||||
def document_store_delete_index!
|
||||
document_store_client.indices.delete(index: __elasticsearch__.index_name, ignore: 404)
|
||||
end
|
||||
|
||||
def document_store_index_exist?
|
||||
document_store_client.indices.exists(index: __elasticsearch__.index_name)
|
||||
end
|
||||
|
||||
def document_store_client
|
||||
DocumentStore.client
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.client
|
||||
@client ||= Elasticsearch::Client.new(host: Danbooru.config.elasticsearch_host)
|
||||
end
|
||||
end
|
@ -19,7 +19,7 @@ FileUtils.chdir APP_ROOT do
|
||||
end
|
||||
|
||||
puts "== Creating elasticsearch indices ==\n"
|
||||
system! "RAILS_ENV=development bin/rails runner '[Post, PostVersion].each { |model| model.__elasticsearch__.create_index! }'"
|
||||
system! "RAILS_ENV=development bin/rails runner '[Post, PostVersion].each { |model| model.document_store_create_index! }'"
|
||||
|
||||
puts "\n== Preparing database =="
|
||||
# Create the test database, since only development exists at this point
|
||||
|
@ -38,8 +38,8 @@ BCrypt::Engine.send(:remove_const, :DEFAULT_COST)
|
||||
BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
|
||||
|
||||
# Clear the elastic indicies completly
|
||||
Post.__elasticsearch__.create_index!(force: true)
|
||||
PostVersion.__elasticsearch__.create_index!(force: true)
|
||||
Post.document_store_create_index!(delete_existing: true)
|
||||
PostVersion.document_store_create_index!(delete_existing: true)
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
include ActionDispatch::TestProcess::FixtureFile
|
||||
|
60
test/unit/document_store_test.rb
Normal file
60
test/unit/document_store_test.rb
Normal file
@ -0,0 +1,60 @@
|
||||
require "test_helper"
|
||||
|
||||
class DocumentStoreTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
WebMock.disable_net_connect!
|
||||
WebMock.reset_executed_requests!
|
||||
end
|
||||
|
||||
teardown do
|
||||
WebMock.disable_net_connect!(allow: [Danbooru.config.elasticsearch_host])
|
||||
end
|
||||
|
||||
def stub_elastic(method, path)
|
||||
stub_request(method, "http://#{Danbooru.config.elasticsearch_host}:9200#{path}")
|
||||
end
|
||||
|
||||
test "it deletes the index" do
|
||||
delete_request = stub_elastic(:delete, "/posts_test")
|
||||
Post.document_store_delete_index!
|
||||
assert_requested delete_request
|
||||
end
|
||||
|
||||
test "it checks for the existance of the index" do
|
||||
head_request = stub_elastic(:head, "/posts_test")
|
||||
Post.document_store_index_exist?
|
||||
assert_requested head_request
|
||||
end
|
||||
|
||||
test "it skips creating the index if it already exists" do
|
||||
head_request = stub_elastic(:head, "/posts_test").to_return(status: 200)
|
||||
Post.document_store_create_index!
|
||||
assert_requested head_request
|
||||
end
|
||||
|
||||
test "it creates the index if it doesn't exist" do
|
||||
head_request = stub_elastic(:head, "/posts_test").to_return(status: 404)
|
||||
put_request = stub_elastic(:put, "/posts_test")
|
||||
|
||||
Post.document_store_create_index!
|
||||
|
||||
assert_requested(head_request)
|
||||
assert_requested(put_request)
|
||||
end
|
||||
|
||||
test "it recreates the index if delete_existing is true and the index already exists" do
|
||||
head_request = stub_elastic(:head, "/posts_test").to_return(status: 200)
|
||||
delete_request = stub_elastic(:delete, "/posts_test")
|
||||
put_request = stub_elastic(:put, "/posts_test")
|
||||
|
||||
Post.document_store_create_index!(delete_existing: true)
|
||||
|
||||
assert_requested(head_request)
|
||||
assert_requested(delete_request)
|
||||
assert_requested(put_request)
|
||||
end
|
||||
|
||||
test "models share the same client" do
|
||||
assert_equal(Post.document_store_client.object_id, PostVersion.document_store_client.object_id)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user