Skip to content

Commit

Permalink
Merge pull request #3073 from sandbergja/backport-3066
Browse files Browse the repository at this point in the history
Backport #3066
  • Loading branch information
tpendragon authored Sep 1, 2023
2 parents 94c8fa3 + a6f2e81 commit f6bdb20
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def header_component
# @since v5.2.0
# @return [String] The url path (relative to the solr base url) to use when requesting only a single document
property :document_solr_path, default: 'get'
# @!attribute json_solr_path
# @since v7.34.0
# @return [String] The url path (relative to the solr base url) to use when using Solr's JSON Query DSL (as with the advanced search)
property :json_solr_path, default: nil
# @!attribute document_unique_id_param
# @since v5.2.0
# @return [Symbol] The solr query parameter used for sending the unique identifiers for one or more documents
Expand Down
16 changes: 14 additions & 2 deletions lib/blacklight/solr/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def find id, params = {}
# Execute a search query against solr
# @param [Hash] params solr query parameters
def search params = {}
send_and_receive blacklight_config.solr_path, params.reverse_merge(qt: blacklight_config.qt)
send_and_receive search_path(params), params.reverse_merge(qt: blacklight_config.qt)
end

# @param [Hash] request_params
Expand Down Expand Up @@ -77,7 +77,7 @@ def send_and_receive(path, solr_params = {})
# @return [Hash]
# @!visibility private
def build_solr_request(solr_params)
if solr_params[:json].present?
if uses_json_query_dsl?(solr_params)
{
data: { params: solr_params.to_hash.except(:json) }.merge(solr_params[:json]).to_json,
method: :post,
Expand Down Expand Up @@ -121,5 +121,17 @@ def defined_rsolr_timeout_exceptions
[]
end
end

# @return [String]
def search_path(solr_params)
return blacklight_config.json_solr_path if blacklight_config.json_solr_path && uses_json_query_dsl?(solr_params)

blacklight_config.solr_path
end

# @return [Boolean]
def uses_json_query_dsl?(solr_params)
solr_params[:json].present?
end
end
end
1 change: 1 addition & 0 deletions lib/generators/blacklight/templates/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class <%= controller_name.classify %>Controller < ApplicationController
# solr path which will be added to solr base url before the other solr params.
#config.solr_path = 'select'
#config.document_solr_path = 'get'
#config.json_solr_path = 'advanced'
# items to show per page, each number in the array represent another option to choose from.
#config.per_page = [10,20,50,100]
Expand Down
69 changes: 69 additions & 0 deletions lib/generators/blacklight/templates/solr/conf/solrconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,75 @@
</arr>
</requestHandler>

<requestHandler name="/advanced" class="solr.SearchHandler">
<!-- a lucene request handler for using the JSON Query DSL,
specifically for advanced search.
Using a separate requestHandler is a workaround to
https://issues.apache.org/jira/browse/SOLR-16916, although
it could be desirable for other reasons as well.
-->
<lst name="defaults">
<str name="defType">lucene</str>
<str name="echoParams">explicit</str>
<str name="df">title_tsim</str>
<str name="qf">
id
full_title_tsim
short_title_tsim
alternative_title_tsim
active_fedora_model_ssi
title_tsim
author_tsim
subject_tsim
all_text_timv
</str>
<str name="pf">
all_text_timv^10
</str>

<str name="author_qf">
author_tsim
</str>
<str name="author_pf">
</str>
<str name="title_qf">
title_tsim
full_title_tsim
short_title_tsim
alternative_title_tsim
</str>
<str name="title_pf">
</str>
<str name="subject_qf">
subject_tsim
</str>
<str name="subject_pf">
</str>

<str name="fl">
*,
score
</str>

<str name="facet">true</str>
<str name="facet.mincount">1</str>
<str name="facet.limit">10</str>
<str name="facet.field">active_fedora_model_ssi</str>
<str name="facet.field">subject_ssim</str>

<str name="spellcheck">true</str>
<str name="spellcheck.dictionary">default</str>
<str name="spellcheck.onlyMorePopular">true</str>
<str name="spellcheck.extendedResults">true</str>
<str name="spellcheck.collate">false</str>
<str name="spellcheck.count">5</str>

</lst>
<arr name="last-components">
<str>spellcheck</str>
</arr>
</requestHandler>

<requestHandler name="permissions" class="solr.SearchHandler" >
<lst name="defaults">
<str name="facet">off</str>
Expand Down
56 changes: 56 additions & 0 deletions spec/features/advanced_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@
RSpec.describe "Blacklight Advanced Search Form" do
describe "advanced search form" do
before do
CatalogController.blacklight_config.search_fields['all_fields']['clause_params'] = {
edismax: {}
}
CatalogController.blacklight_config.search_fields['author']['clause_params'] = {
edismax: { qf: '${author_qf}' }
}
CatalogController.blacklight_config.search_fields['title']['clause_params'] = {
edismax: { qf: '${title_qf}' }
}
CatalogController.blacklight_config.search_fields['subject']['clause_params'] = {
edismax: { qf: '${subject_qf}' }
}
CatalogController.blacklight_config.json_solr_path = 'advanced'
visit '/catalog/advanced?hypothetical_existing_param=true&q=ignore+this+existing+query'
end

after do
%w[all_fields author title subject].each do |field|
CatalogController.blacklight_config.search_fields[field].delete(:clause_params)
end
end

it "has field and facet blocks" do
expect(page).to have_selector('.query-criteria')
expect(page).to have_selector('.limit-criteria')
Expand Down Expand Up @@ -45,6 +64,43 @@
click_on 'advanced-search-submit'
expect(page).to have_content 'Remove constraint Title: Medicine'
expect(page).to have_content 'Strong Medicine speaks'
expect(page).to have_selector('article.document', count: 1)
end

it 'can limit to facets' do
fill_in 'Subject', with: 'Women'
click_on 'Language'
check 'Urdu 3'
click_on 'advanced-search-submit'
expect(page).to have_content 'Pākistānī ʻaurat dorāhe par'
expect(page).not_to have_content 'Ajikto kŭrŏk chŏrŏk sasimnikka : and 아직도 그럭 저럭 사십니까'
expect(page).to have_selector('article.document', count: 1)
end

it 'handles boolean queries' do
fill_in 'All Fields', with: 'history NOT strong'
click_on 'advanced-search-submit'
expect(page).to have_content('Ci an zhou bian')
expect(page).not_to have_content('Strong Medicine speaks')
expect(page).to have_selector('article.document', count: 10)
end

it 'handles queries in multiple fields with the ALL operator' do
fill_in 'All Fields', with: 'history'
fill_in 'Author', with: 'hearth'
click_on 'advanced-search-submit'
expect(page).to have_content('Strong Medicine speaks')
expect(page).to have_selector('article.document', count: 1)
end

it 'handles queries in multiple fields with the ANY operator' do
select 'any', from: 'op'
fill_in 'All Fields', with: 'history'
fill_in 'Subject', with: 'women'
click_on 'advanced-search-submit'
expect(page).to have_content('Ci an zhou bian')
expect(page).to have_content('Pākistānī ʻaurat dorāhe par')
expect(page).to have_selector('article.document', count: 10)
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/models/blacklight/solr/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,33 @@
expect(JSON.parse(actual_params[:data]).with_indifferent_access).to include(query: { bool: {} })
expect(actual_params[:headers]).to include({ 'Content-Type' => 'application/json' })
end

context "without a json solr path configured" do
before do
blacklight_config.json_solr_path = nil
end

it "uses the default solr path" do
blacklight_config.solr_path = 'xyz'
allow(subject.connection).to receive(:send_and_receive) do |path|
expect(path).to eq 'xyz'
end
subject.search(input_params)
end
end

context "with a json solr path configured" do
before do
blacklight_config.json_solr_path = 'my-great-json'
end

it "uses the configured json_solr_path" do
allow(subject.connection).to receive(:send_and_receive) do |path|
expect(path).to eq 'my-great-json'
end
subject.search(input_params)
end
end
end
end

Expand Down

0 comments on commit f6bdb20

Please sign in to comment.