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
 
  40     sql = 'id IN (SELECT id FROM current_way_tags WHERE 1=1'
 
  50     cond_tbl = [sql] + cond_tbl
 
  52     # Matching for tags column
 
  54       cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?', 
 
  56       ''+type+'='+value+';%',
 
  57       '%;'+type+'='+value+';%',
 
  58       '%;'+type+'='+value+'' ]
 
  60       cond_tags = ['tags LIKE ? OR tags LIKE ?',
 
  64       cond_tags = ['tags LIKE ? OR tags LIKE ?',
 
  71     # First up, look for the relations we want
 
  73       relations = Relation.find(:all, :conditions => cond_tbl, :limit => 100)
 
  78       ways = Way.find(:all, :conditions => cond_tbl, :limit => 100)
 
  83       nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
 
  86     # Fetch any node needed for our ways (only have matching nodes so far)
 
  87     nodes += Node.find(ways.collect { |w| w.nds }.uniq)
 
  90     user_display_name_cache = {}
 
  91     doc = OSM::API.new.get_xml_doc
 
  93       doc.root << node.to_xml_node(user_display_name_cache)
 
  97       doc.root << way.to_xml_node(user_display_name_cache)
 
 100     relations.each do |rel|
 
 101       doc.root << rel.to_xml_node(user_display_name_cache)
 
 103     render :text => doc.to_s, :content_type => "text/xml"