SOLR-9027: GraphTermsQuery optimizations and more explicit handling of non-caching behavior

This commit is contained in:
jbernste 2016-04-28 13:31:00 -04:00
parent d1f32c0432
commit 86f371cbc6
2 changed files with 47 additions and 25 deletions

View File

@ -39,6 +39,7 @@ import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight; import org.apache.lucene.search.Weight;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.DocIdSetBuilder; import org.apache.lucene.util.DocIdSetBuilder;
@ -77,7 +78,7 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
final String[] splitVals = qstr.split(","); final String[] splitVals = qstr.split(",");
BytesRef[] bytesRefs = new BytesRef[splitVals.length]; Term[] terms = new Term[splitVals.length];
BytesRefBuilder term = new BytesRefBuilder(); BytesRefBuilder term = new BytesRefBuilder();
for (int i = 0; i < splitVals.length; i++) { for (int i = 0; i < splitVals.length; i++) {
String stringVal = splitVals[i].trim(); String stringVal = splitVals[i].trim();
@ -86,15 +87,17 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
} else { } else {
term.copyChars(stringVal); term.copyChars(stringVal);
} }
bytesRefs[i] = term.toBytesRef(); BytesRef ref = term.toBytesRef();
terms[i] = new Term(fname, ref);
} }
return new ConstantScoreQuery(new GraphTermsQuery(fname, bytesRefs, maxDocFreq)); ArrayUtil.timSort(terms);
return new ConstantScoreQuery(new GraphTermsQuery(fname, terms, maxDocFreq));
} }
}; };
} }
private class GraphTermsQuery extends Query { private class GraphTermsQuery extends Query implements ExtendedQuery {
private Term[] queryTerms; private Term[] queryTerms;
private List<TermContext> finalContexts; private List<TermContext> finalContexts;
@ -103,14 +106,11 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
private int maxDocFreq; private int maxDocFreq;
private Object id; private Object id;
public GraphTermsQuery(String field, BytesRef[] terms, int maxDocFreq) { public GraphTermsQuery(String field, Term[] terms, int maxDocFreq) {
this.maxDocFreq = maxDocFreq; this.maxDocFreq = maxDocFreq;
this.field = field; this.field = field;
this.queryTerms = new Term[terms.length]; this.queryTerms = terms;
this.id = new Object(); this.id = new Object();
for(int i=0; i<terms.length; i++) {
this.queryTerms[i] = new Term(field, terms[i]);
}
} }
//Just for cloning //Just for cloning
@ -121,22 +121,42 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
this.id = id; this.id = id;
} }
public boolean getCache() {
return false;
}
public boolean getCacheSep() {
return false;
}
public void setCacheSep(boolean sep) {
}
public void setCache(boolean cache) {
}
public int getCost() {
return 1; // Not a post filter. The GraphTermsQuery will typically be used as the main query.
}
public void setCost(int cost) {
}
@Override @Override
public Query rewrite(IndexReader reader) throws IOException { public Query rewrite(IndexReader reader) throws IOException {
if(this.finalContexts == null) { this.finalContexts = new ArrayList();
//This query has not been re-written yet. this.finalTerms = new ArrayList();
//Rewriting the query does not effect the cache key as this query is not designed to be cached. List<LeafReaderContext> contexts = reader.leaves();
this.finalContexts = new ArrayList(); TermContext[] termContexts = new TermContext[this.queryTerms.length];
this.finalTerms = new ArrayList(); collectTermContext(reader, contexts, termContexts, this.queryTerms);
List<LeafReaderContext> contexts = reader.leaves(); for(int i=0; i<termContexts.length; i++) {
TermContext[] termContexts = new TermContext[this.queryTerms.length]; TermContext termContext = termContexts[i];
collectTermContext(reader, contexts, termContexts, this.queryTerms); if(termContext != null && termContext.docFreq() <= this.maxDocFreq) {
for(int i=0; i<termContexts.length; i++) { this.finalContexts.add(termContext);
TermContext termContext = termContexts[i]; this.finalTerms.add(queryTerms[i]);
if(termContext != null && termContext.docFreq() <= this.maxDocFreq) {
this.finalContexts.add(termContext);
this.finalTerms.add(queryTerms[i]);
}
} }
} }
@ -157,7 +177,10 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
} }
public GraphTermsQuery clone() { public GraphTermsQuery clone() {
GraphTermsQuery clone = new GraphTermsQuery(this.field, this.queryTerms, this.maxDocFreq, this.id); GraphTermsQuery clone = new GraphTermsQuery(this.field,
this.queryTerms,
this.maxDocFreq,
this.id);
return clone; return clone;
} }

View File

@ -42,7 +42,6 @@ import org.apache.solr.client.solrj.io.stream.expr.Expressible;
import org.apache.solr.client.solrj.io.stream.expr.StreamExplanation; import org.apache.solr.client.solrj.io.stream.expr.StreamExplanation;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter; import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue; import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType; import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;