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
49 # Matching for tags table
53 sql += ' AND current_way_tags.k=?'
57 sql += ' AND current_way_tags.v=? AND MATCH (current_way_tags.v) AGAINST (? IN BOOLEAN MODE)'
58 cond_way += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
60 cond_way = [sql] + cond_way
62 # Matching for tags table
66 sql += ' AND current_relation_tags.k=?'
70 sql += ' AND current_relation_tags.v=? AND MATCH (current_relation_tags.v) AGAINST (? IN BOOLEAN MODE)'
71 cond_rel += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
73 cond_rel = [sql] + cond_rel
75 # Matching for tags column
77 cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?',
79 ''+type+'='+value+';%',
80 '%;'+type+'='+value+';%',
81 '%;'+type+'='+value+'' ]
83 cond_tags = ['tags LIKE ? OR tags LIKE ?',
87 cond_tags = ['tags LIKE ? OR tags LIKE ?',
94 # First up, look for the relations we want
96 relations = Relation.find(:all,
97 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
98 :conditions => cond_rel, :limit => 100)
103 ways = Way.find(:all,
104 :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
105 :conditions => cond_way, :limit => 100)
110 nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
113 # Fetch any node needed for our ways (only have matching nodes so far)
114 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
118 user_display_name_cache = {}
119 doc = OSM::API.new.get_xml_doc
121 doc.root << node.to_xml_node(user_display_name_cache)
122 visible_nodes[node.id] = node
126 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
129 relations.each do |rel|
130 doc.root << rel.to_xml_node(user_display_name_cache)
132 render :text => doc.to_s, :content_type => "text/xml"