[Elasticsearch] Implement index_name

This commit is contained in:
Earlopain 2023-09-16 15:39:49 +02:00
parent c65288d2cd
commit ddcb7c1af1
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
5 changed files with 18 additions and 14 deletions

View File

@ -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

View File

@ -207,7 +207,7 @@ module PostIndex
end
document_store_client.bulk({
index: index_name,
index: document_store_index_name,
body: batch,
})
end

View File

@ -63,7 +63,7 @@ module PostVersionIndex
end
document_store_client.bulk({
index: index_name,
index: document_store_index_name,
body: batch
})
end

View File

@ -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

View File

@ -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" } } }