Skip to content

Commit baddfd0

Browse files
authored
Merge branch 'main' into issue-302-find-text-structure
2 parents 9d0bc16 + a0cc698 commit baddfd0

File tree

13 files changed

+84
-58
lines changed

13 files changed

+84
-58
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.elasticsearch.common.lucene.Lucene;
3434
import org.elasticsearch.common.unit.Fuzziness;
3535
import org.elasticsearch.index.IndexVersion;
36-
import org.elasticsearch.index.IndexVersions;
3736
import org.elasticsearch.index.analysis.IndexAnalyzers;
3837
import org.elasticsearch.index.analysis.NamedAnalyzer;
3938
import org.elasticsearch.index.fielddata.FieldDataContext;
@@ -71,6 +70,8 @@
7170
import java.util.Objects;
7271
import java.util.Set;
7372

73+
import static org.elasticsearch.index.mapper.TextFieldMapper.Builder.multiFieldsNotStoredByDefaultIndexVersionCheck;
74+
7475
/**
7576
* A {@link FieldMapper} for full-text fields that only indexes
7677
* {@link IndexOptions#DOCS} and runs positional queries by looking at the
@@ -140,7 +141,7 @@ private MatchOnlyTextFieldType buildFieldType(MapperBuilderContext context) {
140141
public MatchOnlyTextFieldMapper build(MapperBuilderContext context) {
141142
MatchOnlyTextFieldType tft = buildFieldType(context);
142143
final boolean storeSource;
143-
if (indexCreatedVersion.onOrAfter(IndexVersions.MAPPER_TEXT_MATCH_ONLY_MULTI_FIELDS_DEFAULT_NOT_STORED)) {
144+
if (multiFieldsNotStoredByDefaultIndexVersionCheck(indexCreatedVersion)) {
144145
storeSource = context.isSourceSynthetic()
145146
&& withinMultiField == false
146147
&& multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;

server/src/main/java/org/elasticsearch/index/IndexVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private static Version parseUnchecked(String version) {
142142
public static final IndexVersion DEFAULT_OVERSAMPLE_VALUE_FOR_BBQ_BACKPORT_8_X = def(8_530_0_00, Version.LUCENE_9_12_1);
143143
public static final IndexVersion SEMANTIC_TEXT_DEFAULTS_TO_BBQ_BACKPORT_8_X = def(8_531_0_00, Version.LUCENE_9_12_1);
144144
public static final IndexVersion INDEX_INT_SORT_INT_TYPE_8_19 = def(8_532_0_00, Version.LUCENE_9_12_1);
145+
public static final IndexVersion MAPPER_TEXT_MATCH_ONLY_MULTI_FIELDS_DEFAULT_NOT_STORED_8_19 = def(8_533_0_00, Version.LUCENE_9_12_1);
145146
public static final IndexVersion UPGRADE_TO_LUCENE_10_0_0 = def(9_000_0_00, Version.LUCENE_10_0_0);
146147
public static final IndexVersion LOGSDB_DEFAULT_IGNORE_DYNAMIC_BEYOND_LIMIT = def(9_001_0_00, Version.LUCENE_10_0_0);
147148
public static final IndexVersion TIME_BASED_K_ORDERED_DOC_ID = def(9_002_0_00, Version.LUCENE_10_0_0);

server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public Builder(
311311
// Note that if current builder is a multi field, then we don't need to store, given that responsibility lies with parent field
312312
this.withinMultiField = withinMultiField;
313313
this.store = Parameter.storeParam(m -> ((TextFieldMapper) m).store, () -> {
314-
if (indexCreatedVersion.onOrAfter(IndexVersions.MAPPER_TEXT_MATCH_ONLY_MULTI_FIELDS_DEFAULT_NOT_STORED)) {
314+
if (multiFieldsNotStoredByDefaultIndexVersionCheck(indexCreatedVersion)) {
315315
return isSyntheticSourceEnabled
316316
&& this.withinMultiField == false
317317
&& multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
@@ -329,6 +329,14 @@ public Builder(
329329
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
330330
}
331331

332+
public static boolean multiFieldsNotStoredByDefaultIndexVersionCheck(IndexVersion indexCreatedVersion) {
333+
return indexCreatedVersion.onOrAfter(IndexVersions.MAPPER_TEXT_MATCH_ONLY_MULTI_FIELDS_DEFAULT_NOT_STORED)
334+
|| indexCreatedVersion.between(
335+
IndexVersions.MAPPER_TEXT_MATCH_ONLY_MULTI_FIELDS_DEFAULT_NOT_STORED_8_19,
336+
IndexVersions.UPGRADE_TO_LUCENE_10_0_0
337+
);
338+
}
339+
332340
public Builder index(boolean index) {
333341
this.index.setValue(index);
334342
return this;

x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,5 @@ completionCommand
316316
;
317317

318318
sampleCommand
319-
: DEV_SAMPLE probability=decimalValue
319+
: DEV_SAMPLE probability=constant
320320
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ public enum Cap {
11681168
LUCENE_QUERY_EVALUATOR_QUERY_REWRITE,
11691169

11701170
/**
1171-
* Support parameters for LiMIT command.
1171+
* Support parameters for LIMIT command.
11721172
*/
11731173
PARAMETER_FOR_LIMIT,
11741174

