Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract #search_index_for method, it is being used more broadly

Added additional specs
  • Loading branch information...
commit 9d6b3547397cff8e1435b52dd93bdec7ecf83303 1 parent 4cfbbcb
Saulius Grigaliunas saulius authored
8 lib/trade_tariff_backend.rb
View
@@ -108,6 +108,14 @@ def search_namespace
end
attr_writer :search_namespace
+ # Returns search index instance for given model instance or
+ # model class instance
+ def search_index_for(model)
+ index_name = model.is_a?(Class) ? model : model.class
+
+ "#{index_name}Index".constantize.new(search_namespace)
+ end
+
def search_port
@search_port ||= 9200
end
10 lib/trade_tariff_backend/search_client.rb
View
@@ -7,13 +7,13 @@ class SearchClient < SimpleDelegator
QueryError = Class.new(StandardError)
attr_reader :indexed_models
- attr_reader :namespace
attr_reader :index_page_size
attr_reader :search_operation_options
+ delegate :search_index_for, to: TradeTariffBackend
+
def initialize(search_client, options = {})
@indexed_models = options.fetch(:indexed_models, [])
- @namespace = options.fetch(:namespace, '')
@index_page_size = options.fetch(:index_page_size, 1000)
@search_operation_options = options.fetch(:search_operation_options, {})
@@ -48,7 +48,7 @@ def drop_index(index)
def build_index(index, model)
model.dataset.each_page(index_page_size) do |entries|
- bulk(body: serialize_for(:index, index, entries))
+ bulk({ body: serialize_for(:index, index, entries) }.merge(search_operation_options))
end
end
@@ -75,10 +75,6 @@ def delete(model)
private
- def search_index_for(model)
- "#{model}Index".constantize.new(namespace)
- end
-
def serialize_for(operation, index, entries)
entries.each_with_object([]) do |model, memo|
memo.push(
46 spec/integration/trade_tariff_backend/search_client_spec.rb
View
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe TradeTariffBackend::SearchClient do
+ describe '#search' do
+ let(:commodity) {
+ create :commodity, :with_description, description: 'test description'
+ }
+
+ let(:search_result) {
+ TradeTariffBackend.search_client.search q: 'test', index: TradeTariffBackend.search_index_for(commodity).name
+ }
+
+ it 'searches in supplied index' do
+ expect(search_result.hits.total).to be >= 1
+ expect(search_result.hits.hits.first._source.goods_nomenclature_item_id).to eq commodity.goods_nomenclature_item_id
+ end
+
+ it 'returns results wrapped in Hashie::Mash structure' do
+ expect(search_result).to be_kind_of Hashie::Mash
+ end
+ end
+
+ describe '#build_index' do
+ let(:commodity) {
+ create :commodity, :with_description, description: 'test description'
+ }
+
+ before {
+ # Make sure index is fresh
+ TradeTariffBackend.search_client.drop_index(TradeTariffBackend.search_index_for(commodity))
+ TradeTariffBackend.search_client.create_index(TradeTariffBackend.search_index_for(commodity))
+ }
+
+ it 'bulk indexes all model entries' do
+ TradeTariffBackend.search_client.build_index(
+ TradeTariffBackend.search_index_for(commodity),
+ commodity.class
+ )
+
+ search_result = TradeTariffBackend.search_client.search q: 'test', index: TradeTariffBackend.search_index_for(commodity).name
+
+ expect(search_result.hits.total).to be >= 1
+ expect(search_result.hits.hits.first._source.goods_nomenclature_item_id).to eq commodity.goods_nomenclature_item_id
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.