-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c9af3d
commit 927c197
Showing
15 changed files
with
294 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$(document).on('ready page:load', function() { | ||
var terms = new Bloodhound({ | ||
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), | ||
queryTokenizer: Bloodhound.tokenizers.whitespace, | ||
remote: { | ||
url: '/suggest?q=%QUERY' | ||
} | ||
}); | ||
|
||
terms.initialize(); | ||
|
||
$('input.search_q').typeahead({ | ||
hint: true, | ||
highlight: true, | ||
minLength: 2 | ||
}, | ||
{ | ||
name: 'terms', | ||
displayKey: 'term', | ||
source: terms.ttAdapter() | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.twitter-typeahead { | ||
float: left; | ||
width: 100%; | ||
z-index: 10000; | ||
|
||
.tt-input.form-control { | ||
width: 100%; | ||
} | ||
|
||
.tt-hint.form-control { | ||
width: 100%; | ||
} | ||
|
||
.tt-dropdown-menu { | ||
@extend .dropdown-menu; | ||
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; | ||
|
||
width: 100%; | ||
|
||
.tt-suggestion p{ | ||
font-size: 14px; | ||
padding-left: 10px; | ||
} | ||
|
||
.tt-cursor { | ||
background-color: $dropdown-link-hover-bg; | ||
color: $dropdown-link-hover-color; | ||
text-decoration: none; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class SuggestController < ApplicationController | ||
include Earthworks::Suggest | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Earthworks | ||
module Suggest | ||
extend ActiveSupport::Concern | ||
include Suggest::SearchHelper | ||
|
||
## | ||
# Get suggestion results from the Solr index | ||
def index | ||
@response = get_suggestions params | ||
respond_to do |format| | ||
format.json do | ||
render json: @response.suggestions | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Earthworks | ||
module Suggest | ||
class Response | ||
attr_reader :response, :request_params | ||
|
||
## | ||
# Creates a suggest response | ||
# @param [RSolr::HashWithResponse] response | ||
# @param [Hash] request_params | ||
def initialize(response, request_params) | ||
@response = response | ||
@request_params = request_params | ||
end | ||
|
||
## | ||
# Trys the suggestor response to return suggestions if they are | ||
# present | ||
# @return [Array] | ||
def suggestions | ||
response.try(:[], 'suggest').try(:[], 'mySuggester').try(:[], request_params[:q]).try(:[], 'suggestions') || [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Earthworks | ||
module Suggest | ||
module SearchHelper | ||
extend ActiveSupport::Concern | ||
include Blacklight::SearchHelper | ||
|
||
## | ||
# For now, only use the q parameter to create a | ||
# Earthworks::Suggest::Response | ||
# @param [Hash] params | ||
# @return [Earthworks::Suggest::Response] | ||
def get_suggestions(params) | ||
request_params = { q: params[:q] } | ||
Earthworks::Suggest::Response.new suggest_results(request_params), request_params | ||
end | ||
|
||
## | ||
# Query the suggest handler using RSolr::Client::send_and_receive | ||
# @param [Hash] request_params | ||
# @return [RSolr::HashWithResponse] | ||
def suggest_results(request_params) | ||
repository.connection.send_and_receive('suggest', params: request_params) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
describe SuggestController do | ||
describe 'GET index' do | ||
it 'assigns @response' do | ||
get :index, format: 'json' | ||
expect(assigns(:response)).to be_an Earthworks::Suggest::Response | ||
end | ||
it 'renders json' do | ||
get :index, format: 'json' | ||
expect(response.body).to eq [].to_json | ||
end | ||
it 'returns suggestions' do | ||
get :index, format: 'json', q: 'st' | ||
json = JSON.parse(response.body) | ||
expect(json.count).to eq 2 | ||
expect(json.first['term']).to eq 'stanford' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'rails_helper' | ||
|
||
describe Earthworks::Suggest::Response do | ||
let(:empty_response) { Earthworks::Suggest::Response.new({}, q: 'hello') } | ||
let(:response) do | ||
Earthworks::Suggest::Response.new( | ||
{ | ||
'responseHeader' => { | ||
'status' => 0, | ||
'QTime' => 42 | ||
}, | ||
'suggest' => { | ||
'mySuggester' => { | ||
'st' => { | ||
'numFound' => 2, | ||
'suggestions' => [ | ||
{ | ||
'term' => 'stanford', | ||
'weight' => 3, | ||
'payload' => '' | ||
}, | ||
{ | ||
'term' => 'statistics', | ||
'weight' => 1, | ||
'payload' => '' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
q: 'st' | ||
) | ||
end | ||
|
||
describe '#initialize' do | ||
it 'creates a Earthworks::Suggest::Response' do | ||
expect(empty_response).to be_an Earthworks::Suggest::Response | ||
end | ||
end | ||
describe '#suggestions' do | ||
it 'returns an array of suggestions' do | ||
expect(response.suggestions).to be_an Array | ||
expect(response.suggestions.count).to eq 2 | ||
expect(response.suggestions.first['term']).to eq 'stanford' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'rails_helper' | ||
|
||
describe Earthworks::Suggest::SearchHelper do | ||
|
||
class SearchHelperTestClass | ||
include Earthworks::Suggest::SearchHelper | ||
|
||
attr_accessor :blacklight_config | ||
attr_accessor :repository | ||
|
||
def initialize blacklight_config, conn | ||
self.blacklight_config = blacklight_config | ||
self.repository = Blacklight::SolrRepository.new(blacklight_config) | ||
self.repository.connection = conn | ||
end | ||
|
||
def params | ||
{} | ||
end | ||
end | ||
|
||
subject { SearchHelperTestClass.new blacklight_config, blacklight_solr } | ||
|
||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
let(:copy_of_catalog_config) { ::CatalogController.blacklight_config.deep_copy } | ||
let(:blacklight_solr) { RSolr.connect(Blacklight.connection_config) } | ||
|
||
|
||
|
||
describe '#get_suggestions' do | ||
it 'returns a Earthworks::Suggest::Response' do | ||
expect(subject.get_suggestions q: 'test').to be_an Earthworks::Suggest::Response | ||
end | ||
end | ||
describe '#suggest_results' do | ||
it 'queries the suggest handler with params' do | ||
allow(blacklight_solr).to receive(:get) do |path, params| | ||
expect(path).to eq 'suggest' | ||
expect(params).to eq params: { q: 'st' } | ||
end | ||
subject.query_solr q: 'st' | ||
end | ||
end | ||
end |