title: Manipulate Lucene query before performing search
body: <p>I'm working on a Java webapp (Spring 3.x) that uses SOLR for its search engine. I want to be able to intercept the Lucene query and substitute a "virtual" search field for either one of two indexed fields, based upon a lookup service (if successful use a range search otherwise search a regular field).</p>
  
  <p>E.g., given a query like <code>field0:foo (field1:bar OR field1:bash) AND field2:bing</code> (field1 being a virtual field)</p>
  
  <p>manipulate the query to get <code>field0:foo (field3:[42 TO 45] OR field4:bash) AND field2:bing</code></p>
  
  <p>So after toying with the idea of just using a reg ex, I decided to look at the Lucene classes, to see if I could re-use existing code. I'd like to be able to get a parsed version of the query so as to iterate over the clauses, looking for certain fields to manipulate. Then re-generate the query string and pass it on to SOLR.</p>
  
  <p>I've got close using Lucene's <a href="http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/queryParser/QueryParser.html" rel="nofollow">QueryParser</a> but I can only get the terms and not the boolean operators:</p>
  
  <pre><code>Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
  QueryParser queryParser = new QueryParser(Version.LUCENE_30, "text", analyzer);
  try {
      Query query = queryParser.parse(queryString);
      Set&lt;Term&gt; terms = new TreeSet&lt;Term&gt;();
      query.extractTerms(terms);
  
      for (Term t : terms) {
          logger.info("Term - field:" + t.field() + " | text:" + t.text());
      }
  } catch (ParseException ex) {
      logger.warn(ex.getMessage(), ex);
  }
  </code></pre>
  
  <p>I've looked at the <a href="http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search/BooleanQuery.html" rel="nofollow">BooleanQuery</a> but haven't had luck there either. Please help.</p>