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

Minor refactoring of search service #6918

Closed
wants to merge 3 commits into from
Closed
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
42 changes: 12 additions & 30 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,11 @@ def search_questions(input, limit = 25, order = :natural, type = :boolean)

# Search nearby nodes with respect to given latitude, longitute and tags
def tagNearbyNodes(coordinates, tag, period = { "from" => nil, "to" => nil }, sort_by = nil, order_direction = nil, limit = 10)
raise("Must contain all four coordinates") if coordinates["nwlat"].nil?
raise("Must contain all four coordinates") if coordinates["nwlng"].nil?
raise("Must contain all four coordinates") if coordinates["selat"].nil?
raise("Must contain all four coordinates") if coordinates["selng"].nil?

raise("Must be a float") unless coordinates["nwlat"].is_a? Float
raise("Must be a float") unless coordinates["nwlng"].is_a? Float
raise("Must be a float") unless coordinates["selat"].is_a? Float
raise("Must be a float") unless coordinates["selng"].is_a? Float

raise("If 'from' is not null, must contain date") if period["from"] && !(period["from"].is_a? Date)
raise("If 'to' is not null, must contain date") if period["to"] && !(period["to"].is_a? Date)
coordinates.each_value do |value|
raise("Must contain all four coordinates") if value.nil?
raise("Must be a float") unless value.is_a? Float
end
period.each { |key, value| raise("If '#{key}' is not null, must contain date") if value && !(value.is_a? Date) }

nodes_scope = Node.select(:nid)
.where('`latitude` >= ? AND `latitude` <= ?', coordinates["selat"], coordinates["nwlat"])
Expand Down Expand Up @@ -142,9 +135,7 @@ def tagNearbyNodes(coordinates, tag, period = { "from" => nil, "to" => nil }, so

# selects the items whose node_tags don't have the location:blurred tag
items.select do |item|
item.node_tags.none? do |node_tag|
node_tag.name == "location:blurred"
end
true if item.user_tags.none? { |user_tag| user_tag.name == "location:blurred" }
end

# sort nodes by recent activities if the sort_by==recent
Expand All @@ -160,18 +151,11 @@ def tagNearbyNodes(coordinates, tag, period = { "from" => nil, "to" => nil }, so
# Search nearby people with respect to given latitude, longitute and tags
# and package up as a DocResult
def tagNearbyPeople(coordinates, tag, field, period = nil, sort_by = nil, order_direction = nil, limit = 10)
raise("Must contain all four coordinates") if coordinates["nwlat"].nil?
raise("Must contain all four coordinates") if coordinates["nwlng"].nil?
raise("Must contain all four coordinates") if coordinates["selat"].nil?
raise("Must contain all four coordinates") if coordinates["selng"].nil?

raise("Must be a float") unless coordinates["nwlat"].is_a? Float
raise("Must be a float") unless coordinates["nwlng"].is_a? Float
raise("Must be a float") unless coordinates["selat"].is_a? Float
raise("Must be a float") unless coordinates["selng"].is_a? Float

raise("If 'from' is not null, must contain date") if period["from"] && !(period["from"].is_a? Date)
raise("If 'to' is not null, must contain date") if period["to"] && !(period["to"].is_a? Date)
coordinates.each_value do |value|
raise("Must contain all four coordinates") if value.nil?
raise("Must be a float") unless value.is_a? Float
end
period.each { |key, value| raise("If '#{key}' is not null, must contain date") if value && !(value.is_a? Date) }

user_locations = User.where('rusers.status <> 0')
.joins(:user_tags)
Expand Down Expand Up @@ -202,9 +186,7 @@ def tagNearbyPeople(coordinates, tag, field, period = nil, sort_by = nil, order_

# selects the items whose node_tags don't have the location:blurred tag
items.select do |item|
item.user_tags.none? do |user_tag|
user_tag.name == "location:blurred"
end
true if item.user_tags.none? { |user_tag| user_tag.name == "location:blurred" }
end

# Here we use period["from"] and period["to"] in the query only if they have been specified,
Expand Down