From 7a8aa509db40eebb327fb6f5b073c74a6c07301c Mon Sep 17 00:00:00 2001 From: milaaraujo Date: Fri, 20 Jul 2018 22:38:07 -0700 Subject: [PATCH 1/2] Version 1.0 --- app/api/srch/search.rb | 12 ++++++++++++ app/services/execute_search.rb | 2 ++ app/services/search_service.rb | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/api/srch/search.rb b/app/api/srch/search.rb index e91b51c39c..c329323cad 100644 --- a/app/api/srch/search.rb +++ b/app/api/srch/search.rb @@ -34,6 +34,18 @@ class Search < Grape::API Search.execute(:profiles, params) end + # Request URL should be /api/srch/recentprofiles?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] + desc 'Perform a search of profiles', hidden: false, + is_array: false, + nickname: 'srchGetRecentProfiles' + + params do + use :common + end + get :recentprofiles do + Search.execute(:recentprofiles, params) + end + # Request URL should be /api/srch/notes?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] # Basic implementation from classic plots2 SearchController desc 'Perform a search of research notes', hidden: false, diff --git a/app/services/execute_search.rb b/app/services/execute_search.rb index b2d72b0b7d..4e87b1c35e 100644 --- a/app/services/execute_search.rb +++ b/app/services/execute_search.rb @@ -13,6 +13,8 @@ def execute(type, search_criteria) sresult = sservice.textSearch_all(search_criteria.query) when :profiles sresult = sservice.textSearch_profiles(search_criteria.query) + when :recentprofiles + sresult = sservice.textSearch_recentProfiles(search_criteria.query) when :notes sresult = sservice.textSearch_notes(search_criteria.query) when :questions diff --git a/app/services/search_service.rb b/app/services/search_service.rb index c948548565..19cd9ab5a0 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -131,6 +131,32 @@ def textSearch_profiles(srchString) sresult end + ##################################################################################### + # Search for more recently updated profiles for matching text + def textSearch_recentProfiles(srchString) + sresult = DocList.new + + nodes = Node.all.order("changed DESC").limit(100).distinct + users = [] + nodes.each do |node| + next unless node.author.status != 0 + if node.author.name.downcase.include? srchString.downcase + users << node.author.user + end + end + + users = users.uniq + + # User profiles + users.each do |match| + doc = DocResult.fromSearch(0, 'user', '/profile/' + match.name, match.name, '', 0) + sresult.addDoc(doc) + end + + sresult + end + ##################################################################################### + # Search notes for matching strings def textSearch_notes(srchString) sresult = DocList.new @@ -242,7 +268,7 @@ def tagNearbyNodes(srchString, tagName) def recentPeople(srchString, tagName = nil) sresult = DocList.new - nodes = Node.all.order("changed DESC").limit(srchString).distinct + nodes = Node.all.order("changed DESC").limit(100).distinct users = [] nodes.each do |node| if node.author.status != 0 @@ -254,6 +280,7 @@ def recentPeople(srchString, tagName = nil) end end users = users.uniq + count = 0 # TODO: fazer de maneira mais bonita em Ruby users.each do |user| next unless user.has_power_tag("lat") && user.has_power_tag("lon") blurred = false @@ -262,6 +289,10 @@ def recentPeople(srchString, tagName = nil) end doc = DocResult.fromLocationSearch(user.id, 'people_coordinates', user.path, user.username, 0, 0, user.lat, user.lon, blurred) sresult.addDoc(doc) + count += 1 + if count == srchString.to_i + break + end end sresult From 4ab4f060855b4050f9ee5862783dc3a9460ca476 Mon Sep 17 00:00:00 2001 From: milaaraujo Date: Sat, 21 Jul 2018 16:55:11 -0700 Subject: [PATCH 2/2] Version 2 --- app/services/search_service.rb | 4 +--- test/functional/search_api_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 19cd9ab5a0..f384adb10f 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -131,7 +131,6 @@ def textSearch_profiles(srchString) sresult end - ##################################################################################### # Search for more recently updated profiles for matching text def textSearch_recentProfiles(srchString) sresult = DocList.new @@ -155,7 +154,6 @@ def textSearch_recentProfiles(srchString) sresult end - ##################################################################################### # Search notes for matching strings def textSearch_notes(srchString) @@ -280,7 +278,7 @@ def recentPeople(srchString, tagName = nil) end end users = users.uniq - count = 0 # TODO: fazer de maneira mais bonita em Ruby + count = 0 users.each do |user| next unless user.has_power_tag("lat") && user.has_power_tag("lon") blurred = false diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 074bea3e4a..bfff17778c 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -99,6 +99,30 @@ def app end + test 'search recent profiles functionality' do + get '/api/srch/recentprofiles?srchString=Jeff' + assert last_response.ok? + + # Expected search pattern + pattern = { + srchParams: { + srchString: 'Jeff', + seq: nil, + }.ignore_extra_keys! + }.ignore_extra_keys! + + matcher = JsonExpressions::Matcher.new(pattern) + + json = JSON.parse(last_response.body) + + assert_equal "/profile/jeff", json['items'][0]['docUrl'] + assert_equal "jeff", json['items'][0]['docTitle'] + assert_equal "user", json['items'][0]['docType'] + + assert matcher =~ json + + end + test 'search notes functionality' do get '/api/srch/notes?srchString=Blog' assert last_response.ok?