Exemplo n.º 1
0
 /**
  * Expert: called when re-writing queries under MultiSearcher.
  *
  * <p>Create a single query suitable for use by all subsearchers (in 1-1 correspondence with
  * queries). This is an optimization of the OR of all queries. We handle the common optimization
  * cases of equal queries and overlapping clauses of boolean OR queries (as generated by
  * MultiTermQuery.rewrite()). Be careful overriding this method as queries[0] determines which
  * method will be called and is not necessarily of the same type as the other queries.
  */
 public Query combine(Query[] queries) {
   HashSet<Query> uniques = new HashSet<Query>();
   for (int i = 0; i < queries.length; i++) {
     Query query = queries[i];
     BooleanClause[] clauses = null;
     // check if we can split the query into clauses
     boolean splittable = (query instanceof BooleanQuery);
     if (splittable) {
       BooleanQuery bq = (BooleanQuery) query;
       splittable = bq.isCoordDisabled();
       clauses = bq.getClauses();
       for (int j = 0; splittable && j < clauses.length; j++) {
         splittable = (clauses[j].getOccur() == BooleanClause.Occur.SHOULD);
       }
     }
     if (splittable) {
       for (int j = 0; j < clauses.length; j++) {
         uniques.add(clauses[j].getQuery());
       }
     } else {
       uniques.add(query);
     }
   }
   // optimization: if we have just one query, just return it
   if (uniques.size() == 1) {
     return uniques.iterator().next();
   }
   BooleanQuery result = new BooleanQuery(true);
   for (final Query query : uniques) result.add(query, BooleanClause.Occur.SHOULD);
   return result;
 }