In MatchQuery, remove a check for fragile search analyzers. (#33927)

As far as I can tell this guard against fragile analyzers is no longer relevant, since
we stopped setting special analyzers on numeric fields (3bf6f4). Instead of removing
the guard completely, I opted to keep a check for untokenized + unnormalized fields
to avoid going through the analysis process unnecessarily.

My motivation for simplifying this check is that I'd like to add support for
`split_queries_on_whitespace` to the new 'queryable object' fields. As it stands, I would
have to add a dedicated instanceof check for the new mapper, which is not optimal.
This commit is contained in:
Julie Tibshirani 2018-09-24 08:56:13 -07:00 committed by GitHub
parent 78e483e8d8
commit 8e8bd56cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 12 deletions

View File

@ -48,10 +48,10 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.support.QueryParsers; import org.elasticsearch.index.query.support.QueryParsers;
@ -253,22 +253,17 @@ public class MatchQuery {
} }
final String field = fieldType.name(); final String field = fieldType.name();
Analyzer analyzer = getAnalyzer(fieldType, type == Type.PHRASE);
assert analyzer != null;
/* /*
* If the user forced an analyzer we really don't care if they are * If a keyword analyzer is used, we know that further analysis isn't
* searching a type that wants term queries to be used with query string * needed and can immediately return a term query.
* because the QueryBuilder will take care of it. If they haven't forced
* an analyzer then types like NumberFieldType that want terms with
* query string will blow up because their analyzer isn't capable of
* passing through QueryBuilder.
*/ */
boolean noForcedAnalyzer = this.analyzer == null; if (analyzer == Lucene.KEYWORD_ANALYZER) {
if (fieldType.tokenized() == false && noForcedAnalyzer &&
fieldType instanceof KeywordFieldMapper.KeywordFieldType == false) {
return blendTermQuery(new Term(fieldName, value.toString()), fieldType); return blendTermQuery(new Term(fieldName, value.toString()), fieldType);
} }
Analyzer analyzer = getAnalyzer(fieldType, type == Type.PHRASE);
assert analyzer != null;
MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType); MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType);
builder.setEnablePositionIncrements(this.enablePositionIncrements); builder.setEnablePositionIncrements(this.enablePositionIncrements);
if (hasPositions(fieldType)) { if (hasPositions(fieldType)) {