@@ -1202,7 +1202,12 @@ public enum Cap {
12021202
*/
12031203
KNN_FUNCTION(Build.current().isSnapshot()),
12041204

1205-
LIKE_WITH_LIST_OF_PATTERNS;
1205+
LIKE_WITH_LIST_OF_PATTERNS,
1206+
1207+
/**
1208+
* Support parameters for SAMPLE command.
1209+
*/
1210+
PARAMETER_FOR_SAMPLE(Build.current().isSnapshot());
12061211

12071212
private final boolean enabled;
12081213

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.interp

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.java

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import java.util.function.Function;
8989

9090
import static java.util.Collections.emptyList;
91+
import static org.elasticsearch.xpack.esql.common.Failure.fail;
9192
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
9293
import static org.elasticsearch.xpack.esql.core.util.StringUtils.WILDCARD;
9394
import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputExpressions;
@@ -783,7 +784,15 @@ public Literal inferenceId(EsqlBaseParser.IdentifierOrParameterContext ctx) {
783784
}
784785

785786
public PlanFactory visitSampleCommand(EsqlBaseParser.SampleCommandContext ctx) {
786-
var probability = visitDecimalValue(ctx.probability);
787-
return plan -> new Sample(source(ctx), probability, plan);
787+
Source source = source(ctx);
788+
Object val = expression(ctx.probability).fold(FoldContext.small() /* TODO remove me */);
789+
if (val instanceof Double probability && probability > 0.0 && probability < 1.0) {
790+
return input -> new Sample(source, new Literal(source, probability, DataType.DOUBLE), input);
791+
} else {
792+
throw new ParsingException(
793+
source(ctx),
794+
"invalid value for SAMPLE probability [" + val + "], expecting a number between 0 and 1, exclusive"
795+
);
796+
}
788797
}
789798
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Sample.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@
99
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1010
import org.elasticsearch.common.io.stream.StreamInput;
1111
import org.elasticsearch.common.io.stream.StreamOutput;
12-
import org.elasticsearch.search.aggregations.bucket.sampler.random.RandomSamplingQuery;
13-
import org.elasticsearch.xpack.esql.capabilities.PostAnalysisVerificationAware;
1412
import org.elasticsearch.xpack.esql.capabilities.TelemetryAware;
15-
import org.elasticsearch.xpack.esql.common.Failures;
1613
import org.elasticsearch.xpack.esql.core.expression.Expression;
17-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
18-
import org.elasticsearch.xpack.esql.core.expression.Foldables;
1914
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2015
import org.elasticsearch.xpack.esql.core.tree.Source;
2116
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
2217

2318
import java.io.IOException;
2419
import java.util.Objects;
2520

26-
import static org.elasticsearch.xpack.esql.common.Failure.fail;
27-
28-
public class Sample extends UnaryPlan implements TelemetryAware, PostAnalysisVerificationAware {
21+
public class Sample extends UnaryPlan implements TelemetryAware {
2922
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(LogicalPlan.class, "Sample", Sample::new);
3023

3124
private final Expression probability;
@@ -92,13 +85,4 @@ public boolean equals(Object obj) {
9285

9386
return Objects.equals(probability, other.probability) && Objects.equals(child(), other.child());
9487
}
95-
96-
@Override
97-
public void postAnalysisVerification(Failures failures) {
98-
try {
99-
RandomSamplingQuery.checkProbabilityRange((double) Foldables.valueOf(FoldContext.small(), probability));
100-
} catch (IllegalArgumentException e) {
101-
failures.add(fail(probability, e.getMessage()));
102-
}
103-
}
10488
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.defaultEnrichResolution;
119119
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.indexWithDateDateNanosUnionType;
120120
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.loadMapping;
121-
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.randomValueOtherThanTest;
122121
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.tsdbIndexResolution;
123122
import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY;
124123
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
@@ -3465,20 +3464,6 @@ public void testRrfError() {
34653464
assertThat(e.getMessage(), containsString("Unknown column [_id]"));
34663465
}
34673466

3468-
public void testRandomSampleProbability() {
3469-
assumeTrue("requires SAMPLE capability", EsqlCapabilities.Cap.SAMPLE_V3.isEnabled());
3470-
3471-
var e = expectThrows(VerificationException.class, () -> analyze("FROM test | SAMPLE 1."));
3472-
assertThat(e.getMessage(), containsString("RandomSampling probability must be strictly between 0.0 and 1.0, was [1.0]"));
3473-
3474-
e = expectThrows(VerificationException.class, () -> analyze("FROM test | SAMPLE .0"));
3475-
assertThat(e.getMessage(), containsString("RandomSampling probability must be strictly between 0.0 and 1.0, was [0.0]"));
3476-
3477-
double p = randomValueOtherThanTest(d -> 0 < d && d < 1, () -> randomDoubleBetween(0, Double.MAX_VALUE, false));
3478-
e = expectThrows(VerificationException.class, () -> analyze("FROM test | SAMPLE " + p));
3479-
assertThat(e.getMessage(), containsString("RandomSampling probability must be strictly between 0.0 and 1.0, was [" + p + "]"));
3480-
}
3481-
34823467
// TODO There's too much boilerplate involved here! We need a better way of creating FieldCapabilitiesResponses from a mapping or index.
34833468
private static FieldCapabilitiesIndexResponse fieldCapabilitiesIndexResponse(
34843469
String indexName,

0 commit comments

Comments
 (0)