-
Notifications
You must be signed in to change notification settings - Fork 25.3k
ESQL: Add a random sample command #123879
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36acb35
Add a random sample commadn
bpintea 7abf28d
Update docs/changelog/123879.yaml
15afc08
[CI] Auto commit changes from spotless
elasticsearchmachine 3cbd508
Add non-operator-related tests
bpintea 0f9600c
Make seed parameter optional. Various fixes
bpintea e5de4bf
Merge remote-tracking branch 'upstream/main' into feat/random_sample
bpintea 213634e
[CI] Auto commit changes from spotless
elasticsearchmachine 8fbc684
Merge remote-tracking branch 'upstream/main' into feat/random_sample
bpintea 0063737
Make CsvTests more node-count-induced variation tollerant
bpintea 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,5 @@ | ||
pr: 123879 | ||
summary: Add a random sample command | ||
area: ES|QL | ||
type: feature | ||
issues: [] |
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
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
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
149 changes: 149 additions & 0 deletions
149
...g/elasticsearch/search/aggregations/bucket/sampler/random/RandomSamplingQueryBuilder.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,149 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.sampler.random; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.TransportVersion; | ||
import org.elasticsearch.TransportVersions; | ||
import org.elasticsearch.common.Randomness; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.index.query.AbstractQueryBuilder; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.xcontent.ParseField; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.search.aggregations.bucket.sampler.random.RandomSamplingQuery.checkProbabilityRange; | ||
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
public class RandomSamplingQueryBuilder extends AbstractQueryBuilder<RandomSamplingQueryBuilder> { | ||
|
||
public static final String NAME = "random_sampling"; | ||
static final ParseField PROBABILITY = new ParseField("query"); | ||
static final ParseField SEED = new ParseField("seed"); | ||
static final ParseField HASH = new ParseField("hash"); | ||
|
||
private final double probability; | ||
private int seed = Randomness.get().nextInt(); | ||
private int hash = 0; | ||
|
||
public RandomSamplingQueryBuilder(double probability) { | ||
checkProbabilityRange(probability); | ||
this.probability = probability; | ||
} | ||
|
||
public RandomSamplingQueryBuilder seed(int seed) { | ||
checkProbabilityRange(probability); | ||
this.seed = seed; | ||
return this; | ||
} | ||
|
||
public RandomSamplingQueryBuilder(StreamInput in) throws IOException { | ||
super(in); | ||
this.probability = in.readDouble(); | ||
this.seed = in.readInt(); | ||
this.hash = in.readInt(); | ||
} | ||
|
||
public RandomSamplingQueryBuilder hash(Integer hash) { | ||
this.hash = hash; | ||
return this; | ||
} | ||
|
||
public double probability() { | ||
return probability; | ||
} | ||
|
||
public int seed() { | ||
return seed; | ||
} | ||
|
||
public int hash() { | ||
return hash; | ||
} | ||
|
||
@Override | ||
protected void doWriteTo(StreamOutput out) throws IOException { | ||
out.writeDouble(probability); | ||
out.writeInt(seed); | ||
out.writeInt(hash); | ||
} | ||
|
||
@Override | ||
protected void doXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(NAME); | ||
builder.field(PROBABILITY.getPreferredName(), probability); | ||
builder.field(SEED.getPreferredName(), seed); | ||
builder.field(HASH.getPreferredName(), hash); | ||
builder.endObject(); | ||
} | ||
|
||
private static final ConstructingObjectParser<RandomSamplingQueryBuilder, Void> PARSER = new ConstructingObjectParser<>( | ||
NAME, | ||
false, | ||
args -> { | ||
var randomSamplingQueryBuilder = new RandomSamplingQueryBuilder((double) args[0]); | ||
if (args[1] != null) { | ||
randomSamplingQueryBuilder.seed((int) args[1]); | ||
} | ||
if (args[2] != null) { | ||
randomSamplingQueryBuilder.hash((int) args[2]); | ||
} | ||
return randomSamplingQueryBuilder; | ||
} | ||
); | ||
|
||
static { | ||
PARSER.declareDouble(constructorArg(), PROBABILITY); | ||
PARSER.declareInt(optionalConstructorArg(), SEED); | ||
PARSER.declareInt(optionalConstructorArg(), HASH); | ||
} | ||
|
||
public static RandomSamplingQueryBuilder fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
@Override | ||
protected Query doToQuery(SearchExecutionContext context) throws IOException { | ||
return new RandomSamplingQuery(probability, seed, hash); | ||
} | ||
|
||
@Override | ||
protected boolean doEquals(RandomSamplingQueryBuilder other) { | ||
return probability == other.probability && seed == other.seed && hash == other.hash; | ||
} | ||
|
||
@Override | ||
protected int doHashCode() { | ||
return Objects.hash(probability, seed, hash); | ||
} | ||
|
||
/** | ||
* Returns the name of the writeable object | ||
*/ | ||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
/** | ||
* The minimal version of the recipient this object can be sent to | ||
*/ | ||
@Override | ||
public TransportVersion getMinimalSupportedVersion() { | ||
return TransportVersions.RANDOM_SAMPLER_QUERY_BUILDER; | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
...sticsearch/search/aggregations/bucket/sampler/random/RandomSamplingQueryBuilderTests.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,75 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.sampler.random; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.test.AbstractQueryTestCase; | ||
import org.elasticsearch.xcontent.XContentParseException; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class RandomSamplingQueryBuilderTests extends AbstractQueryTestCase<RandomSamplingQueryBuilder> { | ||
|
||
@Override | ||
protected RandomSamplingQueryBuilder doCreateTestQueryBuilder() { | ||
double probability = randomDoubleBetween(0.0, 1.0, false); | ||
var builder = new RandomSamplingQueryBuilder(probability); | ||
if (randomBoolean()) { | ||
builder.seed(randomInt()); | ||
} | ||
if (randomBoolean()) { | ||
builder.hash(randomInt()); | ||
} | ||
return builder; | ||
} | ||
|
||
@Override | ||
protected void doAssertLuceneQuery(RandomSamplingQueryBuilder queryBuilder, Query query, SearchExecutionContext context) | ||
throws IOException { | ||
var rsQuery = asInstanceOf(RandomSamplingQuery.class, query); | ||
assertThat(rsQuery.probability(), equalTo(queryBuilder.probability())); | ||
assertThat(rsQuery.seed(), equalTo(queryBuilder.seed())); | ||
assertThat(rsQuery.hash(), equalTo(queryBuilder.hash())); | ||
} | ||
|
||
@Override | ||
protected boolean supportsBoost() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean supportsQueryName() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void testUnknownField() { | ||
var json = "{ \"" | ||
+ RandomSamplingQueryBuilder.NAME | ||
+ "\" : {\"bogusField\" : \"someValue\", \"" | ||
+ RandomSamplingQueryBuilder.PROBABILITY.getPreferredName() | ||
+ "\" : \"" | ||
+ randomBoolean() | ||
+ "\", \"" | ||
+ RandomSamplingQueryBuilder.SEED.getPreferredName() | ||
+ "\" : \"" | ||
+ randomInt() | ||
+ "\", \"" | ||
+ RandomSamplingQueryBuilder.HASH.getPreferredName() | ||
+ "\" : \"" | ||
+ randomInt() | ||
+ "\" } }"; | ||
var e = expectThrows(XContentParseException.class, () -> parseQuery(json)); | ||
assertTrue(e.getMessage().contains("bogusField")); | ||
} | ||
} |
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
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
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.
👍