Skip to content

Commit fab91a4

Browse files
committed
Trying to add a PrefilteredQueryBuilder as a wrapper - does not work
1 parent a01e4bc commit fab91a4

File tree

16 files changed

+424
-47
lines changed

16 files changed

+424
-47
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
initial_elasticsearch_8_18_8,8840010
1+
transform_check_for_dangling_tasks,8840011
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
initial_elasticsearch_8_19_5,8841069
1+
transform_check_for_dangling_tasks,8841070
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
initial_elasticsearch_9_0_8,9000017
1+
transform_check_for_dangling_tasks,9000018
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
initial_elasticsearch_9_1_5,9112008
1+
transform_check_for_dangling_tasks,9112009
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
semantic_query_filter_queries,9169000
1+
prefiltered_query_builder,9171000

x-pack/plugin/esql/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
exclude(group: "org.elasticsearch.plugin", module: "esql")
5454
}
5555
testImplementation project(':test:framework')
56+
testImplementation testArtifact(project(':server'))
5657
testImplementation(testArtifact(project(xpackModule('core'))))
5758
testImplementation project(path: xpackModule('enrich'))
5859
testImplementation project(path: xpackModule('spatial'))

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/SemanticMatchTestCase.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,41 @@ public void testCopyToWithSemanticText() throws IOException {
9292
);
9393
}
9494

95+
public void testMatchWithAndPrefilter() throws IOException {
96+
assumeTrue("semantic text capability not available", EsqlCapabilities.Cap.SEMANTIC_TEXT_FIELD_CAPS.isEnabled());
97+
98+
StringBuilder bulkRequestBody = new StringBuilder();
99+
int specificValueCount = 0;
100+
for (int i = 0; i < 100; i++) {
101+
bulkRequestBody.append("{\"index\":{\"_index\":\"test-semantic-prefilter\"}}\n");
102+
String prefilterValue = "other_value";
103+
if (i % 10 == 0) {
104+
prefilterValue = "specific_value";
105+
specificValueCount++;
106+
}
107+
bulkRequestBody.append("{\"semantic_text_field\":\"inference test\", \"prefilter_field\":\"")
108+
.append(prefilterValue)
109+
.append("\"}\n");
110+
}
111+
112+
Request bulkRequest = new Request("POST", "/_bulk");
113+
bulkRequest.addParameter("refresh", "true");
114+
bulkRequest.setJsonEntity(bulkRequestBody.toString());
115+
assertEquals(200, adminClient().performRequest(bulkRequest).getStatusLine().getStatusCode());
116+
117+
String query = """
118+
FROM test-semantic-prefilter
119+
| WHERE MATCH(semantic_text_field, "something") AND prefilter_field == "specific_value"
120+
| LIMIT 10
121+
""";
122+
Map<String, Object> result = runEsqlQuery(query);
123+
124+
@SuppressWarnings("unchecked")
125+
List<List<Object>> values = (List<List<Object>>) result.get("values");
126+
assertNotNull(values);
127+
assertEquals(specificValueCount, values.size());
128+
}
129+
95130
@Before
96131
public void setUpIndices() throws IOException {
97132
var settings = Settings.builder().build();
@@ -139,6 +174,19 @@ public void setUpIndices() throws IOException {
139174
}
140175
""";
141176
createIndex(adminClient(), "test-semantic4", settings, mapping4);
177+
178+
String mappingFilter = """
179+
"properties": {
180+
"semantic_text_field": {
181+
"type": "semantic_text",
182+
"inference_id": "test_dense_inference"
183+
},
184+
"prefilter_field": {
185+
"type": "keyword"
186+
}
187+
}
188+
""";
189+
createIndex(adminClient(), "test-semantic-prefilter", settings, mappingFilter);
142190
}
143191

144192
@Before

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public Translatable translatable(LucenePushdownPredicates pushdownPredicates) {
164164
}
165165

166166
@Override
167-
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
167+
public final Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
168168
return queryBuilder != null ? new TranslationAwareExpressionQuery(source(), queryBuilder) : translate(pushdownPredicates, handler);
169169
}
170170

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/Match.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,6 @@ public Expression withPrefilters(List<Expression> filterExpressions) {
416416
return new Match(source(), field(), query(), options(), queryBuilder(), filterExpressions);
417417
}
418418

419-
@Override
420-
protected Expression replaceFilteredQueryBuilder(QueryBuilder queryBuilder) {
421-
return new Match(source(), field, query(), options(), queryBuilder, prefilterExpressions());
422-
}
423-
424419
@Override
425420
public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() {
426421
return (plan, failures) -> {
@@ -455,14 +450,20 @@ public Object queryAsObject() {
455450
}
456451

457452
@Override
458-
protected Query translate(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
453+
protected Query translateInnerQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
459454
var fieldAttribute = fieldAsFieldAttribute();
460455
Check.notNull(fieldAttribute, "Match must have a field attribute as the first argument");
461456
String fieldName = getNameFromFieldAttribute(fieldAttribute);
462457
// Make query lenient so mixed field types can be queried when a field type is incompatible with the value provided
458+
463459
return new MatchQuery(source(), fieldName, queryAsObject(), matchQueryOptions());
464460
}
465461

462+
@Override
463+
public Expression replaceQueryBuilder(QueryBuilder queryBuilder) {
464+
return new Match(source(), field(), query(), options(), queryBuilder, prefilterExpressions());
465+
}
466+
466467
private FieldAttribute fieldAsFieldAttribute() {
467468
return fieldAsFieldAttribute(field);
468469
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Expression replaceChildren(List<Expression> newChildren) {
9292
}
9393

9494
@Override
95-
public Expression replaceFilteredQueryBuilder(QueryBuilder queryBuilder) {
95+
public Expression replaceQueryBuilder(QueryBuilder queryBuilder) {
9696
return new MatchOperator(source(), field, query(), queryBuilder, prefilterExpressions());
9797
}
9898
}

0 commit comments

Comments
 (0)