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

API for nearby node fetching #1987

Merged
merged 6 commits into from
Jan 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ class Search < Grape::API
sresult
end

# Request URL should be /api/srch/locations?srchString=QRY[&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',
hidden: false,
is_array: false,
nickname: 'srchGetLocations'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
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
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0 || !(params[:srchString].include? ",")
sservice = SearchService.new
sresult = sservice.nearbyNodes(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
end

# end endpoint definitions
end
end
Expand Down
19 changes: 18 additions & 1 deletion app/models/doc_result.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A DocResult is an individual return item for a document (web page) search
class DocResult
attr_accessor :docId, :docType, :docUrl, :docTitle, :docSummary, :docScore
attr_accessor :docId, :docType, :docUrl, :docTitle, :docSummary, :docScore, :latitude, :longitude, :blurred

def initialize; end

Expand All @@ -15,6 +15,20 @@ def self.fromSearch(idval, typeval, urlval, titleval, sumval, scoreval)
obj
end

def self.fromLocationSearch(idval, typeval, urlval, titleval, sumval, scoreval, latitude, longitude, blurred)
obj = new
obj.docId = idval
obj.docType = typeval
obj.docUrl = urlval
obj.docTitle = titleval
obj.docSummary = sumval
obj.docScore = scoreval
obj.latitude = latitude
obj.longitude = longitude
obj.blurred = blurred
obj
end

# This subclass is used to auto-generate the RESTful data structure. It is generally not useful for internal Ruby usage
# but must be included for full RESTful functionality.
class Entity < Grape::Entity
Expand All @@ -24,5 +38,8 @@ class Entity < Grape::Entity
expose :docTitle, documentation: { type: 'String', desc: 'Title or primary descriptor of the linked result.' }
expose :docSummary, documentation: { type: 'String', desc: 'If available, first paragraph or descriptor of the linked document.' }
expose :docScore, documentation: { type: 'Float', desc: "If calculated, the relevance of the document result to the search request; i.e. the 'matching score'" }
expose :latitude, documentation: { type: 'String', desc: "Returns the latitude associated with the node." }
expose :longitude, documentation: { type: 'String', desc: "Returns the longitude associated with the node." }
end

end
36 changes: 36 additions & 0 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,40 @@ def textSearch_questions(srchString)
end
sresult
end

# Search nearby nodes with respect to given latitude and longitude
def nearbyNodes(srchString)
sresult = DocList.new
coordinates = srchString.split(",")
lat = coordinates[0]
lon = coordinates[1]

nids = NodeTag.joins(:tag)
.where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')
.collect(&:nid)

nids = nids || []

items = Node.includes(:tag)
.references(:node, :term_data)
.where('node.nid IN (?) AND term_data.name LIKE ?', nids, 'lon:' + lon[0..lon.length - 2] + '%')
.limit(200)
.order('node.nid DESC')

items.each do |match|
blurred = false

match.node_tags.each do |tag|
if tag.name == "location:blurred"
blurred = true
break
end
end

doc = DocResult.fromLocationSearch(match.nid, 'coordinates', match.path(:items), match.title, 0, match.answers.length.to_i, match.lat, match.lon, blurred)
sresult.addDoc(doc)
end
sresult
end

end
1 change: 1 addition & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Per-model API endpoints are:
* Questions: https://publiclab.org/api/srch/questions?srchString=foo
* Tags: https://publiclab.org/api/srch/tags?srchString=foo
* Notes: https://publiclab.org/api/srch/notes?srchString=foo
* Locations: https://publiclab.org/api/srch/locations?srchString=lat,lon
Copy link
Member

Choose a reason for hiding this comment

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

Awesome


We also provide RSS feeds for tags and authors, in the format:

Expand Down
27 changes: 25 additions & 2 deletions test/functional/search_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def app
test 'search notes functionality' do
get '/api/srch/notes?srchString=Blog'
assert last_response.ok?

# Expected search pattern
pattern = {
srchParams: {
Expand All @@ -26,7 +25,31 @@ def app
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

test 'search nearby nodes functionality' do
Copy link
Member

Choose a reason for hiding this comment

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

This is super. 🎉

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Jeff

get '/api/srch/locations?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 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 Down