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 skip_before_filter :verify_authenticity_token
6 after_filter :compress_output
9 do_search(true,true,true)
13 do_search(true,false,false)
16 do_search(false,true,false)
19 do_search(false,false,true)
22 def do_search(do_ways,do_nodes,do_relations)
24 value = params['value']
34 response.headers['Error'] = "Searching of nodes is currently unavailable"
35 render :nothing => true, :status => :service_unavailable
40 response.headers['Error'] = "Searching for a key without value is currently unavailable"
41 render :nothing => true, :status => :service_unavailable
45 # Matching for node tags table
47 nodes = Node.joins(:node_tags)
48 nodes = nodes.where(:current_node_tags => { :k => type }) if type
49 nodes = nodes.where(:current_node_tags => { :v => value }) if value
50 nodes = nodes.limit(100)
55 # Matching for way tags table
57 ways = Way.joins(:way_tags)
58 ways = ways.where(:current_way_tags => { :k => type }) if type
59 ways = ways.where(:current_way_tags => { :v => value }) if value
60 ways = ways.limit(100)
65 # Matching for relation tags table
67 relations = Relation.joins(:relation_tags)
68 relations = relations.where(:current_relation_tags => { :k => type }) if type
69 relations = relations.where(:current_relation_tags => { :v => value }) if value
70 relations = relations.limit(2000)
75 # Fetch any node needed for our ways (only have matching nodes so far)
76 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
81 user_display_name_cache = {}
82 doc = OSM::API.new.get_xml_doc
84 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
85 visible_nodes[node.id] = node
89 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
92 relations.each do |rel|
93 doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
96 render :text => doc.to_s, :content_type => "text/xml"