2 class SearchController < ApiController
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 authorize_resource :class => false
9 do_search(true, true, true)
13 do_search(true, false, false)
17 do_search(false, true, false)
21 do_search(false, false, true)
24 def do_search(do_ways, do_nodes, do_relations)
26 value = params["value"]
36 response.headers["Error"] = "Searching of nodes is currently unavailable"
37 head :service_unavailable
42 response.headers["Error"] = "Searching for a key without value is currently unavailable"
43 head :service_unavailable
47 # Matching for node tags table
49 nodes = Node.joins(:node_tags)
50 nodes = nodes.where(:current_node_tags => { :k => type }) if type
51 nodes = nodes.where(:current_node_tags => { :v => value }) if value
52 nodes = nodes.limit(100)
57 # Matching for way tags table
59 ways = Way.joins(:way_tags)
60 ways = ways.where(:current_way_tags => { :k => type }) if type
61 ways = ways.where(:current_way_tags => { :v => value }) if value
62 ways = ways.limit(100)
67 # Matching for relation tags table
69 relations = Relation.joins(:relation_tags)
70 relations = relations.where(:current_relation_tags => { :k => type }) if type
71 relations = relations.where(:current_relation_tags => { :v => value }) if value
72 relations = relations.limit(2000)
77 # Fetch any node needed for our ways (only have matching nodes so far)
78 nodes += Node.find(ways.collect(&:nds).uniq)
83 user_display_name_cache = {}
84 doc = OSM::API.new.get_xml_doc
86 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
87 visible_nodes[node.id] = node
91 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
94 relations.each do |rel|
95 doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
98 render :xml => doc.to_s