-
Notifications
You must be signed in to change notification settings - Fork 3
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
326 provide basic search functionality #459
base: master
Are you sure you want to change the base?
Changes from all commits
d1f09f1
7f0dddc
e38f98d
6bb9b2e
1771a0c
d29bf59
0b84acc
b5a73eb
7911016
ff264d5
53c97a1
f25fae6
cf77217
bba8e25
97e5dbb
4b4f008
605bc17
4febae6
768e433
ea14b10
92a5b38
547dc78
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ostruct' | ||
|
||
# Returns a search result to the GraphQL API | ||
class SearchResolver | ||
def call(root, arguments, _context) | ||
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. C: Assignment Branch Condition size for call is too high. [17.26/15] |
||
result = search_index(root[:query], | ||
map_categories_to_indices(arguments[:categories])) | ||
OpenStruct.new( | ||
entries: map_entries_to_models(result), | ||
count: OpenStruct.new( | ||
all: result.size, | ||
organizational_units: organizational_units_count(result), | ||
repositories: repositories_count(result) | ||
) | ||
) | ||
end | ||
|
||
def search_index(query, indices) | ||
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. C: Method has too many lines. [11/10] |
||
indices.map do |index| | ||
index.query(multi_match: {query: query, | ||
fuzziness: 'auto', | ||
fields: %i( | ||
display_name | ||
slug | ||
name | ||
description | ||
)}).entries | ||
end.flatten | ||
end | ||
|
||
def map_categories_to_indices(categories) | ||
if categories.blank? | ||
[::Index::RepositoryIndex::Repository, | ||
::Index::OrganizationIndex::Organization, | ||
::Index::UserIndex::User] | ||
else | ||
reduce_categories(categories) | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
def reduce_categories(categories) | ||
# rubocop:enable Metrics/MethodLength | ||
categories.reduce([]) do |indices, category| | ||
case category | ||
when 'organizationalUnits' | ||
indices + [::Index::OrganizationIndex::Organization, | ||
::Index::UserIndex::User] | ||
when 'repositories' | ||
indices + [::Index::RepositoryIndex::Repository] | ||
else | ||
indices | ||
end | ||
end | ||
end | ||
|
||
def organizational_units_count(result) | ||
result.count do |element| | ||
elem = element._data['_index'] | ||
elem == 'organization' || elem == 'user' | ||
end | ||
end | ||
|
||
def repositories_count(result) | ||
result.count do |element| | ||
element._data['_index'] == 'repository' | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
def map_entries_to_models(result) | ||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | ||
result.map do |element| | ||
OpenStruct.new( | ||
ranking: element._data['_score'], | ||
entry: | ||
if element._data['_type'] == 'user' | ||
User.first(slug: element.attributes['slug']) | ||
elsif element._data['_type'] == 'repository' | ||
Repository.first(slug: element.attributes['slug']) | ||
else | ||
Organization.first(slug: element.attributes['slug']) | ||
end | ||
) | ||
end | ||
end | ||
end | ||
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. E: unexpected token kEND (Using Ruby 2.5 parser; configure using |
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.
E: class definition in method body (Using Ruby 2.5 parser; configure using
TargetRubyVersion
parameter, underAllCops
)