Lucene: Prohibit searching empty string over a full-text field

org.apache.lucene.util.QueryBuilder returns a null query when given an
empty string to tokenize (see line 245). We can detect this case
earlier in the query building process and return a readable error to
the user.

This may not be the only way that QueryBuilder can return a null
query, so add a null check in LuceneChangeIndex so the resulting stack
trace is less obtuse.

Change-Id: Icf9a1ab50881d61ae4400a34cc2ce7e2a78e1162
This commit is contained in:
Dave Borowitz 2015-05-29 11:36:46 -07:00
parent 42850cf30b
commit 7a9c7e6a00
2 changed files with 10 additions and 3 deletions

View File

@ -14,6 +14,7 @@
package com.google.gerrit.lucene;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.gerrit.server.git.QueueProvider.QueueType.INTERACTIVE;
@ -393,7 +394,7 @@ public class LuceneChangeIndex implements ChangeIndex {
private QuerySource(List<SubIndex> indexes, Query query, int start,
int limit, Sort sort) {
this.indexes = indexes;
this.query = query;
this.query = checkNotNull(query, "null query from Lucene");
this.start = start;
this.limit = limit;
this.sort = sort;

View File

@ -242,8 +242,14 @@ public class QueryBuilder {
return new PrefixQuery(new Term(p.getField().getName(), p.getValue()));
}
private Query fullTextQuery(IndexPredicate<ChangeData> p) {
return queryBuilder.createPhraseQuery(p.getField().getName(), p.getValue());
private Query fullTextQuery(IndexPredicate<ChangeData> p)
throws QueryParseException {
String value = p.getValue();
if (value == null) {
throw new QueryParseException(
"Full-text search over empty string not supported");
}
return queryBuilder.createPhraseQuery(p.getField().getName(), value);
}
public int toIndexTimeInMinutes(Date ts) {