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

Improve peopleLocations endpoint: people_locations method #3217

Merged
merged 11 commits into from
Aug 14, 2018
2 changes: 1 addition & 1 deletion app/services/execute_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def execute(type, search_criteria)
when :tags
sresult = sservice.textSearch_tags(search_criteria.query)
when :peoplelocations
sresult = sservice.recentPeople(search_criteria.query, search_criteria.tag)
sresult = sservice.people_locations(search_criteria.query, search_criteria.tag)
when :taglocations
if search_criteria.query.include? ","
sresult = sservice.tagNearbyNodes(search_criteria.query, search_criteria.tag)
Expand Down
31 changes: 10 additions & 21 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +181,18 @@ def tagNearbyNodes(srchString, tagName)
sresult
end

# GET X number of latest people/contributors and package up as a DocResult
# X = srchString
def recentPeople(_srchString, tagName = nil)
# Returns the location of people with most recent contributions.
# The method receives as parameter the number of results to be
# returned and as optional parameter a user tag. If the user tag
# is present, the method returns only the location of people
# with that specific user tag.
def people_locations(srchString, tagName = nil)
sresult = DocList.new

nodes = Node.all.order("changed DESC").limit(100).distinct
users = []
nodes.each do |node|
if node.author.status != 0
if tagName.blank?
users << node.author.user
else
users << node.author.user if node.author.user.has_tag(tagName)
end
end
end
users = users.uniq
users.each do |user|
next unless user.has_power_tag("lat") && user.has_power_tag("lon")
blurred = false
if user.has_power_tag("location")
blurred = user.get_value_of_power_tag("location")
end
user_scope = SrchScope.find_locations(srchString, tagName)

user_scope.each do |user|
blurred = user.has_power_tag("location") ? user.get_value_of_power_tag("location") : false
doc = DocResult.fromLocationSearch(user.id, 'people_coordinates', user.path, user.username, 0, 0, user.lat, user.lon, blurred)
sresult.addDoc(doc)
end
Expand Down
18 changes: 18 additions & 0 deletions app/services/srch_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,22 @@ def self.find_questions(input, limit, order)
.joins(:tag)
.where('term_data.name LIKE ?', 'question:%')
end

def self.find_locations(limit, user_tag = nil)
user_locations = User.where('rusers.status <> 0')\
.joins(:user_tags)\
.where('value LIKE "lat:%"')\
.includes(:revisions)\
.order("node_revisions.timestamp DESC")\
.distinct
if user_tag.present?
user_locations = User.joins(:user_tags)\
.where('user_tags.value LIKE ?', user_tag)\
.where(id: user_locations.select("rusers.id"))
end

user_locations = user_locations.limit(limit.to_i)

user_locations
end
end
2 changes: 1 addition & 1 deletion test/unit/helpers/search_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SearchHelperTest < ActionView::TestCase

test "search Recent People helper" do
sservice = SearchService.new
result = sservice.recentPeople(100)
result = sservice.people_locations(100)
assert_equal result.items.first.docType , "people_coordinates"
assert_equal result.items.first.docTitle , users(:bob).username
assert_equal result.items.first.docSummary , 0
Expand Down