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)
53 # Matching for way tags table
55 ways = Way.joins(:way_tags)
56 ways = ways.where(:current_way_tags => { :k => type }) if type
57 ways = ways.where(:current_way_tags => { :v => value }) if value
58 ways = ways.limit(100)
61 # Matching for relation tags table
63 relations = Relation.joins(:relation_tags)
64 relations = relations.where(:current_relation_tags => { :k => type }) if type
65 relations = relations.where(:current_relation_tags => { :v => value }) if value
66 relations = relations.limit(2000)
69 # Fetch any node needed for our ways (only have matching nodes so far)
70 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
75 user_display_name_cache = {}
76 doc = OSM::API.new.get_xml_doc
78 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
79 visible_nodes[node.id] = node
83 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
86 relations.each do |rel|
87 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
90 render :text => doc.to_s, :content_type => "text/xml"