Skip to content

Commit

Permalink
Query String: Add lenient flag to support *value* parse failures, c…
Browse files Browse the repository at this point in the history
…loses elastic#1932.
  • Loading branch information
kimchy committed May 10, 2012
1 parent 17fa678 commit 089f4b3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/org/apache/lucene/queryParser/MapperQueryParser.java
Expand Up @@ -75,6 +75,8 @@ public class MapperQueryParser extends QueryParser {

private String quoteFieldSuffix;

private boolean lenient;

public MapperQueryParser(QueryParseContext parseContext) {
super(Lucene.QUERYPARSER_VERSION, null, null);
this.parseContext = parseContext;
Expand Down Expand Up @@ -162,15 +164,23 @@ public Query getFieldQuery(String field, String queryText, boolean quoted) throw
if (currentMapper != null) {
Query query = null;
if (currentMapper.useFieldQueryWithQueryString()) {
if (fieldMappers.explicitTypeInNameWithDocMapper()) {
String[] previousTypes = QueryParseContext.setTypesWithPrevious(new String[]{fieldMappers.docMapper().type()});
try {
try {
if (fieldMappers.explicitTypeInNameWithDocMapper()) {
String[] previousTypes = QueryParseContext.setTypesWithPrevious(new String[]{fieldMappers.docMapper().type()});
try {
query = currentMapper.fieldQuery(queryText, parseContext);
} finally {
QueryParseContext.setTypes(previousTypes);
}
} else {
query = currentMapper.fieldQuery(queryText, parseContext);
} finally {
QueryParseContext.setTypes(previousTypes);
}
} else {
query = currentMapper.fieldQuery(queryText, parseContext);
} catch (RuntimeException e) {
if (lenient) {
return null;
} else {
throw e;
}
}
}
if (query == null) {
Expand Down
Expand Up @@ -51,6 +51,7 @@ public class QueryParserSettings {
private String quoteFieldSuffix = null;
private MultiTermQuery.RewriteMethod rewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
private String minimumShouldMatch;
private boolean lenient;

public String queryString() {
return queryString;
Expand Down Expand Up @@ -212,6 +213,14 @@ public String quoteFieldSuffix() {
return this.quoteFieldSuffix;
}

public void lenient(boolean lenient) {
this.lenient = lenient;
}

public boolean lenient() {
return this.lenient;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -246,6 +255,9 @@ public boolean equals(Object o) {
return false;
if (quoteFieldSuffix != null ? !quoteFieldSuffix.equals(that.quoteFieldSuffix) : that.quoteFieldSuffix != null)
return false;
if (lenient != that.lenient) {
return false;
}

return true;
}
Expand Down
Expand Up @@ -83,6 +83,7 @@ protected QueryParseContext initialValue() {
private final Map<String, FilterParser> filterParsers;

private String defaultField;
private boolean queryStringLenient;

@Inject
public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings,
Expand All @@ -101,6 +102,7 @@ public IndexQueryParserService(Index index, @IndexSettings Settings indexSetting
this.indexEngine = indexEngine;

this.defaultField = indexSettings.get("index.query.default_field", AllFieldMapper.NAME);
this.queryStringLenient = indexSettings.getAsBoolean("index.query_string.lenient", false);

List<QueryParser> queryParsers = newArrayList();
if (namedQueryParsers != null) {
Expand Down Expand Up @@ -157,6 +159,10 @@ public String defaultField() {
return this.defaultField;
}

public boolean queryStringLenient() {
return this.queryStringLenient;
}

public QueryParser queryParser(String name) {
return queryParsers.get(name);
}
Expand Down
Expand Up @@ -137,6 +137,10 @@ public String defaultField() {
return indexQueryParser.defaultField();
}

public boolean queryStringLenient() {
return indexQueryParser.queryStringLenient();
}

public MapperQueryParser singleQueryParser(QueryParserSettings settings) {
queryParser.reset(settings);
return queryParser;
Expand Down
Expand Up @@ -69,6 +69,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

MultiFieldQueryParserSettings qpSettings = new MultiFieldQueryParserSettings();
qpSettings.defaultField(parseContext.defaultField());
qpSettings.lenient(parseContext.queryStringLenient());
qpSettings.analyzeWildcard(defaultAnalyzeWildcard);
qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard);

Expand Down Expand Up @@ -178,6 +179,8 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
qpSettings.minimumShouldMatch(parser.textOrNull());
} else if ("quote_field_suffix".equals(currentFieldName) || "quoteFieldSuffix".equals(currentFieldName)) {
qpSettings.quoteFieldSuffix(parser.textOrNull());
} else if ("lenient".equalsIgnoreCase(currentFieldName)) {
qpSettings.lenient(parser.booleanValue());
} else {
throw new QueryParsingException(parseContext.index(), "[query_string] query does not support [" + currentFieldName + "]");
}
Expand Down

0 comments on commit 089f4b3

Please sign in to comment.