title: Rails: A good search algorithm
body: <p>I'm trying to return results more like the search</p>
  
  <p>My curren algorithm is this</p>
  
  <pre><code>def search_conditions(column, q)
    vars  = []
    vars2 = []
  
    vars &lt;&lt; q
  
    if q.size &gt; 3
      (q.size-2).times do |i|
        vars2 &lt;&lt; q[i..(i+2)]
        next if i == 0
        vars &lt;&lt; q[i..-1]
        vars &lt;&lt; q[0..(q.size-1-i)]
        vars &lt;&lt; q[i % 2 == 0 ? (i/2)..(q.size-(i/2)) : (i/2)..(q.size-1-(i/2))] if i &gt; 1
      end
    end
  
    query = "#{column} ILIKE ?"
    vars = (vars+vars2).uniq
  
    return [vars.map { query }.join(' OR ')] + vars.map { |x| "%#{x}%" }
  end
  </code></pre>
  
  <p>If I search for "Ruby on Rails" it will make 4 search ways.</p>
  
  <p>1) Removing the left letters "uby on Rails".."ils"</p>
  
  <p>2) Removing the right letters "Ruby on Rail".."Rub"</p>
  
  <p>3) Removing left and right letters "uby on Rails", "uby on Rail" ... "on "</p>
  
  <p>4) Using only 3 letters "Rub", "uby", "by ", "y o", " on" ... "ils"</p>
  
  <p>Is good to use these 4 ways? There any more?</p>