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

Add resource search for autocomplete #37224

Merged
merged 5 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions dashboard/app/controllers/resources_controller.rb
@@ -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
12 changes: 12 additions & 0 deletions dashboard/lib/resources_autocomplete.rb
@@ -0,0 +1,12 @@
class ResourcesAutocomplete < AutocompleteHelper
Copy link
Member

Choose a reason for hiding this comment

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

nice find on 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Why + 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
20 changes: 20 additions & 0 deletions dashboard/test/fixtures/resource.yml
@@ -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
24 changes: 24 additions & 0 deletions dashboard/test/lib/resources_autocomplete_test.rb
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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