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_action :verify_authenticity_token
8 do_search(true, true, true)
12 do_search(true, false, false)
16 do_search(false, true, false)
20 do_search(false, false, true)
23 def do_search(do_ways, do_nodes, do_relations)
25 value = params["value"]
35 response.headers["Error"] = "Searching of nodes is currently unavailable"
36 head :service_unavailable
41 response.headers["Error"] = "Searching for a key without value is currently unavailable"
42 head :service_unavailable
46 # Matching for node tags table
48 nodes = Node.joins(:node_tags)
49 nodes = nodes.where(:current_node_tags => { :k => type }) if type
50 nodes = nodes.where(:current_node_tags => { :v => value }) if value
51 nodes = nodes.limit(100)
56 # Matching for way tags table
58 ways = Way.joins(:way_tags)
59 ways = ways.where(:current_way_tags => { :k => type }) if type
60 ways = ways.where(:current_way_tags => { :v => value }) if value
61 ways = ways.limit(100)
66 # Matching for relation tags table
68 relations = Relation.joins(:relation_tags)
69 relations = relations.where(:current_relation_tags => { :k => type }) if type
70 relations = relations.where(:current_relation_tags => { :v => value }) if value
71 relations = relations.limit(2000)
76 # Fetch any node needed for our ways (only have matching nodes so far)
77 nodes += Node.find(ways.collect(&:nds).uniq)
82 user_display_name_cache = {}
83 doc = OSM::API.new.get_xml_doc
85 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
86 visible_nodes[node.id] = node
90 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
93 relations.each do |rel|
94 doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
97 render :xml => doc.to_s