From 3ad4eb0cea5cd30995f3018aeb22e67170bf0e91 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Thu, 5 Feb 2015 14:28:19 -0600 Subject: [PATCH] Extract SearchBuilder from SolrHelper --- lib/blacklight.rb | 1 + lib/blacklight/catalog.rb | 2 +- lib/blacklight/request_builders.rb | 11 + lib/blacklight/search_builder.rb | 43 ++ lib/blacklight/solr_helper.rb | 64 +- spec/lib/blacklight/search_builder_spec.rb | 341 +++++++++ spec/lib/blacklight/solr_helper_spec.rb | 654 +++++------------- .../_paginate_compact.html.erb_spec.rb | 2 +- spec/views/catalog/index.atom.builder_spec.rb | 2 +- 9 files changed, 610 insertions(+), 510 deletions(-) create mode 100644 lib/blacklight/search_builder.rb create mode 100644 spec/lib/blacklight/search_builder_spec.rb diff --git a/lib/blacklight.rb b/lib/blacklight.rb index bbfa9d0418..1399811a49 100644 --- a/lib/blacklight.rb +++ b/lib/blacklight.rb @@ -6,6 +6,7 @@ module Blacklight autoload :Configurable, 'blacklight/configurable' autoload :Configuration, 'blacklight/configuration' autoload :SearchFields, 'blacklight/search_fields' + autoload :SearchBuilder, 'blacklight/search_builder' autoload :Solr, 'blacklight/solr' diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb index 6a1353bd87..9344cc0ad1 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -30,7 +30,7 @@ module Blacklight::Catalog # get search results from the solr index def index - (@response, @document_list) = search_results(params, {}, solr_search_params_logic) + (@response, @document_list) = search_results(params, solr_search_params_logic) respond_to do |format| format.html { preferred_view } diff --git a/lib/blacklight/request_builders.rb b/lib/blacklight/request_builders.rb index 9bee6a4027..dfc5d1d40a 100644 --- a/lib/blacklight/request_builders.rb +++ b/lib/blacklight/request_builders.rb @@ -46,6 +46,7 @@ module RequestBuilders # # Incoming parameter :f is mapped to :fq solr parameter. def solr_search_params(user_params = nil, processor_chain = nil) + Deprecation.warn(RequestBuilders, "solr_search_params is deprecated. Use SearchBuilder#processed_parameters instead") unless user_params Deprecation.warn(RequestBuilders, "Calling solr_search_params without a `user_params' argument is deprecated and will be removed in blacklight-6.0") user_params = params || {} @@ -61,6 +62,16 @@ def solr_search_params(user_params = nil, processor_chain = nil) end end + ## + # @param [Hash] user_params a hash of user submitted parameters + # @param [Array] processor_chain a list of processor methods to run + # @param [Hash] extra_params an optional hash of parameters that should be + # added to the query post processing + def build_solr_query(user_params, processor_chain, extra_params=nil) + Deprecation.warn(RequestBuilders, "Use SearchBuilder#query instead") + Blacklight::SearchBuilder.new(user_params, processor_chain, self).query(extra_params) + end + ## # Retrieve the results for a list of document ids def solr_document_ids_params(ids = []) diff --git a/lib/blacklight/search_builder.rb b/lib/blacklight/search_builder.rb new file mode 100644 index 0000000000..ad51c3c955 --- /dev/null +++ b/lib/blacklight/search_builder.rb @@ -0,0 +1,43 @@ +module Blacklight + class SearchBuilder + # @param [Hash,HashWithIndifferentAccess] user_params the user provided parameters (e.g. query, facets, sort, etc) + # @param [List] processor_chain a list of filter methods to run + # @param [Object] scope the scope where the filter methods reside in. + def initialize(user_params, processor_chain, scope) + @user_params = user_params + @processor_chain = processor_chain + @scope = scope + end + + # a solr query method + # @param [Hash,HashWithIndifferentAccess] extra_controller_params (nil) extra parameters to add to the search + # @return [Blacklight::SolrResponse] the solr response object + def query(extra_params = nil) + extra_params ? processed_parameters.merge(extra_params) : processed_parameters + end + + # @returns a params hash for searching solr. + # The CatalogController #index action uses this. + # Solr parameters can come from a number of places. From lowest + # precedence to highest: + # 1. General defaults in blacklight config (are trumped by) + # 2. defaults for the particular search field identified by params[:search_field] (are trumped by) + # 3. certain parameters directly on input HTTP query params + # * not just any parameter is grabbed willy nilly, only certain ones are allowed by HTTP input) + # * for legacy reasons, qt in http query does not over-ride qt in search field definition default. + # 4. extra parameters passed in as argument. + # + # spellcheck.q will be supplied with the [:q] value unless specifically + # specified otherwise. + # + # Incoming parameter :f is mapped to :fq solr parameter. + def processed_parameters + Blacklight::Solr::Request.new.tap do |solr_parameters| + @processor_chain.each do |method_name| + @scope.send(method_name, solr_parameters, @user_params) + end + end + end + + end +end diff --git a/lib/blacklight/solr_helper.rb b/lib/blacklight/solr_helper.rb index 2deccace28..bcb6e014e1 100644 --- a/lib/blacklight/solr_helper.rb +++ b/lib/blacklight/solr_helper.rb @@ -79,7 +79,17 @@ def solr_doc_params(id=nil) # and second an array of SolrDocuments representing the response.docs def get_search_results(user_params = params || {}, extra_controller_params = {}) Deprecation.warn(self, "get_search_results is deprecated and will be removed in blacklight-6.0. Use `search_results' instead") - search_results(user_params, extra_controller_params, solr_search_params_logic) + query = Blacklight::SearchBuilder.new(user_params, solr_search_params_logic, self).query(extra_controller_params) + solr_response = solr_repository.search(query) + + case + when (solr_response.grouped? && grouped_key_for_results) + [solr_response.group(grouped_key_for_results), []] + when (solr_response.grouped? && solr_response.grouped.length == 1) + [solr_response.grouped.first, []] + else + [solr_response, solr_response.documents] + end end # a solr query method @@ -87,8 +97,9 @@ def get_search_results(user_params = params || {}, extra_controller_params = {}) # @param [Hash,HashWithIndifferentAccess] extra_controller_params ({}) extra parameters to add to the search # @param [List] the solr response object and a list of solr documents def get_solr_response_for_field_values(field, values, extra_controller_params = {}) - solr_response = query_solr(params, extra_controller_params.merge(solr_documents_by_field_values_params(field, values)), solr_search_params_logic) + query = Blacklight::SearchBuilder.new(params, solr_search_params_logic, self).query(extra_controller_params.merge(solr_documents_by_field_values_params(field, values))) + solr_response = solr_repository.search(query) + [solr_response, solr_response.documents] end @@ -179,7 +180,8 @@ def get_solr_response_for_field_values(field, values, extra_controller_params = # @return [Blacklight::SolrResponse] the solr response def get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {}) solr_params = solr_facet_params(facet_field, user_params, extra_controller_params) - query_solr(user_params, extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params)), solr_search_params_logic) + query = Blacklight::SearchBuilder.new(user_params, solr_search_params_logic, self).query(extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params))) + solr_repository.search(query) end # a solr query method @@ -210,12 +212,10 @@ def get_facet_pagination(facet_field, user_params=params || {}, extra_controller # the Blacklight app-level request params that define the search. # @return [Blacklight::SolrDocument, nil] the found document or nil if not found def get_single_doc_via_search(index, request_params) - solr_params = solr_search_params(request_params, solr_search_params_logic) - - solr_params[:start] = (index - 1) # start at 0 to get 1st doc, 1 to get 2nd. - solr_params[:rows] = 1 - solr_params[:fl] = '*' - solr_response = solr_repository.search(solr_params) + # start at 0 to get 1st doc, 1 to get 2nd. + extra_params = { start: (index - 1), rows: 1, fl: '*' } + query = Blacklight::SearchBuilder.new(request_params, solr_search_params_logic, self).query(extra_params) + solr_response = solr_repository.search(query) solr_response.documents.first end deprecation_deprecate :get_single_doc_via_search @@ -224,7 +224,8 @@ def get_single_doc_via_search(index, request_params) # @return [Blacklight::SolrResponse, Array] the solr response and a list of the first and last document def get_previous_and_next_documents_for_search(index, request_params, extra_controller_params={}) - solr_response = query_solr(request_params, extra_controller_params.merge(previous_and_next_document_params(index)), solr_search_params_logic) + query = Blacklight::SearchBuilder.new(request_params, solr_search_params_logic, self).query(extra_controller_params.merge(previous_and_next_document_params(index))) + solr_response = solr_repository.search(query) document_list = solr_response.documents @@ -244,7 +245,8 @@ def get_previous_and_next_documents_for_search(index, request_params, extra_cont def get_opensearch_response(field=nil, request_params = params || {}, extra_controller_params={}) field ||= blacklight_config.view_config('opensearch').title_field - response = query_solr(request_params, solr_opensearch_params(field).merge(extra_controller_params), solr_search_params_logic) + query = build_solr_query(request_params, solr_search_params_logic, solr_opensearch_params(field).merge(extra_controller_params)) + response = solr_repository.search(query) [response.params[:q], response.documents.flat_map {|doc| doc[field] }.uniq] end diff --git a/spec/lib/blacklight/search_builder_spec.rb b/spec/lib/blacklight/search_builder_spec.rb new file mode 100644 index 0000000000..d31965c7d8 --- /dev/null +++ b/spec/lib/blacklight/search_builder_spec.rb @@ -0,0 +1,341 @@ +require 'spec_helper' + +describe Blacklight::SearchBuilder do + let(:single_facet) { { format: 'Book' } } + let(:multi_facets) { { format: 'Book', language_facet: 'Tibetan' } } + let(:mult_word_query) { 'tibetan history' } + let(:subject_search_params) { { commit: "search", search_field: "subject", action: "index", controller: "catalog", rows: "10", q: "wome" } } + + let(:blacklight_config) { CatalogController.blacklight_config.deep_copy } + let(:method_chain) { CatalogController.solr_search_params_logic } + let(:user_params) { Hash.new } + let(:context) { CatalogController.new } + + before { allow(context).to receive(:blacklight_config).and_return(blacklight_config) } + + let(:search_builder) { described_class.new(user_params, method_chain, context) } + + subject { search_builder.processed_parameters } + + context "with a complex parameter environment" do + let(:user_params) do + {:search_field => "test_field", :q => "test query", "facet.field" => "extra_facet"} + end + + let(:blacklight_config) do + Blacklight::Configuration.new.tap do |config| + config.add_search_field("test_field", + :display_label => "Test", + :key=>"test_field", + :solr_parameters => { + :qf => "fieldOne^2.3 fieldTwo fieldThree^0.4", + :pf => "", + :spellcheck => 'false', + :rows => "55", + :sort => "request_params_sort" } + ) + end + end + it "should merge parameters from search_field definition" do + expect(subject[:qf]).to eq "fieldOne^2.3 fieldTwo fieldThree^0.4" + expect(subject[:spellcheck]).to eq 'false' + end + it "should merge empty string parameters from search_field definition" do + expect(subject[:pf]).to eq "" + end + + describe "should respect proper precedence of settings, " do + it "should not put :search_field in produced params" do + expect(subject[:search_field]).to be_nil + end + + it "should fall through to BL general defaults for qt not otherwise specified " do + expect(subject[:qt]).to eq blacklight_config[:default_solr_params][:qt] + end + + it "should take rows from search field definition where specified" do + expect(subject[:rows]).to eq "55" + end + + it "should take q from request params" do + expect(subject[:q]).to eq "test query" + end + + it "should add in extra facet.field from params" do + expect(subject[:"facet.field"]).to include("extra_facet") + end + end + end + + # SPECS for actual search parameter generation + describe "#processed_parameters" do + context "when search_params_logic is customized" do + let(:method_chain) { [:add_foo_to_solr_params] } + + it "allows customization of solr_search_params_logic" do + # Normally you'd include a new module into (eg) your CatalogController + # but a sub-class defininig it directly is simpler for test. + allow(context).to receive(:add_foo_to_solr_params) do |solr_params, user_params| + solr_params[:wt] = "TESTING" + end + + expect(subject[:wt]).to eq "TESTING" + end + end + + it "should generate a facet limit" do + expect(subject[:"f.subject_topic_facet.facet.limit"]).to eq 21 + end + + it "should handle no facet_limits in config" do + blacklight_config.facet_fields = {} + expect(subject).not_to have_key(:"f.subject_topic_facet.facet.limit") + end + + describe "with max per page enforced" do + let(:blacklight_config) do + Blacklight::Configuration.new.tap do |config| + config.max_per_page = 123 + end + end + + let(:user_params) { { per_page: 98765 } } + it "should enforce max_per_page against all parameters" do + expect(blacklight_config.max_per_page).to eq 123 + expect(subject[:rows]).to eq 123 + end + end + + describe 'for an entirely empty search' do + + it 'should not have a q param' do + expect(subject[:q]).to be_nil + expect(subject["spellcheck.q"]).to be_nil + end + it 'should have default rows' do + expect(subject[:rows]).to eq 10 + end + it 'should have default facet fields' do + # remove local params from the facet.field + expect(subject[:"facet.field"].map { |x| x.gsub(/\{![^}]+\}/, '') }).to match_array ["format", "subject_topic_facet", "pub_date", "language_facet", "lc_1letter_facet", "subject_geo_facet", "subject_era_facet"] + end + + it "should have default qt" do + expect(subject[:qt]).to eq "search" + end + it "should have no fq" do + expect(subject[:phrase_filters]).to be_blank + expect(subject[:fq]).to be_blank + end + end + + + describe "for an empty string search" do + let(:user_params) { { q: "" } } + it "should return empty string q in solr parameters" do + expect(subject[:q]).to eq "" + expect(subject["spellcheck.q"]).to eq "" + end + end + + describe "for request params also passed in as argument" do + let(:user_params) { { q: "some query", 'q' => 'another value' } } + it "should only have one value for the key 'q' regardless if a symbol or string" do + expect(subject[:q]).to eq 'some query' + expect(subject['q']).to eq 'some query' + end + end + + + describe "for one facet, no query" do + let(:user_params) { { f: single_facet } } + it "should have proper solr parameters" do + + expect(subject[:q]).to be_blank + expect(subject["spellcheck.q"]).to be_blank + + single_facet.each_value do |value| + expect(subject[:fq]).to include("{!raw f=#{single_facet.keys[0]}}#{value}") + end + end + end + + describe "for an empty facet limit param" do + let(:user_params) { { f: { "format" => [""] } } } + it "should not add any fq to solr" do + expect(subject[:fq]).to be_blank + end + end + + describe "with Multi Facets, No Query" do + let(:user_params) { { f: multi_facets } } + it 'should have fq set properly' do + multi_facets.each_pair do |facet_field, value_list| + value_list ||= [] + value_list = [value_list] unless value_list.respond_to? :each + value_list.each do |value| + expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) + end + end + + end + end + + describe "with Multi Facets, Multi Word Query" do + let(:user_params) { { q: mult_word_query, f: multi_facets } } + it 'should have fq and q set properly' do + multi_facets.each_pair do |facet_field, value_list| + value_list ||= [] + value_list = [value_list] unless value_list.respond_to? :each + value_list.each do |value| + expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) + end + end + expect(subject[:q]).to eq mult_word_query + end + end + + + describe "solr parameters for a field search from config (subject)" do + let(:user_params) { subject_search_params } + + it "should look up qt from field definition" do + expect(subject[:qt]).to eq "search" + end + + it "should not include weird keys not in field definition" do + expect(subject[:phrase_filters]).to be_nil + expect(subject[:fq]).to eq [] + expect(subject[:commit]).to be_nil + expect(subject[:action]).to be_nil + expect(subject[:controller]).to be_nil + end + + it "should include proper 'q', possibly with LocalParams" do + expect(subject[:q]).to match(/(\{[^}]+\})?wome/) + end + it "should include proper 'q' when LocalParams are used" do + if subject[:q] =~ /\{[^}]+\}/ + expect(subject[:q]).to match(/\{[^}]+\}wome/) + end + end + it "should include spellcheck.q, without LocalParams" do + expect(subject["spellcheck.q"]).to eq "wome" + end + + it "should include spellcheck.dictionary from field def solr_parameters" do + expect(subject[:"spellcheck.dictionary"]).to eq "subject" + end + it "should add on :solr_local_parameters using Solr LocalParams style" do + #q == "{!pf=$subject_pf $qf=subject_qf} wome", make sure + #the LocalParams are really there + subject[:q] =~ /^\{!([^}]+)\}/ + key_value_pairs = $1.split(" ") + expect(key_value_pairs).to include("pf=$subject_pf") + expect(key_value_pairs).to include("qf=$subject_qf") + end + end + + describe "overriding of qt parameter" do + let(:user_params) do + { qt: 'overridden' } + end + + it "should return the correct overriden parameter" do + expect(subject[:qt]).to eq "overridden" + end + end + + + describe "sorting" do + it "should send the default sort parameter to solr" do + expect(subject[:sort]).to eq 'score desc, pub_date_sort desc, title_sort asc' + end + + it "should not send a sort parameter to solr if the sort value is blank" do + blacklight_config.sort_fields = {} + blacklight_config.add_sort_field('', :label => 'test') + + expect(subject).not_to have_key(:sort) + end + + context "when the user provides sort parmeters" do + let(:user_params) { { sort: 'solr_test_field desc' } } + it "passes them through" do + expect(subject[:sort]).to eq 'solr_test_field desc' + end + end + end + + describe "for :solr_local_parameters config" do + let(:blacklight_config) do + config = Blacklight::Configuration.new + config.add_search_field( + "custom_author_key", + :display_label => "Author", + :qt => "author_qt", + :key => "custom_author_key", + :solr_local_parameters => { + :qf => "$author_qf", + :pf => "you'll have \" to escape this", + :pf2 => "$pf2_do_not_escape_or_quote" + }, + :solr_parameters => { + :qf => "someField^1000", + :ps => "2" + } + ) + return config + end + + let(:user_params) { { search_field: "custom_author_key", q: "query" } } + + it "should pass through ordinary params" do + expect(subject[:qt]).to eq "author_qt" + expect(subject[:ps]).to eq "2" + expect(subject[:qf]).to eq "someField^1000" + end + + it "should include include local params with escaping" do + expect(subject[:q]).to include('qf=$author_qf') + expect(subject[:q]).to include('pf=\'you\\\'ll have \\" to escape this\'') + expect(subject[:q]).to include('pf2=$pf2_do_not_escape_or_quote') + end + end + + describe "mapping facet.field" do + let(:blacklight_config) do + Blacklight::Configuration.new do |config| + config.add_facet_field 'some_field' + config.add_facet_fields_to_solr_request! + end + end + + context "user provides a single facet.field" do + let(:user_params) { { "facet.field" => "additional_facet" } } + it "adds the field" do + expect(subject[:"facet.field"]).to include("additional_facet") + expect(subject[:"facet.field"]).to have(2).fields + end + end + + context "user provides a multiple facet.field" do + let(:user_params) { { "facet.field" => ["add_facet1", "add_facet2"] } } + it "adds the fields" do + expect(subject[:"facet.field"]).to include("add_facet1") + expect(subject[:"facet.field"]).to include("add_facet2") + expect(subject[:"facet.field"]).to have(3).fields + end + end + + context "user provides a multiple facets" do + let(:user_params) { { "facets" => ["add_facet1", "add_facet2"] } } + it "adds the fields" do + expect(subject[:"facet.field"]).to include("add_facet1") + expect(subject[:"facet.field"]).to include("add_facet2") + expect(subject[:"facet.field"]).to have(3).fields + end + end + end + end +end diff --git a/spec/lib/blacklight/solr_helper_spec.rb b/spec/lib/blacklight/solr_helper_spec.rb index 990e76a0c1..847981888f 100644 --- a/spec/lib/blacklight/solr_helper_spec.rb +++ b/spec/lib/blacklight/solr_helper_spec.rb @@ -51,7 +51,6 @@ def params @subject_search_params = {:commit=>"search", :search_field=>"subject", :action=>"index", :"controller"=>"catalog", :"rows"=>"10", :"q"=>"wome"} end - # SPECS for actual search parameter generation describe "solr_search_params" do it "allows customization of solr_search_params_logic" do # Normally you'd include a new module into (eg) your CatalogController @@ -60,505 +59,214 @@ def params solr_params[:wt] = "TESTING" end + expect(Deprecation).to receive(:warn) expect(subject.solr_search_params({}, [:add_foo_to_solr_params])[:wt]).to eq "TESTING" end - - - describe 'for an entirely empty search' do - before do - @produced_params = subject.solr_search_params({}, default_method_chain).with_indifferent_access - end + end - let(:blacklight_config) { copy_of_catalog_config } + describe "facet_value_to_fq_string" do - it 'should not have a q param' do - expect(@produced_params[:q]).to be_nil - expect(@produced_params["spellcheck.q"]).to be_nil - end - it 'should have default rows' do - expect(@produced_params[:rows]).to eq 10 - end - it 'should have default facet fields' do - # remove local params from the facet.field - expect(@produced_params[:"facet.field"].map { |x| x.gsub(/\{![^}]+\}/, '') }).to match_array ["format", "subject_topic_facet", "pub_date", "language_facet", "lc_1letter_facet", "subject_geo_facet", "subject_era_facet"] - end - - it "should have default qt" do - expect(@produced_params[:qt]).to eq "search" - end - it "should have no fq" do - expect(@produced_params[:phrase_filters]).to be_blank - expect(@produced_params[:fq]).to be_blank - end + it "should use the raw handler for strings" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name}my value" end - - describe "for an empty string search" do - it "should return empty string q in solr parameters" do - solr_params = subject.solr_search_params({ q: "" }, default_method_chain) - expect(solr_params[:q]).to eq "" - expect(solr_params["spellcheck.q"]).to eq "" - end + it "should pass booleans through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "facet_name:true" end - describe "for request params also passed in as argument" do - it "should only have one value for the key 'q' regardless if a symbol or string" do - solr_params = subject.solr_search_params({ q: "some query", 'q' => 'another value' }, default_method_chain) - expect(solr_params[:q]).to eq 'some query' - expect(solr_params['q']).to eq 'some query' - end + it "should pass boolean-like strings through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq "facet_name:true" end + it "should pass integers through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq "facet_name:1" + end - describe "for one facet, no query" do - it "should have proper solr parameters" do + it "should pass integer-like strings through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq "facet_name:1" + end - solr_params = subject.solr_search_params({ f: @single_facet }, default_method_chain) + it "should pass floats through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq "facet_name:1\\.11" + end - expect(solr_params[:q]).to be_blank - expect(solr_params["spellcheck.q"]).to be_blank + it "should pass floats through" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq "facet_name:1\\.11" + end - @single_facet.each_value do |value| - expect(solr_params[:fq]).to include("{!raw f=#{@single_facet.keys[0]}}#{value}") - end - end + it "should escape negative integers" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq "facet_name:\\-1" end - describe "for an empty facet limit param" do - it "should not add any fq to solr" do - solr_params = subject.solr_search_params({ f: { "format" => [""] } }, default_method_chain) + it "should pass date-type fields through" do + allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil)) - expect(solr_params[:fq]).to be_blank - end + expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01" end - describe "with Multi Facets, No Query" do - it 'should have fq set properly' do - solr_params = subject.solr_search_params({ f: @multi_facets }, default_method_chain) + it "should escape datetime-type fields" do + allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil)) - @multi_facets.each_pair do |facet_field, value_list| - value_list ||= [] - value_list = [value_list] unless value_list.respond_to? :each - value_list.each do |value| - expect(solr_params[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) - end - end - - end + expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" + end + + it "should format Date objects correctly" do + allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil)) + d = DateTime.parse("2003-04-09T00:00:00") + expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" end - describe "with Multi Facets, Multi Word Query" do - it 'should have fq and q set properly' do - solr_params = subject.solr_search_params({ q: @mult_word_query, f: @multi_facets }, default_method_chain) - - @multi_facets.each_pair do |facet_field, value_list| - value_list ||= [] - value_list = [value_list] unless value_list.respond_to? :each - value_list.each do |value| - expect(solr_params[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) - end - end - expect(solr_params[:q]).to eq @mult_word_query - end + it "should handle range requests" do + expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..5)).to eq "facet_name:[1 TO 5]" end - describe "facet_value_to_fq_string" do + it "should add tag local parameters" do + allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil)) - it "should use the raw handler for strings" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name}my value" - end + expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!tag=asdf}facet_name:true" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name tag=asdf}my value" + end + end - it "should pass booleans through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "facet_name:true" - end + describe "converts a String fq into an Array" do + it "should return the correct overriden parameter" do + solr_parameters = {:fq => 'a string' } - it "should pass boolean-like strings through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq "facet_name:true" - end + subject.add_facet_fq_to_solr(solr_parameters, {}) - it "should pass integers through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq "facet_name:1" - end + expect(solr_parameters[:fq]).to be_a_kind_of Array + end + end - it "should pass integer-like strings through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq "facet_name:1" - end + describe "#add_solr_fields_to_query" do + let(:blacklight_config) do + config = Blacklight::Configuration.new do |config| - it "should pass floats through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq "facet_name:1\\.11" + config.add_index_field 'an_index_field', solr_params: { 'hl.alternativeField' => 'field_x'} + config.add_show_field 'a_show_field', solr_params: { 'hl.alternativeField' => 'field_y'} + config.add_field_configuration_to_solr_request! end + end - it "should pass floats through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq "facet_name:1\\.11" - end + let(:solr_parameters) do + solr_parameters = Blacklight::Solr::Request.new - it "should escape negative integers" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq "facet_name:\\-1" - end + subject.add_solr_fields_to_query(solr_parameters, {}) - it "should pass date-type fields through" do - allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil)) - - expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01" - end + solr_parameters + end - it "should escape datetime-type fields" do - allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil)) + it "should add any extra solr parameters from index and show fields" do + expect(solr_parameters[:'f.an_index_field.hl.alternativeField']).to eq "field_x" + expect(solr_parameters[:'f.a_show_field.hl.alternativeField']).to eq "field_y" + end + end - expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" - end - - it "should format Date objects correctly" do - allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil)) - d = DateTime.parse("2003-04-09T00:00:00") - expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" - end + describe "#add_facetting_to_solr" do - it "should handle range requests" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..5)).to eq "facet_name:[1 TO 5]" - end + let(:blacklight_config) do + config = Blacklight::Configuration.new - it "should add tag local parameters" do - allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil)) + config.add_facet_field 'test_field', :sort => 'count' + config.add_facet_field 'some-query', :query => {'x' => {:fq => 'some:query' }}, :ex => 'xyz' + config.add_facet_field 'some-pivot', :pivot => ['a','b'], :ex => 'xyz' + config.add_facet_field 'some-field', solr_params: { 'facet.mincount' => 15 } + config.add_facet_fields_to_solr_request! - expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!tag=asdf}facet_name:true" - expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name tag=asdf}my value" - end + config end - describe "solr parameters for a field search from config (subject)" do - let(:solr_params) { subject.solr_search_params @subject_search_params, default_method_chain } - let(:blacklight_config) { copy_of_catalog_config } - - it "should look up qt from field definition" do - expect(solr_params[:qt]).to eq "search" - end - it "should not include weird keys not in field definition" do - solr_params.to_hash.tap do |h| - expect(h[:phrase_filters]).to be_nil - expect(h[:fq]).to be_nil - expect(h[:commit]).to be_nil - expect(h[:action]).to be_nil - expect(h[:controller]).to be_nil - end - end - it "should include proper 'q', possibly with LocalParams" do - expect(solr_params[:q]).to match(/(\{[^}]+\})?wome/) - end - it "should include proper 'q' when LocalParams are used" do - if solr_params[:q] =~ /\{[^}]+\}/ - expect(solr_params[:q]).to match(/\{[^}]+\}wome/) - end - end - it "should include spellcheck.q, without LocalParams" do - expect(solr_params["spellcheck.q"]).to eq "wome" - end + let(:solr_parameters) do + solr_parameters = Blacklight::Solr::Request.new + + subject.add_facetting_to_solr(solr_parameters, {}) - it "should include spellcheck.dictionary from field def solr_parameters" do - expect(solr_params[:"spellcheck.dictionary"]).to eq "subject" - end - it "should add on :solr_local_parameters using Solr LocalParams style" do - #q == "{!pf=$subject_pf $qf=subject_qf} wome", make sure - #the LocalParams are really there - solr_params[:q] =~ /^\{!([^}]+)\}/ - key_value_pairs = $1.split(" ") - expect(key_value_pairs).to include("pf=$subject_pf") - expect(key_value_pairs).to include("qf=$subject_qf") - end + solr_parameters end - describe "overriding of qt parameter" do - let(:user_params) do - { qt: 'overridden' } - end - let(:produced_params) { subject.solr_search_params(user_params, default_method_chain) } + it "should add sort parameters" do + expect(solr_parameters[:facet]).to be true - it "should return the correct overriden parameter" do - allow(subject).to receive_messages(params: { qt: 'overridden' }) - expect(produced_params[:qt]).to eq "overridden" - end + expect(solr_parameters[:'facet.field']).to include('test_field') + expect(solr_parameters[:'f.test_field.facet.sort']).to eq 'count' end - describe "converts a String fq into an Array" do - it "should return the correct overriden parameter" do - solr_parameters = {:fq => 'a string' } - - subject.add_facet_fq_to_solr(solr_parameters, {}) - - expect(solr_parameters[:fq]).to be_a_kind_of Array - end + it "should add facet exclusions" do + expect(solr_parameters[:'facet.query']).to include('{!ex=xyz}some:query') + expect(solr_parameters[:'facet.pivot']).to include('{!ex=xyz}a,b') end - describe "#add_solr_fields_to_query" do - let(:blacklight_config) do - config = Blacklight::Configuration.new do |config| - - config.add_index_field 'an_index_field', solr_params: { 'hl.alternativeField' => 'field_x'} - config.add_show_field 'a_show_field', solr_params: { 'hl.alternativeField' => 'field_y'} - config.add_field_configuration_to_solr_request! - end - end - - let(:solr_parameters) do - solr_parameters = Blacklight::Solr::Request.new - - subject.add_solr_fields_to_query(solr_parameters, {}) - - solr_parameters - end - - it "should add any extra solr parameters from index and show fields" do - expect(solr_parameters[:'f.an_index_field.hl.alternativeField']).to eq "field_x" - expect(solr_parameters[:'f.a_show_field.hl.alternativeField']).to eq "field_y" - end + it "should add any additional solr_params" do + expect(solr_parameters[:'f.some-field.facet.mincount']).to eq 15 end - describe "#add_facetting_to_solr" do - - let(:blacklight_config) do - config = Blacklight::Configuration.new - - config.add_facet_field 'test_field', :sort => 'count' - config.add_facet_field 'some-query', :query => {'x' => {:fq => 'some:query' }}, :ex => 'xyz' - config.add_facet_field 'some-pivot', :pivot => ['a','b'], :ex => 'xyz' - config.add_facet_field 'some-field', solr_params: { 'facet.mincount' => 15 } - config.add_facet_fields_to_solr_request! - - config - end - + describe ":include_in_request" do let(:solr_parameters) do solr_parameters = Blacklight::Solr::Request.new - subject.add_facetting_to_solr(solr_parameters, {}) - solr_parameters end - it "should add sort parameters" do - expect(solr_parameters[:facet]).to be true - - expect(solr_parameters[:'facet.field']).to include('test_field') - expect(solr_parameters[:'f.test_field.facet.sort']).to eq 'count' - end - - it "should add facet exclusions" do - expect(solr_parameters[:'facet.query']).to include('{!ex=xyz}some:query') - expect(solr_parameters[:'facet.pivot']).to include('{!ex=xyz}a,b') - end - - it "should add any additional solr_params" do - expect(solr_parameters[:'f.some-field.facet.mincount']).to eq 15 - end - - describe ":include_in_request" do - let(:solr_parameters) do - solr_parameters = Blacklight::Solr::Request.new - subject.add_facetting_to_solr(solr_parameters, {}) - solr_parameters - end - - it "should respect the include_in_request parameter" do - blacklight_config.add_facet_field 'yes_facet', include_in_request: true - blacklight_config.add_facet_field 'no_facet', include_in_request: false - - expect(solr_parameters[:'facet.field']).to include('yes_facet') - expect(solr_parameters[:'facet.field']).not_to include('no_facet') - end - - it "should default to including facets if add_facet_fields_to_solr_request! was called" do - blacklight_config.add_facet_field 'yes_facet' - blacklight_config.add_facet_field 'no_facet', include_in_request: false - blacklight_config.add_facet_fields_to_solr_request! - - expect(solr_parameters[:'facet.field']).to include('yes_facet') - expect(solr_parameters[:'facet.field']).not_to include('no_facet') - end - end - - describe ":add_facet_fields_to_solr_request!" do - - let(:blacklight_config) do - config = Blacklight::Configuration.new - config.add_facet_field 'yes_facet', include_in_request: true - config.add_facet_field 'no_facet', include_in_request: false - config.add_facet_field 'maybe_facet' - config.add_facet_field 'another_facet' - config - end - - let(:solr_parameters) do - solr_parameters = Blacklight::Solr::Request.new - subject.add_facetting_to_solr(solr_parameters, {}) - solr_parameters - end - - it "should add facets to the solr request" do - blacklight_config.add_facet_fields_to_solr_request! - expect(solr_parameters[:'facet.field']).to match_array ['yes_facet', 'maybe_facet', 'another_facet'] - end - - it "should not override field-specific configuration by default" do - blacklight_config.add_facet_fields_to_solr_request! - expect(solr_parameters[:'facet.field']).to_not include 'no_facet' - end - - it "should allow white-listing facets" do - blacklight_config.add_facet_fields_to_solr_request! 'maybe_facet' - expect(solr_parameters[:'facet.field']).to include 'maybe_facet' - expect(solr_parameters[:'facet.field']).not_to include 'another_facet' - end - - it "should allow white-listed facets to override any field-specific include_in_request configuration" do - blacklight_config.add_facet_fields_to_solr_request! 'no_facet' - expect(solr_parameters[:'facet.field']).to include 'no_facet' - end - end - end - - describe "with a complex parameter environment" do - let(:blacklight_config) do - config = Blacklight::Configuration.new - config.add_search_field("test_field", - :display_label => "Test", - :key=>"test_field", - :solr_parameters => { - :qf => "fieldOne^2.3 fieldTwo fieldThree^0.4", - :pf => "", - :spellcheck => 'false', - :rows => "55", - :sort => "request_params_sort" } - ) - return config - end - - let(:user_params) do - {:search_field => "test_field", :q => "test query", "facet.field" => "extra_facet"} - end - - let(:produced_params) { subject.solr_search_params(user_params, default_method_chain) } - - it "should merge parameters from search_field definition" do - expect(produced_params[:qf]).to eq "fieldOne^2.3 fieldTwo fieldThree^0.4" - expect(produced_params[:spellcheck]).to eq 'false' - end - it "should merge empty string parameters from search_field definition" do - expect(produced_params[:pf]).to eq "" + it "should respect the include_in_request parameter" do + blacklight_config.add_facet_field 'yes_facet', include_in_request: true + blacklight_config.add_facet_field 'no_facet', include_in_request: false + + expect(solr_parameters[:'facet.field']).to include('yes_facet') + expect(solr_parameters[:'facet.field']).not_to include('no_facet') end - describe "should respect proper precedence of settings, " do - it "should not put :search_field in produced params" do - expect(produced_params[:search_field]).to be_nil - end - - it "should fall through to BL general defaults for qt not otherwise specified " do - expect(produced_params[:qt]).to eq blacklight_config[:default_solr_params][:qt] - end - - it "should take rows from search field definition where specified" do - expect(produced_params[:rows]).to eq "55" - end - - it "should take q from request params" do - expect(produced_params[:q]).to eq "test query" - end + it "should default to including facets if add_facet_fields_to_solr_request! was called" do + blacklight_config.add_facet_field 'yes_facet' + blacklight_config.add_facet_field 'no_facet', include_in_request: false + blacklight_config.add_facet_fields_to_solr_request! - it "should add in extra facet.field from params" do - expect(produced_params[:"facet.field"]).to include("extra_facet") - end + expect(solr_parameters[:'facet.field']).to include('yes_facet') + expect(solr_parameters[:'facet.field']).not_to include('no_facet') end end - describe "sorting" do - let(:blacklight_config) { copy_of_catalog_config } - - it "should send the default sort parameter to solr" do - expect(subject.solr_search_params({}, default_method_chain)[:sort]).to eq 'score desc, pub_date_sort desc, title_sort asc' - end - - it "should not send a sort parameter to solr if the sort value is blank" do - blacklight_config.sort_fields = {} - blacklight_config.add_sort_field('', :label => 'test') + describe ":add_facet_fields_to_solr_request!" do - produced_params = subject.solr_search_params({}, default_method_chain) - expect(produced_params).not_to have_key(:sort) - end - - it "should pass through user sort parameters" do - produced_params = subject.solr_search_params({ sort: 'solr_test_field desc' }, default_method_chain) - expect(produced_params[:sort]).to eq 'solr_test_field desc' - end - end - - describe "for :solr_local_parameters config" do - let(:blacklight_config) do + let(:blacklight_config) do config = Blacklight::Configuration.new - config.add_search_field( - "custom_author_key", - :display_label => "Author", - :qt => "author_qt", - :key => "custom_author_key", - :solr_local_parameters => { - :qf => "$author_qf", - :pf => "you'll have \" to escape this", - :pf2 => "$pf2_do_not_escape_or_quote" - }, - :solr_parameters => { - :qf => "someField^1000", - :ps => "2" - } - ) - return config - end - - let(:result) do - subject.solr_search_params({:search_field => "custom_author_key", :q => "query"}, default_method_chain) - end - - it "should pass through ordinary params" do - expect(result[:qt]).to eq "author_qt" - expect(result[:ps]).to eq "2" - expect(result[:qf]).to eq "someField^1000" + config.add_facet_field 'yes_facet', include_in_request: true + config.add_facet_field 'no_facet', include_in_request: false + config.add_facet_field 'maybe_facet' + config.add_facet_field 'another_facet' + config end - it "should include include local params with escaping" do - expect(result[:q]).to include('qf=$author_qf') - expect(result[:q]).to include('pf=\'you\\\'ll have \\" to escape this\'') - expect(result[:q]).to include('pf2=$pf2_do_not_escape_or_quote') + let(:solr_parameters) do + solr_parameters = Blacklight::Solr::Request.new + subject.add_facetting_to_solr(solr_parameters, {}) + solr_parameters end - end - describe "mapping facet.field" do - let(:blacklight_config) do - Blacklight::Configuration.new do |config| - config.add_facet_field 'some_field' - config.add_facet_fields_to_solr_request! - end + it "should add facets to the solr request" do + blacklight_config.add_facet_fields_to_solr_request! + expect(solr_parameters[:'facet.field']).to match_array ['yes_facet', 'maybe_facet', 'another_facet'] end - it "should add single additional facet.field from app" do - solr_params = subject.solr_search_params({ "facet.field" => "additional_facet" }, default_method_chain) - expect(solr_params[:"facet.field"]).to include("additional_facet") - expect(solr_params[:"facet.field"]).to have(2).fields + it "should not override field-specific configuration by default" do + blacklight_config.add_facet_fields_to_solr_request! + expect(solr_parameters[:'facet.field']).to_not include 'no_facet' end - it "should map multiple facet.field to additional facet.field" do - solr_params = subject.solr_search_params({ "facet.field" => ["add_facet1", "add_facet2"] }, default_method_chain) - expect(solr_params[:"facet.field"]).to include("add_facet1") - expect(solr_params[:"facet.field"]).to include("add_facet2") - expect(solr_params[:"facet.field"]).to have(3).fields + it "should allow white-listing facets" do + blacklight_config.add_facet_fields_to_solr_request! 'maybe_facet' + expect(solr_parameters[:'facet.field']).to include 'maybe_facet' + expect(solr_parameters[:'facet.field']).not_to include 'another_facet' end - it "should map facets[fields][] to additional facet.field" do - solr_params = subject.solr_search_params({ "facets" => ["add_facet1", "add_facet2"] }, default_method_chain) - expect(solr_params[:"facet.field"]).to include("add_facet1") - expect(solr_params[:"facet.field"]).to include("add_facet2") - expect(solr_params[:"facet.field"]).to have(3).fields + it "should allow white-listed facets to override any field-specific include_in_request configuration" do + blacklight_config.add_facet_fields_to_solr_request! 'no_facet' + expect(solr_parameters[:'facet.field']).to include 'no_facet' end end - end + + describe "solr_facet_params" do before do @facet_field = 'format' @@ -668,7 +376,7 @@ def params describe 'for a sample query returning results' do before do - (@solr_response, @document_list) = subject.search_results({ q: @all_docs_query }, {}, default_method_chain) + (@solr_response, @document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) end it "should use the configured request handler " do @@ -679,7 +387,7 @@ def params expect(params[:params]["facet.query"]).to eq ["pub_date:[#{5.years.ago.year} TO *]", "pub_date:[#{10.years.ago.year} TO *]", "pub_date:[#{25.years.ago.year} TO *]"] expect(params[:params]).to include('rows' => 10, 'qt'=>"custom_request_handler", 'q'=>"", "spellcheck.q"=>"", "f.subject_topic_facet.facet.limit"=>21, 'sort'=>"score desc, pub_date_sort desc, title_sort asc") end.and_return({'response'=>{'docs'=>[]}}) - subject.search_results({ q: @all_docs_query }, {}, default_method_chain) + subject.search_results({ q: @all_docs_query }, default_method_chain) end it 'should have a @response.docs list of the same size as @document_list' do @@ -701,16 +409,22 @@ def params end describe "#get_search_results " do - it "should be deprecated and call search_results()" do + it "should be deprecated and return results" do expect(Deprecation).to receive(:warn) - expect(subject).to receive(:search_results).with({}, {}, default_method_chain) - subject.get_search_results() + (solr_response, document_list) = subject.get_search_results(q: @all_docs_query) + result_docs = document_list + document = result_docs.first + expect(document.get(blacklight_config.index.title_field)).not_to be_nil + expect(document.get(blacklight_config.index.display_type_field)).not_to be_nil end end describe "for a query returning a grouped response" do + let(:blacklight_config) { copy_of_catalog_config } before do - (@solr_response, @document_list) = subject.search_results({ q: @all_docs_query }, { group: true, :'group.field' => 'pub_date_sort' }, default_method_chain) + blacklight_config.default_solr_params[:group] = true + blacklight_config.default_solr_params[:'group.field'] = 'pub_date_sort' + (@solr_response, @document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) end it "should have an empty document list" do @@ -728,7 +442,9 @@ def params before do allow(subject).to receive_messages grouped_key_for_results: 'title_sort' - (@solr_response, @document_list) = subject.search_results({:q => @all_docs_query}, { group: true, :'group.field' => ['pub_date_sort', 'title_sort'] }, default_method_chain) + blacklight_config.default_solr_params[:group] = true + blacklight_config.default_solr_params[:'group.field'] = ['pub_date_sort', 'title_sort'] + (@solr_response, @document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) end it "should have an empty document list" do @@ -743,7 +459,8 @@ def params describe '#query_solr' do it 'should have results' do - solr_response = subject.query_solr({ q: @single_word_query }, {}, default_method_chain) + expect(Deprecation).to receive(:warn) + solr_response = subject.query_solr(q: @single_word_query) expect(solr_response.docs).to have_at_least(1).result end @@ -751,7 +468,7 @@ def params describe 'for All Docs Query, No Facets' do it 'should have non-nil values for required doc fields set in initializer' do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) result_docs = document_list document = result_docs.first expect(document.get(blacklight_config.index.title_field)).not_to be_nil @@ -763,12 +480,13 @@ def params describe "Single Word Query with no Facets" do it 'should have results' do - solr_response = subject.query_solr({ q: @single_word_query }, {}, default_method_chain) + expect(Deprecation).to receive(:warn) + solr_response = subject.query_solr( q: @single_word_query) expect(solr_response.docs).to have_at_least(1).result end it 'should have results' do - (solr_response, document_list) = subject.search_results({ q: @single_word_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @single_word_query }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -777,7 +495,7 @@ def params describe "Multiple Words Query with No Facets" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ q: @mult_word_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @mult_word_query }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -785,7 +503,7 @@ def params describe "One Facet, No Query" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ f: @single_facet }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ f: @single_facet }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -793,7 +511,7 @@ def params describe "Mult Facets, No Query" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ f: @multi_facets }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ f: @multi_facets }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -801,7 +519,7 @@ def params describe "Single Word Query with One Facet" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ q: @single_word_query, f: @single_facet }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @single_word_query, f: @single_facet }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -809,7 +527,7 @@ def params describe "Multiple Words Query with Multiple Facets" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ q: @mult_word_query, f: @multi_facets }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @mult_word_query, f: @multi_facets }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -817,7 +535,7 @@ def params describe "for All Docs Query and One Facet" do it 'should have results' do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query, f: @single_facet }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query, f: @single_facet }, default_method_chain) expect(solr_response.docs).to have(document_list.size).results expect(solr_response.docs).to have_at_least(1).result end @@ -827,7 +545,7 @@ def params describe "for Query Without Results and No Facet" do it 'should have no results and not raise error' do - (solr_response, document_list) = subject.search_results({ q: @no_docs_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @no_docs_query }, default_method_chain) expect(document_list).to have(0).results expect(solr_response.docs).to have(0).results end @@ -835,7 +553,7 @@ def params describe "for Query Without Results and One Facet" do it 'should have no results and not raise error' do - (solr_response, document_list) = subject.search_results({ q: @no_docs_query, f: @single_facet }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @no_docs_query, f: @single_facet }, default_method_chain) expect(document_list).to have(0).results expect(solr_response.docs).to have(0).results end @@ -843,7 +561,7 @@ def params describe "for All Docs Query and Bad Facet" do it 'should have no results and not raise error' do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query, f: @bad_facet }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query, f: @bad_facet }, default_method_chain) expect(document_list).to have(0).results expect(solr_response.docs).to have(0).results end @@ -857,7 +575,7 @@ def params let(:blacklight_config) { copy_of_catalog_config } before do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query}, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query}, default_method_chain) @facets = solr_response.facets end @@ -901,44 +619,44 @@ def params let(:blacklight_config) { copy_of_catalog_config } it 'should start with first results by default' do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) expect(solr_response.params[:start].to_i).to eq 0 end it 'should have number of results (per page) set in initializer, by default' do - (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: @all_docs_query }, default_method_chain) expect(solr_response.docs).to have(blacklight_config[:default_solr_params][:rows]).items expect(document_list).to have(blacklight_config[:default_solr_params][:rows]).items end it 'should get number of results per page requested' do num_results = 3 # non-default value - (solr_response1, document_list1) = subject.search_results({ q: @all_docs_query, per_page: num_results }, {}, default_method_chain) + (solr_response1, document_list1) = subject.search_results({ q: @all_docs_query, per_page: num_results }, default_method_chain) expect(document_list1).to have(num_results).docs expect(solr_response1.docs).to have(num_results).docs end it 'should get number of rows requested' do num_results = 4 # non-default value - (solr_response1, document_list1) = subject.search_results({ q: @all_docs_query, rows: num_results }, {}, default_method_chain) + (solr_response1, document_list1) = subject.search_results({ q: @all_docs_query, rows: num_results }, default_method_chain) expect(document_list1).to have(num_results).docs expect(solr_response1.docs).to have(num_results).docs end it 'should skip appropriate number of results when requested - default per page' do page = 3 - (solr_response2, document_list2) = subject.search_results({ q: @all_docs_query, page: page }, {}, default_method_chain) + (solr_response2, document_list2) = subject.search_results({ q: @all_docs_query, page: page }, default_method_chain) expect(solr_response2.params[:start].to_i).to eq blacklight_config[:default_solr_params][:rows] * (page-1) end it 'should skip appropriate number of results when requested - non-default per page' do page = 3 num_results = 3 - (solr_response2a, document_list2a) = subject.search_results({ q: @all_docs_query, per_page: num_results, page: page }, {}, default_method_chain) + (solr_response2a, document_list2a) = subject.search_results({ q: @all_docs_query, per_page: num_results, page: page }, default_method_chain) expect(solr_response2a.params[:start].to_i).to eq num_results * (page-1) end it 'should have no results when prompted for page after last result' do big = 5000 - (solr_response3, document_list3) = subject.search_results({ q: @all_docs_query, rows: big, page: big }, {}, default_method_chain) + (solr_response3, document_list3) = subject.search_results({ q: @all_docs_query, rows: big, page: big }, default_method_chain) expect(document_list3).to have(0).docs expect(solr_response3.docs).to have(0).docs end @@ -946,12 +664,12 @@ def params it 'should show first results when prompted for page before first result' do # FIXME: should it show first results, or should it throw an error for view to deal w? # Solr throws an error for a negative start value - (solr_response4, document_list4) = subject.search_results({ q: @all_docs_query, page: '-1' }, {}, default_method_chain) + (solr_response4, document_list4) = subject.search_results({ q: @all_docs_query, page: '-1' }, default_method_chain) expect(solr_response4.params[:start].to_i).to eq 0 end it 'should have results available when asked for more than are in response' do big = 5000 - (solr_response5, document_list5) = subject.search_results({ q: @all_docs_query, rows: big, page: 1 }, {}, default_method_chain) + (solr_response5, document_list5) = subject.search_results({ q: @all_docs_query, rows: big, page: 1 }, default_method_chain) expect(solr_response5.docs).to have(document_list5.length).docs expect(solr_response5.docs).to have_at_least(1).doc end @@ -1098,13 +816,13 @@ def params # SPECS FOR SPELLING SUGGESTIONS VIA SEARCH describe "Searches should return spelling suggestions", :integration => true do it 'search results for just-poor-enough-query term should have (multiple) spelling suggestions' do - (solr_response, document_list) = subject.search_results({ q: 'boo' }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: 'boo' }, default_method_chain) expect(solr_response.spelling.words).to include('bon') expect(solr_response.spelling.words).to include('bod') #for multiple suggestions end it 'search results for just-poor-enough-query term should have multiple spelling suggestions' do - (solr_response, document_list) = subject.search_results({ q: 'politica' }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: 'politica' }, default_method_chain) expect(solr_response.spelling.words).to include('policy') # less freq expect(solr_response.spelling.words).to include('politics') # more freq expect(solr_response.spelling.words).to include('political') # more freq @@ -1117,17 +835,17 @@ def params end it "title search results for just-poor-enough query term should have spelling suggestions" do - (solr_response, document_list) = subject.search_results({ q: 'yehudiyam', qt: 'search', :"spellcheck.dictionary" => "title" }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: 'yehudiyam', qt: 'search', :"spellcheck.dictionary" => "title" }, default_method_chain) expect(solr_response.spelling.words).to include('yehudiyim') end it "author search results for just-poor-enough-query term should have spelling suggestions" do - (solr_response, document_list) = subject.search_results({ q: 'shirma', qt: 'search', :"spellcheck.dictionary" => "author" }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: 'shirma', qt: 'search', :"spellcheck.dictionary" => "author" }, default_method_chain) expect(solr_response.spelling.words).to include('sharma') end it "subject search results for just-poor-enough-query term should have spelling suggestions" do - (solr_response, document_list) = subject.search_results({ q: 'wome', qt: 'search', :"spellcheck.dictionary" => "subject" }, {}, default_method_chain) + (solr_response, document_list) = subject.search_results({ q: 'wome', qt: 'search', :"spellcheck.dictionary" => "subject" }, default_method_chain) expect(solr_response.spelling.words).to include('women') end @@ -1144,10 +862,7 @@ def params it "should return specified value for facet_field specified" do expect(subject.facet_limit_for("subject_topic_facet")).to eq blacklight_config.facet_fields["subject_topic_facet"].limit end - it "should generate proper solr param" do - expect(subject.solr_search_params({}, default_method_chain)[:"f.subject_topic_facet.facet.limit"]).to eq 21 - end - + it "facet_limit_hash should return hash with key being facet_field and value being configured limit" do # facet_limit_hash has been removed from solrhelper in refactor. should it go back? skip "facet_limit_hash has been removed from solrhelper in refactor. should it go back?" @@ -1157,7 +872,6 @@ def params it "should handle no facet_limits in config" do blacklight_config.facet_fields = {} expect(subject.facet_limit_for("subject_topic_facet")).to be_nil - expect(subject.solr_search_params({}, default_method_chain)).not_to have_key(:"f.subject_topic_facet.facet.limit") end describe "for 'true' configured values" do @@ -1188,19 +902,6 @@ def params end end - describe "with max per page enforced" do - let(:blacklight_config) do - config = Blacklight::Configuration.new - config.max_per_page = 123 - return config - end - - it "should enforce max_per_page against all parameters" do - expect(blacklight_config.max_per_page).to eq 123 - expect(subject.solr_search_params({ per_page: 98765 }, default_method_chain)[:rows]).to eq 123 - end - end - describe "#get_solr_response_for_field_values" do before do @mock_response = double() @@ -1237,7 +938,8 @@ def params # nearby on shelf it "should raise a Blacklight exception if RSolr can't connect to the Solr instance" do allow(blacklight_solr).to receive(:send_and_receive).and_raise(Errno::ECONNREFUSED) - expect { subject.query_solr({}, {}, default_method_chain) }.to raise_exception(/Unable to connect to Solr instance/) + expect(Deprecation).to receive(:warn) + expect { subject.query_solr }.to raise_exception(/Unable to connect to Solr instance/) end describe "grouped_key_for_results" do @@ -1262,7 +964,7 @@ def params describe "#get_previous_and_next_documents_for_search" do before do - @full_response, @all_docs = subject.search_results({ q: '' }, { rows: 1000 }, default_method_chain) + @full_response, @all_docs = subject.search_results({ q: '', per_page: '100' }, default_method_chain) end it "should return the previous and next documents for a search" do diff --git a/spec/views/catalog/_paginate_compact.html.erb_spec.rb b/spec/views/catalog/_paginate_compact.html.erb_spec.rb index 70d67b9ead..52ea9cf691 100644 --- a/spec/views/catalog/_paginate_compact.html.erb_spec.rb +++ b/spec/views/catalog/_paginate_compact.html.erb_spec.rb @@ -18,7 +18,7 @@ def facet_limit_for *args include Blacklight::SolrHelper it "should render solr responses" do - solr_response, document_list = search_results({ q: '' }, {}, CatalogController.solr_search_params_logic) + solr_response, document_list = search_results({ q: '' }, CatalogController.solr_search_params_logic) render :partial => 'catalog/paginate_compact', :object => solr_response expect(rendered).to have_selector ".page_entries" diff --git a/spec/views/catalog/index.atom.builder_spec.rb b/spec/views/catalog/index.atom.builder_spec.rb index a00be254ab..f240fb04d5 100644 --- a/spec/views/catalog/index.atom.builder_spec.rb +++ b/spec/views/catalog/index.atom.builder_spec.rb @@ -15,7 +15,7 @@ # run a solr query to get our data c = CatalogController.new c.blacklight_config = @config - @response, @document_list = c.search_results(@params, {}, c.solr_search_params_logic) + @response, @document_list = c.search_results(@params, c.solr_search_params_logic) # munge the solr response to match test expectations @document_list[1] = SolrDocument.new(@document_list[1].with_indifferent_access.reject! { |k,v| k == "author_display" })