SOLR-115 - minor optimization, replace uses of BooleanQuery.getClauses() with BooleanQuery.clauses()

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@536282 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2007-05-08 18:15:48 +00:00
parent 23e5fb0e47
commit 46428da0dc
9 changed files with 43 additions and 40 deletions

View File

@ -223,6 +223,11 @@ Optimizations
hash algorithm speeds up exists() by 10% and intersectionSize() by 8%. hash algorithm speeds up exists() by 10% and intersectionSize() by 8%.
(yonik) (yonik)
2. SOLR-115: Solr now uses BooleanQuery.clauses() instead of
BooleanQuery.getClauses() in any situation where there is no risk of
modifying the original query.
(hossman)
Bug Fixes Bug Fixes
1. SOLR-87: Parsing of synonym files did not correctly handle escaped 1. SOLR-87: Parsing of synonym files did not correctly handle escaped
whitespace such as \r\n\t\b\f. (yonik) whitespace such as \r\n\t\b\f. (yonik)

View File

@ -275,8 +275,8 @@ public class DisMaxRequestHandler extends RequestHandlerBase {
/* if the default boost was used, and we've got a BooleanQuery /* if the default boost was used, and we've got a BooleanQuery
* extract the subqueries out and use them directly * extract the subqueries out and use them directly
*/ */
for (BooleanClause c : ((BooleanQuery)f).getClauses()) { for (Object c : ((BooleanQuery)f).clauses()) {
query.add(c); query.add((BooleanClause)c);
} }
} else { } else {
query.add(f, BooleanClause.Occur.SHOULD); query.add(f, BooleanClause.Occur.SHOULD);

View File

@ -24,6 +24,7 @@ package org.apache.solr.search;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.io.IOException; import java.io.IOException;
@ -64,9 +65,7 @@ class LuceneQueryOptimizer {
BooleanQuery query = new BooleanQuery(); BooleanQuery query = new BooleanQuery();
BooleanQuery filterQuery = null; BooleanQuery filterQuery = null;
BooleanClause[] clauses = original.getClauses(); for (BooleanClause c : (List<BooleanClause>)original.clauses()) {
for (int i = 0; i < clauses.length; i++) {
BooleanClause c = clauses[i];
/*** /***
System.out.println("required="+c.required); System.out.println("required="+c.required);

View File

@ -304,9 +304,8 @@ public class QueryParsing {
if (needParens) { if (needParens) {
out.append('('); out.append('(');
} }
BooleanClause[] clauses = q.getClauses();
boolean first=true; boolean first=true;
for (BooleanClause c : clauses) { for (BooleanClause c : (List<BooleanClause>)q.clauses()) {
if (!first) { if (!first) {
out.append(' '); out.append(' ');
} else { } else {

View File

@ -18,12 +18,9 @@ public class QueryUtils {
static boolean isNegative(Query q) { static boolean isNegative(Query q) {
if (!(q instanceof BooleanQuery)) return false; if (!(q instanceof BooleanQuery)) return false;
BooleanQuery bq = (BooleanQuery)q; BooleanQuery bq = (BooleanQuery)q;
// TODO: use after next lucene update List<BooleanClause> clauses = bq.clauses();
//for (BooleanClause clause: (List <BooleanClause>)bq.clauses()) { if (clauses.size()==0) return false;
// if (bq.getClauses().size()==0) return false; for (BooleanClause clause : clauses) {
BooleanClause[] clauses = bq.getClauses();
if (clauses.length==0) return false;
for (BooleanClause clause: clauses) {
if (!clause.isProhibited()) return false; if (!clause.isProhibited()) return false;
} }
return true; return true;
@ -43,17 +40,17 @@ public class QueryUtils {
if (!(q instanceof BooleanQuery)) return q; if (!(q instanceof BooleanQuery)) return q;
BooleanQuery bq = (BooleanQuery)q; BooleanQuery bq = (BooleanQuery)q;
BooleanClause[] clauses = bq.getClauses(); List<BooleanClause> clauses = bq.clauses();
if (clauses.length==0) return q; if (clauses.size()==0) return q;
for (BooleanClause clause: clauses) { for (BooleanClause clause : clauses) {
if (!clause.isProhibited()) return q; if (!clause.isProhibited()) return q;
} }
if (clauses.length==1) { if (clauses.size()==1) {
// if only one clause, dispense with the wrapping BooleanQuery // if only one clause, dispense with the wrapping BooleanQuery
Query negClause = clauses[0].getQuery(); Query negClause = clauses.get(0).getQuery();
// we shouldn't need to worry about adjusting the boosts since the negative // we shouldn't need to worry about adjusting the boosts since the negative
// clause would have never been selected in a positive query, and hence would // clause would have never been selected in a positive query, and hence would
// not contribute to a score. // not contribute to a score.
@ -64,7 +61,7 @@ public class QueryUtils {
// ignore minNrShouldMatch... it doesn't make sense for a negative query // ignore minNrShouldMatch... it doesn't make sense for a negative query
// the inverse of -a -b is a OR b // the inverse of -a -b is a OR b
for (BooleanClause clause: clauses) { for (BooleanClause clause : clauses) {
newBq.add(clause.getQuery(), BooleanClause.Occur.SHOULD); newBq.add(clause.getQuery(), BooleanClause.Occur.SHOULD);
} }
return newBq; return newBq;

View File

@ -566,7 +566,7 @@ public class SolrPluginUtils {
public static void setMinShouldMatch(BooleanQuery q, String spec) { public static void setMinShouldMatch(BooleanQuery q, String spec) {
int optionalClauses = 0; int optionalClauses = 0;
for (BooleanClause c : q.getClauses()) { for (BooleanClause c : (List<BooleanClause>)q.clauses()) {
if (c.getOccur() == Occur.SHOULD) { if (c.getOccur() == Occur.SHOULD) {
optionalClauses++; optionalClauses++;
} }
@ -633,21 +633,20 @@ public class SolrPluginUtils {
*/ */
public static void flattenBooleanQuery(BooleanQuery to, BooleanQuery from) { public static void flattenBooleanQuery(BooleanQuery to, BooleanQuery from) {
BooleanClause[] c = from.getClauses(); for (BooleanClause clause : (List<BooleanClause>)from.clauses()) {
for (int i = 0; i < c.length; i++) {
Query ci = c[i].getQuery(); Query cq = clause.getQuery();
ci.setBoost(ci.getBoost() * from.getBoost()); cq.setBoost(cq.getBoost() * from.getBoost());
if (ci instanceof BooleanQuery if (cq instanceof BooleanQuery
&& !c[i].isRequired() && !clause.isRequired()
&& !c[i].isProhibited()) { && !clause.isProhibited()) {
/* we can recurse */ /* we can recurse */
flattenBooleanQuery(to, (BooleanQuery)ci); flattenBooleanQuery(to, (BooleanQuery)cq);
} else { } else {
to.add(c[i]); to.add(clause);
} }
} }
} }

View File

@ -281,7 +281,7 @@ public class BasicFunctionalityTest extends AbstractSolrTestCase {
h.getCore().getSchema()); h.getCore().getSchema());
assertTrue("not boolean?", q instanceof BooleanQuery); assertTrue("not boolean?", q instanceof BooleanQuery);
assertEquals("unexpected number of stemmed synonym tokens", assertEquals("unexpected number of stemmed synonym tokens",
2, ((BooleanQuery) q).getClauses().length); 2, ((BooleanQuery) q).clauses().size());
} }

View File

@ -8,6 +8,8 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.solr.util.AbstractSolrTestCase; import org.apache.solr.util.AbstractSolrTestCase;
import java.util.List;
/** /**
* @author yonik * @author yonik
* @version $Id$ * @version $Id$
@ -27,9 +29,9 @@ public class TestQueryUtils extends AbstractSolrTestCase {
public void positive(Query q) { public void positive(Query q) {
assertFalse(QueryUtils.isNegative(q)); assertFalse(QueryUtils.isNegative(q));
assertTrue(QueryUtils.getAbs(q)==q); assertTrue(QueryUtils.getAbs(q)==q);
BooleanClause[] clauses = (q instanceof BooleanQuery) ? ((BooleanQuery)q).getClauses() : null; List<BooleanClause> clauses = (q instanceof BooleanQuery) ? ((BooleanQuery)q).clauses() : null;
if (clauses != null) { if (clauses != null) {
if (clauses.length != 0) { if (clauses.size() != 0) {
assertTrue(QueryUtils.makeQueryable(q)==q); assertTrue(QueryUtils.makeQueryable(q)==q);
} }
} else { } else {

View File

@ -186,14 +186,15 @@ public class SolrPluginUtilsTest extends AbstractSolrTestCase {
out instanceof BooleanQuery); out instanceof BooleanQuery);
{ {
BooleanQuery bq = (BooleanQuery)out; BooleanQuery bq = (BooleanQuery)out;
List<BooleanClause> clauses = bq.clauses();
assertEquals(t+" wrong number of clauses", 2, assertEquals(t+" wrong number of clauses", 2,
bq.getClauses().length); clauses.size());
Query sub = bq.getClauses()[0].getQuery(); Query sub = clauses.get(0).getQuery();
assertTrue(t+" first wasn't a DMQ:" + sub.getClass(), assertTrue(t+" first wasn't a DMQ:" + sub.getClass(),
sub instanceof DisjunctionMaxQuery); sub instanceof DisjunctionMaxQuery);
assertEquals(t+" first had wrong number of clauses", 4, assertEquals(t+" first had wrong number of clauses", 4,
countItems(((DisjunctionMaxQuery)sub).iterator())); countItems(((DisjunctionMaxQuery)sub).iterator()));
sub = bq.getClauses()[1].getQuery(); sub = clauses.get(1).getQuery();
assertTrue(t+" second wasn't a DMQ:" + sub.getClass(), assertTrue(t+" second wasn't a DMQ:" + sub.getClass(),
sub instanceof DisjunctionMaxQuery); sub instanceof DisjunctionMaxQuery);
assertEquals(t+" second had wrong number of clauses", 1, assertEquals(t+" second had wrong number of clauses", 1,
@ -208,14 +209,15 @@ public class SolrPluginUtilsTest extends AbstractSolrTestCase {
out instanceof BooleanQuery); out instanceof BooleanQuery);
{ {
BooleanQuery bq = (BooleanQuery)out; BooleanQuery bq = (BooleanQuery)out;
List<BooleanClause> clauses = bq.clauses();
assertEquals(t+" wrong number of clauses", 2, assertEquals(t+" wrong number of clauses", 2,
bq.getClauses().length); clauses.size());
Query sub = bq.getClauses()[0].getQuery(); Query sub = clauses.get(0).getQuery();
assertTrue(t+" first wasn't a DMQ:" + sub.getClass(), assertTrue(t+" first wasn't a DMQ:" + sub.getClass(),
sub instanceof DisjunctionMaxQuery); sub instanceof DisjunctionMaxQuery);
assertEquals(t+" first had wrong number of clauses", 4, assertEquals(t+" first had wrong number of clauses", 4,
countItems(((DisjunctionMaxQuery)sub).iterator())); countItems(((DisjunctionMaxQuery)sub).iterator()));
sub = bq.getClauses()[1].getQuery(); sub = clauses.get(1).getQuery();
assertTrue(t+" second wasn't a DMQ:" + sub.getClass(), assertTrue(t+" second wasn't a DMQ:" + sub.getClass(),
sub instanceof DisjunctionMaxQuery); sub instanceof DisjunctionMaxQuery);
assertEquals(t+" second had wrong number of clauses (stop words)", 2, assertEquals(t+" second had wrong number of clauses (stop words)", 2,