From 39b04c7d98ba3eefe8673bbeab446693b582080d Mon Sep 17 00:00:00 2001 From: Stefanni Date: Mon, 18 Jun 2018 17:07:17 -0700 Subject: [PATCH 1/3] API tagging locations implementation --- app/api/srch/search.rb | 11 ++++++----- app/services/search_service.rb | 7 +++++-- test/functional/search_api_test.rb | 24 ++++++++++-------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/app/api/srch/search.rb b/app/api/srch/search.rb index 89ecbb89bd..5d5467ef79 100644 --- a/app/api/srch/search.rb +++ b/app/api/srch/search.rb @@ -115,23 +115,24 @@ class Search < Grape::API sresult end - # Request URL should be /api/srch/locations?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] + # Request URL should be /api/srch/taglocations?srchString=QRY[&tagName=awesome&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] # Note: Query(QRY as above) must have latitude and longitude as srchString=lat,lon - desc 'Perform a search of documents having nearby latitude and longitude tag values', + desc 'Perform a search of documents having nearby latitude, longitude and tag values', hidden: false, is_array: false, - nickname: 'srchGetLocations' + nickname: 'srchGetTagLocations' params do requires :srchString, type: String, documentation: { example: 'Spec' } + optional :tagName, type: String, documentation: { example: 'awesome' } optional :seq, type: Integer, documentation: { example: 995 } optional :showCount, type: Integer, documentation: { example: 3 } optional :pageNum, type: Integer, documentation: { example: 0 } end - get :locations do + get :taglocations do sresult = DocList.new unless params[:srchString].nil? || params[:srchString] == 0 || !(params[:srchString].include? ",") sservice = SearchService.new - sresult = sservice.nearbyNodes(params[:srchString]) + sresult = sservice.tagNearbyNodes(params[:srchString], params[:tag]) end sparms = SearchRequest.fromRequest(params) sresult.srchParams = sparms diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 861e50002b..768d24a37f 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -196,13 +196,16 @@ def textSearch_questions(srchString) sresult end - # Search nearby nodes with respect to given latitude and longitude - def nearbyNodes(srchString) + # Search nearby nodes with respect to given latitude, longitute and tags + def tagNearbyNodes(srchString, tag) sresult = DocList.new coordinates = srchString.split(",") lat = coordinates[0] lon = coordinates[1] + tagList = textSearch_tags(tag) + sresult.addAll(tagList.items) + nids = NodeTag.joins(:tag) .where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%') .collect(&:nid) diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 9bf9642ef6..a46718d524 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -23,7 +23,7 @@ def app json = JSON.parse(last_response.body) - assert_equal nodes(:blog).path, json['items'][0]['docUrl'] + assert_equal nodes(:blog).path, json['items'][0]['docUrl'] assert_equal "Blog post", json['items'][0]['docTitle'] assert_equal 13, json['items'][0]['docId'] @@ -49,7 +49,7 @@ def app assert_equal "/profile/jeff", json['items'][0]['docUrl'] assert_equal "jeff", json['items'][0]['docTitle'] - assert_equal "user", json['items'][0]['docType'] + assert_equal "user", json['items'][0]['docType'] assert matcher =~ json @@ -119,16 +119,17 @@ def app json = JSON.parse(last_response.body) assert matcher =~ json - end + end - test 'search nearby nodes functionality' do - get '/api/srch/locations?srchString=71.00,52.00' + test 'search Tag Nearby Nodes functionality' do + get '/api/srch/taglocations?srchString=71.00,52.00&tagName=awesome' assert last_response.ok? # Expected search pattern pattern = { srchParams: { srchString: '71.00,52.00', + tagName: 'awesome', seq: nil, }.ignore_extra_keys! }.ignore_extra_keys! @@ -136,11 +137,6 @@ def app matcher = JsonExpressions::Matcher.new(pattern) json = JSON.parse(last_response.body) - - assert_equal nodes(:blog).path, json['items'][0]['docUrl'] - assert_equal "Blog post", json['items'][0]['docTitle'] - assert_equal 13, json['items'][0]['docId'] - assert matcher =~ json end @@ -162,8 +158,8 @@ def app json = JSON.parse(last_response.body) assert_equal users(:bob).username, json['items'][0]['docTitle'] - assert_equal "people_coordinates", json['items'][0]['docType'] - assert_equal 1, json['items'][0]['docId'] + assert_equal "people_coordinates", json['items'][0]['docType'] + assert_equal 1, json['items'][0]['docId'] assert matcher =~ json @@ -187,8 +183,8 @@ def app json = JSON.parse(last_response.body) assert_equal users(:bob).username, json['items'][0]['docTitle'] - assert_equal "people_coordinates", json['items'][0]['docType'] - assert_equal 1, json['items'][0]['docId'] + assert_equal "people_coordinates", json['items'][0]['docType'] + assert_equal 1, json['items'][0]['docId'] assert matcher =~ json From 2506d2f1cf7e2e2c72f7d8ca1b72006927360358 Mon Sep 17 00:00:00 2001 From: Stefanni Date: Sun, 1 Jul 2018 10:46:03 -0700 Subject: [PATCH 2/3] Requested changes for the API tagging implementation --- app/services/search_service.rb | 34 ++++++++++++++---------------- test/functional/search_api_test.rb | 27 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 768d24a37f..3332f586f5 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -197,20 +197,18 @@ def textSearch_questions(srchString) end # Search nearby nodes with respect to given latitude, longitute and tags - def tagNearbyNodes(srchString, tag) + def tagNearbyNodes(srchString, tagName) sresult = DocList.new - coordinates = srchString.split(",") - lat = coordinates[0] - lon = coordinates[1] + lat, lon = srchString.split(',') - tagList = textSearch_tags(tag) - sresult.addAll(tagList.items) - - nids = NodeTag.joins(:tag) + nodes_scope = NodeTag.joins(:tag) .where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%') - .collect(&:nid) - nids ||= [] + if tagName.present? + nodes_scope = nodes_scope.where('name LIKE ?', tagName) + end + + nids = nodes_scope.collect(&:nid) || [] items = Node.includes(:tag) .references(:node, :term_data) @@ -234,14 +232,14 @@ def tagNearbyNodes(srchString, tag) sresult end - #GET X number of latest people/contributors + #GET X number of latest people/contributors # X = srchString def recentPeople(_srchString, tagName = nil) - sresult = DocList.new + sresult = DocList.new nodes = Node.all.order("changed DESC").limit(100).distinct users = [] - nodes.each do |node| - if node.author.status != 0 + nodes.each do |node| + if node.author.status != 0 if tagName.blank? users << node.author.user else @@ -249,16 +247,16 @@ def recentPeople(_srchString, tagName = nil) end end end - users = users.uniq + users = users.uniq users.each do |user| - next unless user.has_power_tag("lat") && user.has_power_tag("lon") - blurred = false + 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 doc = DocResult.fromLocationSearch(user.id, 'people_coordinates', user.path, user.username, 0, 0, user.lat, user.lon, blurred) sresult.addDoc(doc) - end + end sresult end diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index a46718d524..646ab70df6 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -137,6 +137,33 @@ def app matcher = JsonExpressions::Matcher.new(pattern) json = JSON.parse(last_response.body) + + assert_equal "coordinates", json['items'][0]['docType'] + assert_equal 13, json['items'][0]['docId'] + + assert matcher =~ json + + end + + test 'search Tag Nearby Nodes functionality without tagName' do + get '/api/srch/taglocations?srchString=71.00,52.00' + assert last_response.ok? + + # Expected search pattern + pattern = { + srchParams: { + srchString: '71.00,52.00', + seq: nil, + }.ignore_extra_keys! + }.ignore_extra_keys! + + matcher = JsonExpressions::Matcher.new(pattern) + + json = JSON.parse(last_response.body) + + assert_equal "coordinates", json['items'][0]['docType'] + assert_equal 13, json['items'][0]['docId'] + assert matcher =~ json end From 2ff6ce0a905690b70d3b504750821f9f88d1e802 Mon Sep 17 00:00:00 2001 From: Stefanni Date: Tue, 3 Jul 2018 20:21:07 -0700 Subject: [PATCH 3/3] Requested changes for the API tagging implementation --- app/api/srch/search.rb | 4 ++-- app/services/search_service.rb | 6 ++++-- test/functional/search_api_test.rb | 26 -------------------------- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/app/api/srch/search.rb b/app/api/srch/search.rb index 5d5467ef79..dad6d4b4a4 100644 --- a/app/api/srch/search.rb +++ b/app/api/srch/search.rb @@ -132,7 +132,7 @@ class Search < Grape::API sresult = DocList.new unless params[:srchString].nil? || params[:srchString] == 0 || !(params[:srchString].include? ",") sservice = SearchService.new - sresult = sservice.tagNearbyNodes(params[:srchString], params[:tag]) + sresult = sservice.tagNearbyNodes(params[:srchString], params[:tagName]) end sparms = SearchRequest.fromRequest(params) sresult.srchParams = sparms @@ -155,7 +155,7 @@ class Search < Grape::API end get :peoplelocations do sresult = DocList.new - unless params[:srchString].nil? || params[:srchString] == 0 + unless params[:srchString].nil? || params[:srchString] == 0 sservice = SearchService.new sresult = sservice.recentPeople(params[:srchString], params[:tagName]) end diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 3332f586f5..b2edf59c2e 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -205,10 +205,12 @@ def tagNearbyNodes(srchString, tagName) .where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%') if tagName.present? - nodes_scope = nodes_scope.where('name LIKE ?', tagName) + nodes_scope = NodeTag.joins(:tag) + .where('name LIKE ?', tagName) + .where(nid: nodes_scope.select(:nid)) end - nids = nodes_scope.collect(&:nid) || [] + nids = nodes_scope.collect(&:nid).uniq || [] items = Node.includes(:tag) .references(:node, :term_data) diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 646ab70df6..a8ae92769d 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -138,32 +138,6 @@ def app json = JSON.parse(last_response.body) - assert_equal "coordinates", json['items'][0]['docType'] - assert_equal 13, json['items'][0]['docId'] - - assert matcher =~ json - - end - - test 'search Tag Nearby Nodes functionality without tagName' do - get '/api/srch/taglocations?srchString=71.00,52.00' - assert last_response.ok? - - # Expected search pattern - pattern = { - srchParams: { - srchString: '71.00,52.00', - seq: nil, - }.ignore_extra_keys! - }.ignore_extra_keys! - - matcher = JsonExpressions::Matcher.new(pattern) - - json = JSON.parse(last_response.body) - - assert_equal "coordinates", json['items'][0]['docType'] - assert_equal 13, json['items'][0]['docId'] - assert matcher =~ json end