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=....)
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
50 # Matching for tags table
54 sql += ' AND current_way_tags.k=?'
58 sql += ' AND current_way_tags.v=? AND MATCH (current_way_tags.v) AGAINST (? IN BOOLEAN MODE)'
59 cond_way += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
61 cond_way = [sql] + cond_way
63 # Matching for tags table
67 sql += ' AND current_relation_tags.k=?'
71 sql += ' AND current_relation_tags.v=? AND MATCH (current_relation_tags.v) AGAINST (? IN BOOLEAN MODE)'
72 cond_rel += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
74 cond_rel = [sql] + cond_rel
76 # Matching for tags column
78 cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?',
80 ''+type+'='+value+';%',
81 '%;'+type+'='+value+';%',
82 '%;'+type+'='+value+'' ]
84 cond_tags = ['tags LIKE ? OR tags LIKE ?',
88 cond_tags = ['tags LIKE ? OR tags LIKE ?',
95 # First up, look for the relations we want
97 relations = Relation.find(:all,
98 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
99 :conditions => cond_rel, :limit => 100)
104 ways = Way.find(:all,
105 :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
106 :conditions => cond_way, :limit => 100)
111 nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
114 # Fetch any node needed for our ways (only have matching nodes so far)
115 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
119 user_display_name_cache = {}
120 doc = OSM::API.new.get_xml_doc
122 doc.root << node.to_xml_node(user_display_name_cache)
123 visible_nodes[node.id] = node
127 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
130 relations.each do |rel|
131 doc.root << rel.to_xml_node(user_display_name_cache)
133 render :text => doc.to_s, :content_type => "text/xml"