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:
parent
c6e2634bf8
commit
903d27a8fd
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue