Skip to content
Merged
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
8 changes: 7 additions & 1 deletion app/assets/stylesheets/partials/_facets.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
margin-bottom: 0;
}

.category {
margin-bottom: 1em;
}

ul.category-terms {
border-collapse: separate;
border-spacing: 0px 4px;
display: table;
margin-bottom: 0;
table-layout: fixed;
width: 100%;

Expand All @@ -18,7 +23,8 @@
text-decoration: none;

&:hover,
&:focus {
&:focus,
&.applied {
background: #000;
color: #fff;
}
Expand Down
16 changes: 1 addition & 15 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ def extract_errors(response)
end

def extract_facets(response)
facets = response&.data&.search&.to_h&.dig('aggregations')

facets&.map { |k, v| { facet_labels[k] => v } }
response&.data&.search&.to_h&.dig('aggregations')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fits my brain so much better. Thanks so much for talking that through with me so we both understood a bit better what was going on and why here :)

end

def extract_results(response)
response&.data&.search&.to_h&.dig('records')
end

def facet_labels
{
'contentFormat' => 'Formats',
'contentType' => 'Content types',
'contributors' => 'Contributors',
'languages' => 'Languages',
'literaryForm' => 'Literary forms',
'source' => 'Sources',
'subjects' => 'Subjects'
}
end

def validate_q!
return if params[:advanced]&.strip.present?
return if params[:q]&.strip.present?
Expand Down
22 changes: 22 additions & 0 deletions app/helpers/facet_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module FacetHelper
def add_facet(query, facet, term)
new_query = query.clone
new_query[:page] = 1
new_query[facet.to_sym] = term
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay with the facets we currently are displaying because they are all "only one makes sense" kinda facets. We may need to revisit this in the future to ensure this doesn't remove facets that are applied during a drill down application (i.e. search > apply "foo" from group A > apply "bar" from group A... is the result just "bar" applied or are both being preserved in the query with this code?

Even if this only works for our currently displayed types of facets, I'm okay with coming back to this later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this work is predicated on only one value being applicable from a given vocabulary - so I'm not pushing values onto an array, or purging only one value, etc.

This work does support applying facets from multiple vocabularies, which I think you've noted somewhere in these conversations, but more work will be needed if we get into dealing with arrays of values.

new_query
end

def nice_labels
{
'contentType' => 'Content types',
'source' => 'Sources'
}
end

def remove_facet(query, facet)
new_query = query.clone
new_query[:page] = 1
new_query.delete facet.to_sym
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my note above. I think this works with contentType really well as it's a one value facet, but with facets in which multiple tiers of filtering can happen (such as subjects) this probably won't hold up. I'm okay with dealing with this later, but wanted to share the concern so we aren't surprised later if this needs to get more complex.

new_query
end
end
2 changes: 1 addition & 1 deletion app/models/enhancer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Enhancer
attr_accessor :enhanced_query

QUERY_PARAMS = %i[q citation contributors fundingInformation identifiers locations subjects title].freeze
QUERY_PARAMS = %i[q citation contentType contributors fundingInformation identifiers locations subjects title].freeze
FILTER_PARAMS = [:source].freeze
# accepts all params as each enhancer may require different data
def initialize(params)
Expand Down
2 changes: 1 addition & 1 deletion app/models/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class QueryBuilder
attr_reader :query

RESULTS_PER_PAGE = 20
QUERY_PARAMS = %w[q citation contributors fundingInformation identifiers locations subjects title].freeze
QUERY_PARAMS = %w[q citation contentType contributors fundingInformation identifiers locations subjects title].freeze
FILTER_PARAMS = [:source].freeze

def initialize(enhanced_query)
Expand Down
22 changes: 2 additions & 20 deletions app/models/timdex_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TimdexSearch < TimdexBase
$title: String
$sourceFacet: [String!]
$from: String
$contentType: String
) {
search(
searchterm: $q
Expand All @@ -26,6 +27,7 @@ class TimdexSearch < TimdexBase
title: $title
sourceFacet: $sourceFacet
from: $from
contentTypeFacet: $contentType
) {
hits
records {
Expand All @@ -51,34 +53,14 @@ class TimdexSearch < TimdexBase
}
}
aggregations {
contentFormat {
key
docCount
}
contentType {
key
docCount
}
contributors {
key
docCount
}
languages {
key
docCount
}
literaryForm {
key
docCount
}
source {
key
docCount
}
subjects {
key
docCount
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions app/views/search/_facet.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<% return if facet.values[0].empty? %>
<% return if values.empty? %>

<div class="category">
<h3><%= facet.keys[0] %></h3>
<h3><%= nice_labels[category] || category %></h3>
<ul class="category-terms list-unbulleted">
<% facet.values[0].each do |term| %>
<% values.each do |term| %>
<li class="term">
<a href="#">
<a href="<%= results_path(add_facet(@enhanced_query, category, term['key'])) %>" class="<%= "applied" if @enhanced_query[category.to_sym] == term['key'] %>">
<span class="name"><%= term['key'] %></span>
<span class="count"><%= term['docCount'] %> <span class="sr">records</span></span>
</a>
</li>
<% end %>
</ul>
<% if @enhanced_query[category.to_sym].present? %>
<div><%= link_to "Show all #{nice_labels[category]&.downcase || category}", results_path(remove_facet(@enhanced_query, category)) %>
</div>
<% end %>
</div>
43 changes: 24 additions & 19 deletions app/views/search/results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@

<p>Showing results for:
<ul>
<% if params[:q].present? %>
<li>Keyword anywhere: <%= params[:q] %></li>
<% if @enhanced_query[:q].present? %>
<li>Keyword anywhere: <%= @enhanced_query[:q] %></li>
<% end %>
<% if params[:citation].present? %>
<li>Citation: <%= params[:citation] %></li>
<% if @enhanced_query[:citation].present? %>
<li>Citation: <%= @enhanced_query[:citation] %></li>
<% end %>
<% if params[:contributors].present? %>
<li>Contributors: <%= params[:contributors] %></li>
<% if @enhanced_query[:contentType].present? %>
<li>Content type: <%= @enhanced_query[:contentType] %></li>
<% end %>
<% if params[:fundingInformation].present? %>
<li>Funders: <%= params[:fundingInformation] %></li>
<% if @enhanced_query[:contributors].present? %>
<li>Contributors: <%= @enhanced_query[:contributors] %></li>
<% end %>
<% if params[:identifiers].present? %>
<li>Identifiers: <%= params[:identifiers] %></li>
<% if @enhanced_query[:fundingInformation].present? %>
<li>Funders: <%= @enhanced_query[:fundingInformation] %></li>
<% end %>
<% if params[:locations].present? %>
<li>Locations: <%= params[:locations] %></li>
<% if @enhanced_query[:identifiers].present? %>
<li>Identifiers: <%= @enhanced_query[:identifiers] %></li>
<% end %>
<% if params[:subjects].present? %>
<li>Subjects: <%= params[:subjects] %></li>
<% if @enhanced_query[:locations].present? %>
<li>Locations: <%= @enhanced_query[:locations] %></li>
<% end %>
<% if params[:title].present? %>
<li>Title: <%= params[:title] %></li>
<% if @enhanced_query[:subjects].present? %>
<li>Subjects: <%= @enhanced_query[:subjects] %></li>
<% end %>
<% if params[:source].present? %>
<li>Source: <%= params[:source] %></li>
<% if @enhanced_query[:title].present? %>
<li>Title: <%= @enhanced_query[:title] %></li>
<% end %>
<% if @enhanced_query[:source].present? %>
<li>Source: <%= @enhanced_query[:source] %></li>
<% end %>
</ul>
</p>
Expand All @@ -55,7 +58,9 @@
<aside class="col1q facet-container">
<div id="facets">
<h2>Available filters</h2>
<%= render(partial: 'search/facet', collection: @facets) || render('facet_empty') %>
<% @facets&.each do |category, values| %>
<%= render(partial: 'search/facet', locals: {category: category, values: values}) %>
<% end %>
</div>
</aside>
</div>
Expand Down
67 changes: 67 additions & 0 deletions test/helpers/facet_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'test_helper'

class FacetHelperTest < ActionView::TestCase
include FacetHelper

test 'add_facet will support adding a facet parameter to a search URL' do
original_query = {
page: 1,
q: 'data'
}
expected_query = {
page: 1,
q: 'data',
contentType: 'dataset'
}
assert_equal expected_query, add_facet(original_query, 'contentType', 'dataset')
end

test 'add_facet will reset a page count when called' do
original_query = {
page: 3,
q: 'data'
}
expected_query = {
page: 1,
q: 'data',
contentType: 'dataset'
}
assert_equal expected_query, add_facet(original_query, 'contentType', 'dataset')
end

test 'nice_labels allows translation of machine categories to human readable headings' do
needle = 'contentType'
assert_equal 'Content types', nice_labels[needle]
end

test 'nice_labels returns nil if a category is not mapped yet' do
needle = 'foo'
assert_nil nice_labels[needle]
end

test 'remove_facet will remove a specific facet parameter from a search URL' do
original_query = {
page: 1,
q: 'data',
contentType: 'dataset'
}
expected_query = {
page: 1,
q: 'data'
}
assert_equal expected_query, remove_facet(original_query, 'contentType')
end

test 'remove_facet will reset a page count when called' do
original_query = {
page: 3,
q: 'data',
contentType: 'dataset'
}
expected_query = {
page: 1,
q: 'data'
}
assert_equal expected_query, remove_facet(original_query, 'contentType')
end
end
27 changes: 13 additions & 14 deletions test/vcr_cassettes/advanced.yml

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions test/vcr_cassettes/advanced_all.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading