1 class SearchController < ApplicationController
2 # Support searching for nodes, ways, or all
3 # Can search by tag k, v, or both (type->k,value->v)
4 # Can search by name (k=name,v=....)
5 after_filter :compress_output
8 do_search(true,true,true)
12 do_search(true,false,false)
15 do_search(false,true,false)
18 do_search(false,false,true)
21 def do_search(do_ways,do_nodes,do_relations)
23 value = params['value']
33 response.headers['Error'] = "Searching of nodes is currently unavailable"
34 render :nothing => true, :status => :service_unavailable
39 response.headers['Error'] = "Searching for a key without value is currently unavailable"
40 render :nothing => true, :status => :service_unavailable
44 # Matching for node tags table
46 nodes = Node.joins(:node_tags)
47 nodes = nodes.where(:current_node_tags => { :k => type }) if type
48 nodes = nodes.where(:current_node_tags => { :v => value }) if value
49 nodes = nodes.limit(100)
52 # Matching for way tags table
54 ways = Way.joins(:way_tags)
55 ways = ways.where(:current_way_tags => { :k => type }) if type
56 ways = ways.where(:current_way_tags => { :v => value }) if value
57 ways = ways.limit(100)
60 # Matching for relation tags table
62 relations = Relation.joins(:relation_tags)
63 relations = relations.where(:current_relation_tags => { :k => type }) if type
64 relations = relations.where(:current_relation_tags => { :v => value }) if value
65 relations = relations.limit(2000)
68 # Fetch any node needed for our ways (only have matching nodes so far)
69 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
74 user_display_name_cache = {}
75 doc = OSM::API.new.get_xml_doc
77 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
78 visible_nodes[node.id] = node
82 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
85 relations.each do |rel|
86 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
89 render :text => doc.to_s, :content_type => "text/xml"