Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

advanced search updates #286

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ class CatalogController < ApplicationController
}
end

config.add_search_field('subject', label: :'blacklight.search.fields.subject') do |field|
config.add_search_field('full_text', label: :'blacklight.search.fields.full_text') do |field|
field.solr_parameters = {
qf: 'subject_tesim',
pf: ''
qf: 'all_text_timv',
pf: 'all_text_timv'
}
end

Expand Down
40 changes: 39 additions & 1 deletion app/search_builders/spot/catalog_search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@ class CatalogSearchBuilder < ::Hyrax::CatalogSearchBuilder
include BlacklightAdvancedSearch::AdvancedSearchBuilder
include BlacklightRangeLimit::RangeLimitBuilder

self.default_processor_chain += [:add_advanced_search_to_solr]
class_attribute :join_fields
self.join_fields = %w[all_fields full_text advanced]
self.default_processor_chain += [:add_advanced_parse_q_to_solr, :add_advanced_search_to_solr]

def add_advanced_parse_q_to_solr(solr_parameters)
super(adv_params = {})

solr_parameters[:q] = [solr_parameters[:q], adv_params[:q]].reject(&:blank?).join(' ')
solr_parameters[:defType] = adv_params[:defType]
end

# Rewrites +BlacklightAdvancedSearch::AdvancedSearch#add_advanced_search_to_solr+
# so that a pre-existing +solr_parameters[:q]+ value isn't truncated by the
# generated advanced query.
#
# @param [Hash<Symbol => *] solr_parameters
# @return [void]
def add_advanced_search_to_solr(solr_parameters)
return unless is_advanced_search?
parsed = BlacklightAdvancedSearch::QueryParser.new(blacklight_params, blacklight_config).to_solr

solr_parameters[:q] = [solr_parameters[:q], parsed[:q]].reject(&:blank?).join(' ')
solr_parameters[:fq] ||= []
solr_parameters[:fq] += parsed[:fq]
end

# Overridden from +Hyrax::CatalogSearchBuilder+ to expand the search fields
# that are used to determine when to join file-sets. Also ensures that a
# pre-existing +solr_parameters[:q]+ value isn't truncated by the join query.
#
# @param [Hash<Symbol => *] solr_parameters
# @retrun [void]
def show_works_or_works_that_contain_files(solr_parameters)
return if blacklight_params[:q].blank? || !join_fields.include?(blacklight_params[:search_field])

solr_parameters[:user_query] = blacklight_params[:q]
solr_parameters[:q] = [solr_parameters[:q], new_query].reject(&:blank?).join(' ')
solr_parameters[:defType] = 'lucene'
end
end
end
8 changes: 8 additions & 0 deletions app/views/advanced/_advanced_search_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%- search_fields_for_advanced_search.each do |key, field_def| -%>
<div class="form-group advanced-search-field">
<%= label_tag key, t(field_def.label), class: 'col-sm-3 control-label' %>
<div class="col-sm-9">
<%= text_field_tag key, label_tag_default_for(key), class: 'form-control' %>
</div>
</div>
<%- end -%>
4 changes: 2 additions & 2 deletions config/locales/blacklight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ en:
visibility: Visibility
years_encompassed: Year

search:
all_fields: All fields
all_fields: All fields
full_text: Full text

sort:
date_added:
Expand Down
77 changes: 77 additions & 0 deletions spec/search_builders/spot/catalog_search_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true
RSpec.describe Spot::CatalogSearchBuilder do
subject(:builder) { described_class.new([], scope).with(blacklight_params) }

let(:scope) { OpenStruct.new(blacklight_config: CatalogController.blacklight_config) }
let(:blacklight_parameters) { params }
let(:params) { { search_field: 'advanced' } }
let(:solr_parameters) { {} }

describe '#add_advanced_parse_q_to_solr' do
before { builder.add_advanced_parse_q_to_solr(solr_parameters) }

# note: the spaces after !dismax are expected
let(:parsed_q) { '_query_:"{!dismax }+q +me" AND NOT _query_:"{!dismax }you"' }
let(:blacklight_params) { { q: 'q AND me NOT you', search_field: 'title' } }

context 'when the only query to exist' do
it 'adds the parsed query to solr_parameters[:q]' do
expect(solr_parameters[:q]).to eq parsed_q
end
end

context 'when not the only query' do
let(:solr_parameters) { { q: 'a previous example' } }

it 'appends the value (rather than truncates)' do
expect(solr_parameters[:q]).to include parsed_q
expect(solr_parameters[:q]).to include 'a previous example'
end
end
end

describe '#add_advanced_search_to_solr' do
before { builder.add_advanced_search_to_solr(solr_parameters) }

let(:blacklight_params) { params.merge(title: 'a cool query AND thing') }

context 'when the only query to exist' do
it 'adds the value as _query_' do
expect(solr_parameters[:q].scan(/_query_:/).size).to eq 1
end
end

context 'when another query already exists' do
let(:solr_parameters) { { q: '_query_:"{!dismax title_tesim}first-search"' } }

it 'appends another _query_ value' do
expect(solr_parameters[:q].scan(/_query_/).size).to eq 2
end
end
end

describe '#show_works_or_works_that_contain_files' do
before { builder.show_works_or_works_that_contain_files(solr_parameters) }

let(:blacklight_params) { params.merge(q: 'a good search') }

context 'when the only query to exist' do
it 'moves blacklight_params[:q] to :user_query' do
expect(solr_parameters[:user_query]).to eq blacklight_params[:q]
end

it 'adds the {!lucene}_query_ syntax to solr_parameters[:q]' do
expect(solr_parameters[:q]).to include '_query_'
expect(solr_parameters[:q]).to include '$user_query'
end
end

context 'when another query already exists' do
let(:solr_parameters) { { q: '_query_:cool+beans' } }

it 'appends the pre-existing value' do
expect(solr_parameters[:q]).to include '_query_:cool+beans'
end
end
end
end