title: Extracting MUST-match and MUST_NOT-match Boolean clauses from an EDismax query
body: <p>I have a <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/Query.html" rel="nofollow">Query</a> <code>q</code> returned by <a href="http://lucene.apache.org/solr/4_5_0/solr-core/org/apache/solr/search/ExtendedDismaxQParser.html" rel="nofollow">ExtendedDismaxQParser</a>'s <a href="http://lucene.apache.org/solr/4_5_0/solr-core/org/apache/solr/search/ExtendedDismaxQParser.html#parse%28%29" rel="nofollow">parse()</a> method. I need to generate from that <code>Query q</code> a new <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/BooleanQuery.html" rel="nofollow">BooleanQuery</a> <code>qNew</code>. The <code>BooleanQuery qNew</code> should consist of:</p>
  
  <ul>
  <li>a <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/BooleanClause.Occur.html#MUST" rel="nofollow">MUST-match</a> Boolean clause for each MUST-match clause in the original <code>Query q</code>. </li>
  <li>a <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/BooleanClause.Occur.html#MUST_NOT" rel="nofollow">MUST_NOT-match</a> Boolean clause for each MUST_NOT-match clause in the original <code>Query q</code>.</li>
  <li>a single <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/BooleanClause.Occur.html#SHOULD" rel="nofollow">SHOULD-match</a> Boolean clause consisting of the rest of the original <code>Query q</code>.</li>
  <li>Other certain SHOULD-match clauses I might need to add.</li>
  </ul>
  
  <p>For example, if the <code>Query q</code> is <code>a +b c +d -e -f g</code> and the other certain clauses are <code>x</code>, <code>y</code> and <code>z</code> then the new <code>BooleanQuery qNew</code> should be <code>(a c g) +b +d -e -f x y z</code>.</p>
  
  <p>In order to generate that <code>BooleanQuery qNew</code> I need to extract all the MUST-match and MUST_NOT-match Boolean clauses from the original <code>Query q</code>, which would have been easy had <code>q</code> been a <code>BooleanQuery</code>. I looked at the ExtendedDismax <a href="https://github.com/apache/lucene-solr/blob/trunk/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java" rel="nofollow">parse() method code</a>, and the returned <code>Query q</code>'s dynamic type can be either a <a href="http://lucene.apache.org/core/4_5_0/core/org/apache/lucene/search/BooleanQuery.html" rel="nofollow">BooleanQuery</a> or a <a href="http://lucene.apache.org/core/4_5_0/queries/org/apache/lucene/queries/function/BoostedQuery.html" rel="nofollow">BoostedQuery</a>. Is there a general way to perform that transformation from the <code>Query q</code> returned by ExtendedDismax <code>parse()</code> method to the new <code>BooleanQuery qNew</code>?</p>
  
  <p><strong>Clarification:</strong> The original <code>Query q</code> doesn't necessarily consists of any MUST-match, MUST_NOT-match or SHOULD-match clauses. As a matter of fact, the original <code>Query q</code> can be any <code>Query</code> that can be returned by <code>ExtendedDismaxQParser</code>'s <code>parse()</code> method. Queries such as <code>a b c</code> and <code>a b -c</code> shuold be transformed into <code>(a b c) x y z</code> and <code>(a b) -c x y z</code> respectively. Therefore, wrapping the original query in a MUST-match clause of a Boolean query and add those <code>x</code>, <code>y</code> and <code>z</code> clauses (as suggested in a comment) wouldn't solve the problem in such cases.</p>