1 class SearchController < ApplicationController
2 # Support searching for nodes, segments, 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)
23 def do_search(do_ways,do_segments,do_nodes)
25 value = params['value']
39 # Matching for tags table
41 sql = 'id IN (SELECT id FROM current_way_tags WHERE 1=1'
51 cond_tbl = [sql] + cond_tbl
53 # Matching for tags column
55 cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?',
57 ''+type+'='+value+';%',
58 '%;'+type+'='+value+';%',
59 '%;'+type+'='+value+'' ]
61 cond_tags = ['tags LIKE ? OR tags LIKE ?',
65 cond_tags = ['tags LIKE ? OR tags LIKE ?',
73 # First up, look for the ways we want
75 ways = Way.find(:all, :conditions => cond_tbl, :limit => 100)
78 # Now, segments matching
80 segments = Segment.find(:all, :conditions => cond_tags, :limit => 500)
85 nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
88 # Fetch any segments needed for our ways (only have matching segments so far)
89 segments += Segment.find(ways.collect { |w| w.segs }.uniq)
91 # Fetch any nodes needed for our segments (only have matching nodes so far)
92 nodes += Node.find(segments.collect { |s| [s.node_a, s.node_b] }.flatten.uniq)
95 user_display_name_cache = {}
96 doc = OSM::API.new.get_xml_doc
98 doc.root << node.to_xml_node(user_display_name_cache)
101 segments.each do |segment|
102 doc.root << segment.to_xml_node(user_display_name_cache)
106 doc.root << way.to_xml_node(user_display_name_cache)
109 render :text => doc.to_s, :content_type => "text/xml"