Fix phrase_slop in query_string query (#35533)

This change ensures that custom `phrase_slop`s are correctly handled
in the `query_string` query.

Fixes #35125
This commit is contained in:
Jim Ferenczi 2018-11-16 09:08:07 +01:00 committed by GitHub
parent c6e2634bf8
commit 903d27a8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -160,6 +160,12 @@ public class QueryStringQueryParser extends XQueryParser {
queryBuilder.setOccur(op == Operator.AND ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD); queryBuilder.setOccur(op == Operator.AND ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD);
} }
@Override
public void setPhraseSlop(int phraseSlop) {
super.setPhraseSlop(phraseSlop);
queryBuilder.setPhraseSlop(phraseSlop);
}
/** /**
* @param type Sets how multiple fields should be combined to build textual part queries. * @param type Sets how multiple fields should be combined to build textual part queries.
*/ */

View File

@ -1387,6 +1387,44 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStr
} }
} }
public void testPhraseSlop() throws Exception {
Query query = new QueryStringQueryBuilder("quick fox")
.field(STRING_FIELD_NAME)
.type(MultiMatchQueryBuilder.Type.PHRASE)
.toQuery(createShardContext());
PhraseQuery expected = new PhraseQuery.Builder()
.add(new Term(STRING_FIELD_NAME, "quick"))
.add(new Term(STRING_FIELD_NAME, "fox"))
.build();
assertEquals(expected, query);
query = new QueryStringQueryBuilder("quick fox")
.field(STRING_FIELD_NAME)
.type(MultiMatchQueryBuilder.Type.PHRASE)
.phraseSlop(2)
.toQuery(createShardContext());
expected = new PhraseQuery.Builder()
.add(new Term(STRING_FIELD_NAME, "quick"))
.add(new Term(STRING_FIELD_NAME, "fox"))
.setSlop(2)
.build();
assertEquals(expected, query);
query = new QueryStringQueryBuilder("\"quick fox\"")
.field(STRING_FIELD_NAME)
.phraseSlop(2)
.toQuery(createShardContext());
assertEquals(expected, query);
query = new QueryStringQueryBuilder("\"quick fox\"~2")
.field(STRING_FIELD_NAME)
.phraseSlop(10)
.toQuery(createShardContext());
assertEquals(expected, query);
}
private static IndexMetaData newIndexMeta(String name, Settings oldIndexSettings, Settings indexSettings) { private static IndexMetaData newIndexMeta(String name, Settings oldIndexSettings, Settings indexSettings) {
Settings build = Settings.builder().put(oldIndexSettings) Settings build = Settings.builder().put(oldIndexSettings)
.put(indexSettings) .put(indexSettings)