Skip to content

Commit

Permalink
Replace !raw query parser with !term for field-appropriate analysis o…
Browse files Browse the repository at this point in the history
…f facet queries
  • Loading branch information
cbeer committed Oct 18, 2015
1 parent 70820e0 commit 8cb787c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def default_values
#fl: '*',
## this is a fancy way to say "find the document by id using
## the value in the id query parameter"
#q: "{!raw f=#{unique_key} v=$id}",
#q: "{!term f=#{unique_key} v=$id}",
## disable features we don't need
#facet: false,
#rows: 1
Expand Down
26 changes: 12 additions & 14 deletions lib/blacklight/solr/search_builder_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,21 @@ def facet_value_to_fq_string(facet_field, value)
# exclude all documents if the custom facet key specified was not found
'-*:*'
end
when (facet_config and facet_config.date)
# in solr 3.2+, this could be replaced by a !term query
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value)}"
when (value.is_a?(DateTime) or value.is_a?(Time))
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value.utc.strftime("%Y-%m-%dT%H:%M:%SZ"))}"
when value.is_a?(Date)
# rubocop:disable Rails/Date
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value.to_time(:local).strftime("%Y-%m-%dT%H:%M:%SZ"))}"
# rubocop:enable Rails/Date
when (value.is_a?(TrueClass) or value.is_a?(FalseClass) or value == 'true' or value == 'false'),
(value.is_a?(Integer) or (value.to_i.to_s == value if value.respond_to? :to_i)),
(value.is_a?(Float) or (value.to_f.to_s == value if value.respond_to? :to_f))
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value.to_s)}"
when value.is_a?(Range)
"#{prefix}#{solr_field}:[#{value.first} TO #{value.last}]"
else
"{!raw f=#{solr_field}#{(" " + local_params.join(" ")) unless local_params.empty?}}#{value}"
"{!term f=#{solr_field}#{(" " + local_params.join(" ")) unless local_params.empty?}}#{convert_to_term_value(value)}"
end
end

def convert_to_term_value(value)
case
when (value.is_a?(DateTime) or value.is_a?(Time))
"#{value.utc.strftime("%Y-%m-%dT%H:%M:%SZ")}"
when value.is_a?(Date)
"#{value.to_time(:local).strftime("%Y-%m-%dT%H:%M:%SZ")}"
else
value.to_s
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/blacklight/templates/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class <%= controller_name.classify %>Controller < ApplicationController
# ## These are hard-coded in the blacklight 'document' requestHandler
# # :fl => '*',
# # :rows => 1
# # :q => '{!raw f=id v=$id}'
# # :q => '{!term f=id v=$id}'
#}

# solr field configuration for search results/index views
Expand Down
2 changes: 1 addition & 1 deletion solr/conf/solrconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
<str name="echoParams">all</str>
<str name="fl">*</str>
<str name="rows">1</str>
<str name="q">{!raw f=id v=$id}</str> <!-- use id=666 instead of q=id:666 -->
<str name="q">{!term f=id v=$id}</str> <!-- use id=666 instead of q=id:666 -->
</lst>
</requestHandler>

Expand Down
36 changes: 18 additions & 18 deletions spec/lib/blacklight/solr/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
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}")
expect(subject[:fq]).to include("{!term f=#{single_facet.keys[0]}}#{value}")
end
end
end
Expand All @@ -179,7 +179,7 @@
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}" )
expect(subject[:fq]).to include("{!term f=#{facet_field}}#{value}" )
end
end

Expand All @@ -193,7 +193,7 @@
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}" )
expect(subject[:fq]).to include("{!term f=#{facet_field}}#{value}" )
end
end
expect(subject[:q]).to eq mult_word_query
Expand Down Expand Up @@ -348,57 +348,57 @@
describe "#facet_value_to_fq_string" do
it "should use the configured field name" do
blacklight_config.add_facet_field :facet_key, field: "facet_name"
expect(subject.send(:facet_value_to_fq_string, "facet_key", "my value")).to eq "{!raw f=facet_name}my value"
expect(subject.send(:facet_value_to_fq_string, "facet_key", "my value")).to eq "{!term f=facet_name}my value"
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!term f=facet_name}my value"
end

it "should pass booleans through" do
expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "facet_name:true"
expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq '{!term f=facet_name}true'
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq '{!term f=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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq '{!term f=facet_name}1'
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq '{!term f=facet_name}1'
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq '{!term f=facet_name}1.11'
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq '{!term f=facet_name}1.11'
end

it "should escape negative integers" do
expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq "facet_name:\\-1"
expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq '{!term f=facet_name}-1'
end

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, :field => 'facet_name'))

expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq '{!term f=facet_name}2012-01-01'
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, :field => 'facet_name'))

expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq '{!term f=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, :field => 'facet_name'))
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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq '{!term f=facet_name}2003-04-09T00:00:00Z'
end

it "should handle range requests" do
Expand All @@ -408,8 +408,8 @@
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, :field => 'facet_name'))

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"
expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!term f=facet_name tag=asdf}true"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!term f=facet_name tag=asdf}my value"
end
end

Expand Down

0 comments on commit 8cb787c

Please sign in to comment.