-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Improve execution of terms queries over wildcard fields #128986
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ec2534
Improve execution of terms queries over wildcard fields
iverase c7b8d23
Update docs/changelog/128986.yaml
iverase 6c3d3fe
Merge branch 'main' into wildCardTermsQuery
iverase 43987b2
Merge branch 'main' into wildCardTermsQuery
iverase b482dbc
Merge branch 'main' into wildCardTermsQuery
iverase 74d1a62
Merge branch 'main' into wildCardTermsQuery
iverase c3b4b52
Merge branch 'main' into wildCardTermsQuery
iverase b1cc13b
Merge branch 'main' into wildCardTermsQuery
iverase 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pr: 128986 | ||
summary: Improve execution of terms queries over wildcard fields | ||
area: Search | ||
type: bug | ||
issues: | ||
- 128201 |
This file contains hidden or 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
153 changes: 153 additions & 0 deletions
153
...rc/internalClusterTest/java/org/elasticsearch/xpack/wildcard/search/WildcardSearchIT.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.wildcard.search; | ||
|
||
import org.elasticsearch.action.bulk.BulkRequestBuilder; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.index.query.TermQueryBuilder; | ||
import org.elasticsearch.index.query.TermsQueryBuilder; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xcontent.XContentFactory; | ||
import org.elasticsearch.xcontent.XContentType; | ||
import org.elasticsearch.xpack.wildcard.Wildcard; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; | ||
|
||
public class WildcardSearchIT extends ESIntegTestCase { | ||
|
||
private List<String> terms = null; | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return List.of(Wildcard.class); | ||
} | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
terms = new ArrayList<>(); | ||
XContentBuilder xcb = XContentFactory.jsonBuilder() | ||
.startObject() | ||
.startObject("properties") | ||
.startObject("wildcard") | ||
.field("type", "wildcard") | ||
.endObject() | ||
.startObject("keyword") | ||
.field("type", "keyword") | ||
.endObject() | ||
.endObject() | ||
.endObject(); | ||
indicesAdmin().prepareCreate("test").setMapping(xcb).get(); | ||
final int numDocs = randomIntBetween(100, 1000); | ||
final BulkRequestBuilder builder = client().prepareBulk(); | ||
for (int i = 0; i < numDocs; i++) { | ||
if (rarely()) { | ||
indexMultiValue(builder); | ||
} else { | ||
indexSingleValue(builder); | ||
} | ||
} | ||
assertFalse(builder.get().hasFailures()); | ||
indicesAdmin().prepareRefresh("test").get(); | ||
} | ||
|
||
private void indexSingleValue(BulkRequestBuilder builder) { | ||
String term = randomIndexString(); | ||
builder.add( | ||
new IndexRequest("test").source("{\"wildcard\" : \"" + term + "\", \"keyword\" : \"" + term + "\"}", XContentType.JSON) | ||
); | ||
terms.add(term); | ||
} | ||
|
||
private void indexMultiValue(BulkRequestBuilder builder) { | ||
int docSize = randomIntBetween(1, 10); | ||
String[] docTerms = new String[docSize]; | ||
for (int i = 0; i < docSize; i++) { | ||
String term = randomIndexString(); | ||
terms.add(term); | ||
docTerms[i] = "\"" + term + "\""; | ||
} | ||
builder.add( | ||
new IndexRequest("test").source( | ||
"{\"wildcard\" : " + Arrays.toString(docTerms) + ", \"keyword\" : " + Arrays.toString(docTerms) + "}", | ||
XContentType.JSON | ||
) | ||
); | ||
} | ||
|
||
public void testTermQueryDuel() { | ||
for (int i = 0; i < 50; i++) { | ||
String term = randomQueryString(terms); | ||
TermQueryBuilder termQueryBuilder1 = new TermQueryBuilder("wildcard", term); | ||
TermQueryBuilder termQueryBuilder2 = new TermQueryBuilder("keyword", term); | ||
assertResponse( | ||
client().prepareSearch("test").setQuery(termQueryBuilder1), | ||
response -> assertResponse( | ||
client().prepareSearch("test").setQuery(termQueryBuilder2), | ||
response2 -> assertThat( | ||
response.getHits().getTotalHits().value(), | ||
Matchers.equalTo(response2.getHits().getTotalHits().value()) | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
public void testTermsQueryDuel() { | ||
for (int i = 0; i < 10; i++) { | ||
String[] terms = new String[randomIntBetween(2, 8192)]; | ||
for (int j = 0; j < terms.length; j++) { | ||
terms[j] = randomQueryString(this.terms); | ||
} | ||
TermsQueryBuilder termsQueryBuilder1 = new TermsQueryBuilder("wildcard", terms); | ||
TermsQueryBuilder termsQueryBuilder2 = new TermsQueryBuilder("keyword", terms); | ||
assertResponse( | ||
client().prepareSearch("test").setQuery(termsQueryBuilder1), | ||
response -> assertResponse( | ||
client().prepareSearch("test").setQuery(termsQueryBuilder2), | ||
response2 -> assertThat( | ||
response.getHits().getTotalHits().value(), | ||
Matchers.equalTo(response2.getHits().getTotalHits().value()) | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
private static String randomIndexString() { | ||
String string = randomAlphaOfLength(randomIntBetween(0, 30)); | ||
if (rarely()) { | ||
return string + "*"; | ||
} else if (rarely()) { | ||
return "*" + string; | ||
} else if (rarely()) { | ||
return "*" + string + "*"; | ||
} else { | ||
return string; | ||
} | ||
} | ||
|
||
private static String randomQueryString(List<String> terms) { | ||
if (rarely()) { | ||
return terms.get(randomIntBetween(0, terms.size() - 1)); | ||
} else if (randomBoolean()) { | ||
return randomAlphaOfLength(randomIntBetween(0, 30)); | ||
} else { | ||
return randomAlphaOfLength(1) + "*"; | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
How long this test will run in number of terms is 8192? Is the performance acceptable to be included as a regular test?
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.
It is very fast, the full suite takes a couple of seconds.