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']
38 # Matching for tags table
42 sql += ' AND current_way_tags.k=?'
46 sql += ' AND current_way_tags.v=? AND MATCH (current_way_tags.v) AGAINST (? IN BOOLEAN MODE)'
47 cond_way += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
49 cond_way = [sql] + cond_way
51 # Matching for tags table
55 sql += ' AND current_relation_tags.k=?'
59 sql += ' AND current_relation_tags.v=? AND MATCH (current_relation_tags.v) AGAINST (? IN BOOLEAN MODE)'
60 cond_rel += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
62 cond_rel = [sql] + cond_rel
64 # Matching for tags column
66 cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?',
68 ''+type+'='+value+';%',
69 '%;'+type+'='+value+';%',
70 '%;'+type+'='+value+'' ]
72 cond_tags = ['tags LIKE ? OR tags LIKE ?',
76 cond_tags = ['tags LIKE ? OR tags LIKE ?',
83 # First up, look for the relations we want
85 relations = Relation.find(:all,
86 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
87 :conditions => cond_rel, :limit => 100)
93 :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
94 :conditions => cond_way, :limit => 100)
99 nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
102 # Fetch any node needed for our ways (only have matching nodes so far)
103 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
106 user_display_name_cache = {}
107 doc = OSM::API.new.get_xml_doc
109 doc.root << node.to_xml_node(user_display_name_cache)
113 doc.root << way.to_xml_node(nodes, user_display_name_cache)
116 relations.each do |rel|
117 doc.root << rel.to_xml_node(user_display_name_cache)
119 render :text => doc.to_s, :content_type => "text/xml"