title: Converting a Google search query to a PostgreSQL "tsquery"
body: <p>How can I convert a Google search query to something I can feed PostgreSQL's to_tsquery() ?</p>
  
  <p>If there's no existing library out there, how should I go about parsing a Google search query in a language like PHP?</p>
  
  <p>For example, I'd like to take the following Google-ish search query:</p>
  
  <pre><code>("used cars" OR "new cars") -ford -mistubishi
  </code></pre>
  
  <p>And turn it into a to_tsquery()-friendly string:</p>
  
  <pre><code>('used cars' | 'new cars') &amp; !ford &amp; !mistubishi
  </code></pre>
  
  <p>I can fudge this with regexes, but that's the best I can do.  Is there some robust lexical analysis method of going about this?  I'd like to be able to support extended search operators too (like Google's site: and intitle:) that will apply to different database fields, and thus would need to be separated from the tsquery string.</p>
  
  <p>UPDATE: I realize that with special operators this becomes a Google to SQL WHERE-clause conversion, rather than a Google to tsquery conversion.  But the WHERE clause may contain one or more tsqueries.</p>
  
  <p>For example, the Google-style query:</p>
  
  <pre><code>((color:blue OR "4x4") OR style:coupe) -color:red used
  </code></pre>
  
  <p>Should produce an SQL WHERE-clause like this:</p>
  
  <pre><code>WHERE to_tsvector(description) MATCH to_tsquery('used')
    AND color &lt;&gt; 'red'
    AND ( (color = 'blue' OR to_tsvector(description) MATCH to_tsquery('4x4') )
      OR style = 'coupe'
    );
  </code></pre>
  
  <p>I'm not sure if the above is possible with regex?</p>