Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alias field resolution in match query #47369

Merged
merged 3 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import static org.elasticsearch.common.lucene.search.Queries.newLenientFieldQuery;
Expand Down Expand Up @@ -235,6 +236,14 @@ public Query parse(Type type, String fieldName, Object value) throws IOException
if (fieldType == null) {
return newUnmappedFieldQuery(fieldName);
}
Set<String> fields = context.simpleMatchToIndexNames(fieldName);
if (fields.contains(fieldName)) {
assert fields.size() == 1;
// this field is a concrete field or an alias so we use the
// field type name directly
fieldName = fieldType.name();
}

Analyzer analyzer = getAnalyzer(fieldType, type == Type.PHRASE || type == Type.PHRASE_PREFIX);
assert analyzer != null;

Expand All @@ -248,9 +257,9 @@ public Query parse(Type type, String fieldName, Object value) throws IOException
*/
if (analyzer == Lucene.KEYWORD_ANALYZER && type != Type.PHRASE_PREFIX) {
final Term term = new Term(fieldName, value.toString());
if ((fieldType instanceof TextFieldMapper.TextFieldType || fieldType instanceof KeywordFieldMapper.KeywordFieldType)
&& type == Type.BOOLEAN_PREFIX) {
return builder.newPrefixQuery(fieldName, term);
if (type == Type.BOOLEAN_PREFIX
&& (fieldType instanceof TextFieldMapper.TextFieldType || fieldType instanceof KeywordFieldMapper.KeywordFieldType)) {
return builder.newPrefixQuery(term);
} else {
return builder.newTermQuery(term);
}
Expand Down Expand Up @@ -540,12 +549,12 @@ protected Query newTermQuery(Term term) {
/**
* Builds a new prefix query instance.
*/
protected Query newPrefixQuery(String field, Term term) {
protected Query newPrefixQuery(Term term) {
try {
return fieldType.prefixQuery(term.text(), null, context);
} catch (RuntimeException e) {
if (lenient) {
return newLenientFieldQuery(field, e);
return newLenientFieldQuery(term.field(), e);
}
throw e;
}
Expand All @@ -562,7 +571,7 @@ private Query analyzeTerm(String field, TokenStream stream, boolean isPrefix) th
final Term term = new Term(field, termAtt.getBytesRef());
int lastOffset = offsetAtt.endOffset();
stream.end();
return isPrefix && lastOffset == offsetAtt.endOffset() ? newPrefixQuery(field, term) : newTermQuery(term);
return isPrefix && lastOffset == offsetAtt.endOffset() ? newPrefixQuery(term) : newTermQuery(term);
}

private void add(BooleanQuery.Builder q, String field, List<Term> current, BooleanClause.Occur operator, boolean isPrefix) {
Expand All @@ -571,7 +580,7 @@ private void add(BooleanQuery.Builder q, String field, List<Term> current, Boole
}
if (current.size() == 1) {
if (isPrefix) {
q.add(newPrefixQuery(field, current.get(0)), operator);
q.add(newPrefixQuery(current.get(0)), operator);
} else {
q.add(newTermQuery(current.get(0)), operator);
}
Expand Down Expand Up @@ -688,7 +697,7 @@ public Query next() {
Term[] terms = graph.getTerms(field, start);
assert terms.length > 0;
if (terms.length == 1) {
queryPos = usePrefix ? newPrefixQuery(field, terms[0]) : newTermQuery(terms[0]);
queryPos = usePrefix ? newPrefixQuery(terms[0]) : newTermQuery(terms[0]);
} else {
// We don't apply prefix on synonyms
queryPos = newSynonymQuery(terms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected Query newTermQuery(Term term) {
}

@Override
protected Query newPrefixQuery(String field, Term term) {
protected Query newPrefixQuery(Term term) {
List<Query> disjunctions = new ArrayList<>();
for (FieldAndBoost fieldType : blendedFields) {
Query query = fieldType.fieldType.prefixQuery(term.text(), null, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static Map<String, Float> parseFieldsAndWeights(List<String> fields) {
float boost = 1.0f;
if (boostIndex != -1) {
fieldName = field.substring(0, boostIndex);
boost = Float.parseFloat(field.substring(boostIndex+1, field.length()));
boost = Float.parseFloat(field.substring(boostIndex+1));
} else {
fieldName = field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanOrQuery;
Expand Down Expand Up @@ -475,6 +476,18 @@ public void testMultiWordSynonymsPhrase() throws Exception {
assertEquals(expected, actual);
}


public void testAliasWithSynonyms() throws Exception {
final MatchQuery matchQuery = new MatchQuery(createShardContext());
matchQuery.setAnalyzer(new MockSynonymAnalyzer());
final Query actual = matchQuery.parse(Type.PHRASE, STRING_ALIAS_FIELD_NAME, "dogs");
Query expected = new SynonymQuery.Builder(STRING_FIELD_NAME)
.addTerm(new Term(STRING_FIELD_NAME, "dogs"))
.addTerm(new Term(STRING_FIELD_NAME, "dog"))
.build();
assertEquals(expected, actual);
}

public void testMaxBooleanClause() {
MatchQuery query = new MatchQuery(createShardContext());
query.setAnalyzer(new MockGraphAnalyzer(createGiantGraph(40)));
Expand Down