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

Implement API tagging location #2847

Merged
merged 3 commits into from Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions app/api/srch/search.rb
Expand Up @@ -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[:tagName])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
Expand All @@ -154,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
Expand Down
35 changes: 19 additions & 16 deletions app/services/search_service.rb
Expand Up @@ -196,18 +196,21 @@ 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, tagName)
sresult = DocList.new
coordinates = srchString.split(",")
lat = coordinates[0]
lon = coordinates[1]
lat, lon = srchString.split(',')
Copy link
Contributor

Choose a reason for hiding this comment

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

Amazing 😄 !


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 = NodeTag.joins(:tag)
.where('name LIKE ?', tagName)
.where(nid: nodes_scope.select(:nid))
end

nids = nodes_scope.collect(&:nid).uniq || []

items = Node.includes(:tag)
.references(:node, :term_data)
Expand All @@ -231,31 +234,31 @@ def nearbyNodes(srchString)
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
users << node.author.user if node.author.user.has_tag(tagName)
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

Expand Down
23 changes: 10 additions & 13 deletions test/functional/search_api_test.rb
Expand Up @@ -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']

Expand All @@ -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

Expand Down Expand Up @@ -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!
Expand All @@ -137,10 +138,6 @@ def app

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
Expand All @@ -162,8 +159,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

Expand All @@ -187,8 +184,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

Expand Down