-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Allow fields
to be set to *
#42301
Merged
Merged
Allow fields
to be set to *
#42301
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
import static org.hamcrest.CoreMatchers.anyOf; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.hasItems; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
|
||
|
@@ -419,42 +420,65 @@ public void testDefaultField() throws Exception { | |
.build()) | ||
); | ||
|
||
MultiMatchQueryBuilder qb = new MultiMatchQueryBuilder("hello"); | ||
query = qb.toQuery(context); | ||
DisjunctionMaxQuery expected = new DisjunctionMaxQuery( | ||
Arrays.asList( | ||
new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new BoostQuery(new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")), 5.0f) | ||
), 0.0f | ||
); | ||
assertEquals(expected, query); | ||
|
||
context.getIndexSettings().updateIndexMetaData( | ||
newIndexMeta("index", context.getIndexSettings().getSettings(), | ||
Settings.builder().putList("index.query.default_field", | ||
STRING_FIELD_NAME, STRING_FIELD_NAME_2 + "^5", INT_FIELD_NAME).build()) | ||
); | ||
// should fail because lenient defaults to false | ||
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> qb.toQuery(context)); | ||
assertThat(exc, instanceOf(NumberFormatException.class)); | ||
assertThat(exc.getMessage(), equalTo("For input string: \"hello\"")); | ||
|
||
// explicitly sets lenient | ||
qb.lenient(true); | ||
query = qb.toQuery(context); | ||
expected = new DisjunctionMaxQuery( | ||
Arrays.asList( | ||
new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new BoostQuery(new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")), 5.0f), | ||
new MatchNoDocsQuery("failed [mapped_int] query, caused by number_format_exception:[For input string: \"hello\"]") | ||
), 0.0f | ||
); | ||
assertEquals(expected, query); | ||
try { | ||
MultiMatchQueryBuilder qb = new MultiMatchQueryBuilder("hello"); | ||
query = qb.toQuery(context); | ||
DisjunctionMaxQuery expected = new DisjunctionMaxQuery( | ||
Arrays.asList( | ||
new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new BoostQuery(new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")), 5.0f) | ||
), 0.0f | ||
); | ||
assertEquals(expected, query); | ||
|
||
context.getIndexSettings().updateIndexMetaData( | ||
newIndexMeta("index", context.getIndexSettings().getSettings(), | ||
Settings.builder().putList("index.query.default_field", | ||
STRING_FIELD_NAME, STRING_FIELD_NAME_2 + "^5", INT_FIELD_NAME).build()) | ||
); | ||
// should fail because lenient defaults to false | ||
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> qb.toQuery(context)); | ||
assertThat(exc, instanceOf(NumberFormatException.class)); | ||
assertThat(exc.getMessage(), equalTo("For input string: \"hello\"")); | ||
|
||
// explicitly sets lenient | ||
qb.lenient(true); | ||
query = qb.toQuery(context); | ||
expected = new DisjunctionMaxQuery( | ||
Arrays.asList( | ||
new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new BoostQuery(new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")), 5.0f), | ||
new MatchNoDocsQuery("failed [mapped_int] query, caused by number_format_exception:[For input string: \"hello\"]") | ||
), 0.0f | ||
); | ||
assertEquals(expected, query); | ||
|
||
} finally { | ||
// Reset to the default value | ||
context.getIndexSettings().updateIndexMetaData( | ||
newIndexMeta("index", context.getIndexSettings().getSettings(), | ||
Settings.builder().putNull("index.query.default_field").build()) | ||
); | ||
} | ||
} | ||
|
||
context.getIndexSettings().updateIndexMetaData( | ||
newIndexMeta("index", context.getIndexSettings().getSettings(), | ||
Settings.builder().putNull("index.query.default_field").build()) | ||
); | ||
public void testAllFieldsWildcard() throws Exception { | ||
Query query = new MultiMatchQueryBuilder("hello") | ||
.field(STRING_FIELD_NAME) | ||
.field("*") | ||
.field(STRING_FIELD_NAME_2) | ||
.toQuery(createShardContext()); | ||
assertEquals(DisjunctionMaxQuery.class, query.getClass()); | ||
DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query; | ||
int noMatchNoDocsQueries = 0; | ||
for (Query q : disjunctionMaxQuery.getDisjuncts()) { | ||
if (q.getClass() == MatchNoDocsQuery.class) { | ||
noMatchNoDocsQueries++; | ||
} | ||
} | ||
assertEquals(11, noMatchNoDocsQueries); | ||
assertThat(disjunctionMaxQuery.getDisjuncts(), hasItems(new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test with a single field set on |
||
} | ||
|
||
public void testWithStopWords() throws Exception { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ | |
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertDisjunctionSubQuery; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.hasItems; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
|
@@ -1278,6 +1279,25 @@ public void testDefaultField() throws Exception { | |
} | ||
} | ||
|
||
public void testAllFieldsWildcard() throws Exception { | ||
Query query = new QueryStringQueryBuilder("hello") | ||
.field(STRING_FIELD_NAME) | ||
.field("*") | ||
.field(STRING_FIELD_NAME_2) | ||
.toQuery(createShardContext()); | ||
assertEquals(DisjunctionMaxQuery.class, query.getClass()); | ||
DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query; | ||
int noMatchNoDocsQueries = 0; | ||
for (Query q : disjunctionMaxQuery.getDisjuncts()) { | ||
if (q.getClass() == MatchNoDocsQuery.class) { | ||
noMatchNoDocsQueries++; | ||
} | ||
} | ||
assertEquals(11, noMatchNoDocsQueries); | ||
assertThat(disjunctionMaxQuery.getDisjuncts(), hasItems(new TermQuery(new Term(STRING_FIELD_NAME, "hello")), | ||
new TermQuery(new Term(STRING_FIELD_NAME_2, "hello")))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
|
||
/** | ||
* the quote analyzer should overwrite any other forced analyzer in quoted parts of the query | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jimczi Why do we check only the first element here, shouldn't we check all the
defaultFields
?(same applies to all 3 types of queries)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should check all elements. However I don't think it's useful to set
fields
or the default fields to something like["*", "field1", "field2"]
but for consistency we should always set the leniency to true if*
is part of the list of fields to query.