Refactor Common Search Methods into a Base Class (#6133) [deploy]
This commit is contained in:
parent
98f25e2b9d
commit
06b44bce26
6 changed files with 53 additions and 82 deletions
47
app/services/search/base.rb
Normal file
47
app/services/search/base.rb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
module Search
|
||||
class Base
|
||||
class << self
|
||||
def index(doc_id, serialized_data)
|
||||
SearchClient.index(
|
||||
id: doc_id,
|
||||
index: self::INDEX_ALIAS,
|
||||
body: serialized_data,
|
||||
)
|
||||
end
|
||||
|
||||
def find_document(doc_id)
|
||||
SearchClient.get(id: doc_id, index: self::INDEX_ALIAS)
|
||||
end
|
||||
|
||||
def create_index(index_name: self::INDEX_NAME)
|
||||
SearchClient.indices.create(index: index_name, body: settings)
|
||||
end
|
||||
|
||||
def delete_index(index_name: self::INDEX_NAME)
|
||||
SearchClient.indices.delete(index: index_name)
|
||||
end
|
||||
|
||||
def refresh_index(index_name: self::INDEX_ALIAS)
|
||||
SearchClient.indices.refresh(index: index_name)
|
||||
end
|
||||
|
||||
def add_alias(index_name: self::INDEX_NAME, index_alias: self::INDEX_ALIAS)
|
||||
SearchClient.indices.put_alias(index: index_name, name: index_alias)
|
||||
end
|
||||
|
||||
def update_mappings(index_alias: self::INDEX_ALIAS)
|
||||
SearchClient.indices.put_mapping(index: index_alias, body: self::MAPPINGS)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def settings
|
||||
{ settings: { index: index_settings } }
|
||||
end
|
||||
|
||||
def index_settings
|
||||
raise "Search classes must implement their own index settings"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,44 +1,12 @@
|
|||
module Search
|
||||
class ClassifiedListing
|
||||
class ClassifiedListing < Base
|
||||
INDEX_NAME = "classified_listings_#{Rails.env}".freeze
|
||||
INDEX_ALIAS = "classified_listings_#{Rails.env}_alias".freeze
|
||||
MAPPINGS = JSON.parse(File.read("config/elasticsearch/mappings/classified_listings.json"), symbolize_names: true).freeze
|
||||
|
||||
class << self
|
||||
def index(classified_listing_id, serialized_data)
|
||||
SearchClient.index(
|
||||
id: classified_listing_id,
|
||||
index: INDEX_ALIAS,
|
||||
body: serialized_data,
|
||||
)
|
||||
end
|
||||
|
||||
def find_document(classified_listing_id)
|
||||
SearchClient.get(id: classified_listing_id, index: INDEX_ALIAS)
|
||||
end
|
||||
|
||||
def create_index(index_name: INDEX_NAME)
|
||||
SearchClient.indices.create(index: index_name, body: settings)
|
||||
end
|
||||
|
||||
def delete_index(index_name: INDEX_NAME)
|
||||
SearchClient.indices.delete(index: index_name)
|
||||
end
|
||||
|
||||
def add_alias(index_name: INDEX_NAME, index_alias: INDEX_ALIAS)
|
||||
SearchClient.indices.put_alias(index: index_name, name: index_alias)
|
||||
end
|
||||
|
||||
def update_mappings(index_alias: INDEX_ALIAS)
|
||||
SearchClient.indices.put_mapping(index: index_alias, body: mappings)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def settings
|
||||
{ settings: { index: index_settings } }
|
||||
end
|
||||
|
||||
def index_settings
|
||||
if Rails.env.production?
|
||||
{
|
||||
|
|
@ -52,10 +20,6 @@ module Search
|
|||
}
|
||||
end
|
||||
end
|
||||
|
||||
def mappings
|
||||
MAPPINGS
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,22 +1,10 @@
|
|||
module Search
|
||||
class Tag
|
||||
class Tag < Base
|
||||
INDEX_NAME = "tags_#{Rails.env}".freeze
|
||||
INDEX_ALIAS = "tags_#{Rails.env}_alias".freeze
|
||||
MAPPING = JSON.parse(File.read("config/elasticsearch/mappings/tags.json"), symbolize_names: true).freeze
|
||||
MAPPINGS = JSON.parse(File.read("config/elasticsearch/mappings/tags.json"), symbolize_names: true).freeze
|
||||
|
||||
class << self
|
||||
def index(tag_id, serialized_data)
|
||||
SearchClient.index(
|
||||
id: tag_id,
|
||||
index: INDEX_ALIAS,
|
||||
body: serialized_data,
|
||||
)
|
||||
end
|
||||
|
||||
def find_document(tag_id)
|
||||
SearchClient.get(id: tag_id, index: INDEX_ALIAS)
|
||||
end
|
||||
|
||||
def search(query_string)
|
||||
SearchClient.search(
|
||||
index: INDEX_ALIAS,
|
||||
|
|
@ -40,32 +28,8 @@ module Search
|
|||
results.dig("hits", "hits").map { |tag_doc| tag_doc.dig("_source") }
|
||||
end
|
||||
|
||||
def create_index(index_name: INDEX_NAME)
|
||||
SearchClient.indices.create(index: index_name, body: settings)
|
||||
end
|
||||
|
||||
def delete_index(index_name: INDEX_NAME)
|
||||
SearchClient.indices.delete(index: index_name)
|
||||
end
|
||||
|
||||
def refresh_index(index_name: INDEX_ALIAS)
|
||||
SearchClient.indices.refresh(index: index_name)
|
||||
end
|
||||
|
||||
def add_alias(index_name: INDEX_NAME, index_alias: INDEX_ALIAS)
|
||||
SearchClient.indices.put_alias(index: index_name, name: index_alias)
|
||||
end
|
||||
|
||||
def update_mappings(index_alias: INDEX_ALIAS)
|
||||
SearchClient.indices.put_mapping(index: index_alias, body: mappings)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def settings
|
||||
{ settings: { index: index_settings } }
|
||||
end
|
||||
|
||||
def index_settings
|
||||
if Rails.env.production?
|
||||
{
|
||||
|
|
@ -79,10 +43,6 @@ module Search
|
|||
}
|
||||
end
|
||||
end
|
||||
|
||||
def mappings
|
||||
MAPPING
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ RSpec.describe Tag, type: :model do
|
|||
|
||||
describe "#serialized_search_hash" do
|
||||
it "creates a valid serialized hash to send to elasticsearch" do
|
||||
mapping_keys = Search::Tag.send("mappings").dig(:properties).keys
|
||||
mapping_keys = Search::Tag::MAPPINGS.dig(:properties).keys
|
||||
expect(tag.serialized_search_hash.symbolize_keys.keys).to eq(mapping_keys)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ RSpec.describe Search::ClassifiedListing, type: :service, elasticsearch: true do
|
|||
|
||||
described_class.update_mappings(index_alias: other_name)
|
||||
mapping = SearchClient.indices.get_mapping(index: other_name).dig(other_name, "mappings")
|
||||
expect(mapping.deep_stringify_keys).to include(described_class.send("mappings").deep_stringify_keys)
|
||||
expect(mapping.deep_stringify_keys).to include(described_class::MAPPINGS.deep_stringify_keys)
|
||||
|
||||
# Have to cleanup index since it wont automatically be handled by our cluster class bc of the unexpected name
|
||||
described_class.delete_index(index_name: other_name)
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ RSpec.describe Search::Tag, type: :service, elasticsearch: true do
|
|||
|
||||
described_class.update_mappings(index_alias: other_name)
|
||||
mapping = SearchClient.indices.get_mapping(index: other_name).dig(other_name, "mappings")
|
||||
expect(mapping.deep_stringify_keys).to include(described_class.send("mappings").deep_stringify_keys)
|
||||
expect(mapping.deep_stringify_keys).to include(described_class::MAPPINGS.deep_stringify_keys)
|
||||
|
||||
# Have to cleanup index since it wont automatically be handled by our cluster class bc of the unexpected name
|
||||
described_class.delete_index(index_name: other_name)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue