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
Add resource search for autocomplete #37224
Changes from 4 commits
fb0b31c
b121c20
a03fc35
c067d9d
5e7cec3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
class ResourcesController < ApplicationController | ||
# GET /resourcesearch/:q/:limit | ||
def search | ||
# TODO: use the query to provide real suggestions | ||
render json: Resource.limit(params[:limit]).map(&:attributes) | ||
render json: ResourcesAutocomplete.get_search_matches(params[:q], params[:limit]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class ResourcesAutocomplete < AutocompleteHelper | ||
def self.get_search_matches(query, limit) | ||
limit = format_limit(limit) | ||
|
||
rows = Resource.limit(limit) | ||
query = format_query(query) | ||
return [] if query.length < MIN_WORD_LENGTH + 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why + 2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The query adds a symbol on each side of the word. I'll add a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I'm going to move it up a line and not bother with the +2 thing |
||
rows = rows. | ||
where("MATCH(name,url) AGAINST(? in BOOLEAN MODE)", query) | ||
return rows.map(&:attributes) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource_101: | ||
id: 101 | ||
key: resource_101 | ||
name: Code Studio | ||
url: code.org | ||
resource_102: | ||
id: 102 | ||
key: resource_102 | ||
name: wiki | ||
url: wikipedia.org | ||
resource_103: | ||
id: 103 | ||
key: resource_103 | ||
name: class slides | ||
url: docs.google.com/slides | ||
resource_104: | ||
id: 104 | ||
key: resource_104 | ||
name: class site | ||
url: testsebsite.fake |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test_helper' | ||
|
||
class ResourcesAutocompleteTest < ActiveSupport::TestCase | ||
# We rely on fulltext indices to be used in this test. | ||
# In order to get this test to work, we need to have the resources created | ||
# as fixtures. These are defined in test/fixtures/resource.yml | ||
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for documenting. this strategy seems fine for now, though I'm a bit worried it will no longer work once we are only searching within a specific course version. |
||
|
||
test "finds resource with matching name" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a test for when there is less than 5 characters in the search? |
||
matches = ResourcesAutocomplete.get_search_matches('Studi', 5) | ||
assert_equal 1, matches.length | ||
assert_equal 'Code Studio', matches[0]['name'] | ||
end | ||
|
||
test "finds resource with matching url" do | ||
matches = ResourcesAutocomplete.get_search_matches("wikip", 5) | ||
assert_equal 1, matches.length | ||
assert_equal 'wiki', matches[0]['name'] | ||
end | ||
|
||
test "finds multiple matches" do | ||
matches = ResourcesAutocomplete.get_search_matches("class", 5) | ||
assert_equal 2, matches.length | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice find on AutocompleteHelper!