From 06b44bce2641f1fc9f6d0bed9da91323a01b83ea Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Tue, 18 Feb 2020 10:43:47 -0500 Subject: [PATCH] Refactor Common Search Methods into a Base Class (#6133) [deploy] --- app/services/search/base.rb | 47 +++++++++++++++++++ app/services/search/classified_listing.rb | 38 +-------------- app/services/search/tag.rb | 44 +---------------- spec/models/tag_spec.rb | 2 +- .../search/classified_listing_spec.rb | 2 +- spec/services/search/tag_spec.rb | 2 +- 6 files changed, 53 insertions(+), 82 deletions(-) create mode 100644 app/services/search/base.rb diff --git a/app/services/search/base.rb b/app/services/search/base.rb new file mode 100644 index 000000000..74e5227b9 --- /dev/null +++ b/app/services/search/base.rb @@ -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 diff --git a/app/services/search/classified_listing.rb b/app/services/search/classified_listing.rb index 30c93f915..2b2fa9ff2 100644 --- a/app/services/search/classified_listing.rb +++ b/app/services/search/classified_listing.rb @@ -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 diff --git a/app/services/search/tag.rb b/app/services/search/tag.rb index a8239fe6c..a5b1bbe3d 100644 --- a/app/services/search/tag.rb +++ b/app/services/search/tag.rb @@ -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 diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 454ab14a3..17ca4ce6c 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -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 diff --git a/spec/services/search/classified_listing_spec.rb b/spec/services/search/classified_listing_spec.rb index 2792883ab..e5f559eb4 100644 --- a/spec/services/search/classified_listing_spec.rb +++ b/spec/services/search/classified_listing_spec.rb @@ -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) diff --git a/spec/services/search/tag_spec.rb b/spec/services/search/tag_spec.rb index 46194ee51..8f3cd023a 100644 --- a/spec/services/search/tag_spec.rb +++ b/spec/services/search/tag_spec.rb @@ -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)