2 class SearchController < ApplicationController
3 # Support searching for nodes, ways, or all
4 # Can search by tag k, v, or both (type->k,value->v)
5 # Can search by name (k=name,v=....)
6 skip_before_action :verify_authenticity_token
7 authorize_resource :class => false
10 do_search(true, true, true)
14 do_search(true, false, false)
18 do_search(false, true, false)
22 do_search(false, false, true)
25 def do_search(do_ways, do_nodes, do_relations)
27 value = params["value"]
37 response.headers["Error"] = "Searching of nodes is currently unavailable"
38 head :service_unavailable
43 response.headers["Error"] = "Searching for a key without value is currently unavailable"
44 head :service_unavailable
48 # Matching for node tags table
50 nodes = Node.joins(:node_tags)
51 nodes = nodes.where(:current_node_tags => { :k => type }) if type
52 nodes = nodes.where(:current_node_tags => { :v => value }) if value
53 nodes = nodes.limit(100)
58 # Matching for way tags table
60 ways = Way.joins(:way_tags)
61 ways = ways.where(:current_way_tags => { :k => type }) if type
62 ways = ways.where(:current_way_tags => { :v => value }) if value
63 ways = ways.limit(100)
68 # Matching for relation tags table
70 relations = Relation.joins(:relation_tags)
71 relations = relations.where(:current_relation_tags => { :k => type }) if type
72 relations = relations.where(:current_relation_tags => { :v => value }) if value
73 relations = relations.limit(2000)
78 # Fetch any node needed for our ways (only have matching nodes so far)
79 nodes += Node.find(ways.collect(&:nds).uniq)
84 user_display_name_cache = {}
85 doc = OSM::API.new.get_xml_doc
87 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
88 visible_nodes[node.id] = node
92 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
95 relations.each do |rel|
96 doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
99 render :xml => doc.to_s