Skip to content

Commit 42c83cd

Browse files
authored
[8.15] Update Semantic Query To Handle Zero Size Responses (#116277) (#116983)
* Update Semantic Query To Handle Zero Size Responses (#116277) (cherry picked from commit 29659fd) # Conflicts: # x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java # x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java # x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/40_semantic_text_query.yml * Fix spotless error
1 parent 1d55a0a commit 42c83cd

File tree

5 files changed

+132
-4
lines changed

5 files changed

+132
-4
lines changed

docs/changelog/116277.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 116277
2+
summary: Update Semantic Query To Handle Zero Size Responses
3+
area: Vector Search
4+
type: bug
5+
issues:
6+
- 116083

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public Set<NodeFeature> getFeatures() {
2626

2727
@Override
2828
public Set<NodeFeature> getTestFeatures() {
29-
return Set.of(SemanticTextFieldMapper.SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX);
29+
return Set.of(SemanticTextFieldMapper.SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX, SemanticTextFieldMapper.SEMANTIC_TEXT_ZERO_SIZE_FIX);
3030
}
3131
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import java.util.Set;
6767
import java.util.function.Function;
6868

69+
import static org.elasticsearch.search.SearchService.DEFAULT_SIZE;
6970
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_EMBEDDINGS_FIELD;
7071
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_TEXT_FIELD;
7172
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKS_FIELD;
@@ -81,6 +82,7 @@
8182
*/
8283
public class SemanticTextFieldMapper extends FieldMapper implements InferenceFieldMapper {
8384
public static final NodeFeature SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX = new NodeFeature("semantic_text.in_object_field_fix");
85+
public static final NodeFeature SEMANTIC_TEXT_ZERO_SIZE_FIX = new NodeFeature("semantic_text.zero_size_fix");
8486

8587
public static final String CONTENT_TYPE = "semantic_text";
8688

@@ -427,7 +429,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
427429
throw new IllegalArgumentException("[semantic_text] fields do not support sorting, scripting or aggregating");
428430
}
429431

430-
public QueryBuilder semanticQuery(InferenceResults inferenceResults, float boost, String queryName) {
432+
public QueryBuilder semanticQuery(InferenceResults inferenceResults, Integer requestSize, float boost, String queryName) {
431433
String nestedFieldPath = getChunksFieldName(name());
432434
String inferenceResultsFieldName = getEmbeddingsFieldName(name());
433435
QueryBuilder childQueryBuilder;
@@ -491,7 +493,13 @@ public QueryBuilder semanticQuery(InferenceResults inferenceResults, float boost
491493
);
492494
}
493495

494-
yield new KnnVectorQueryBuilder(inferenceResultsFieldName, inference, null, null, null);
496+
Integer k = requestSize;
497+
if (k != null) {
498+
// Ensure that k is at least the default size so that aggregations work when size is set to 0 in the request
499+
k = Math.max(k, DEFAULT_SIZE);
500+
}
501+
502+
yield new KnnVectorQueryBuilder(inferenceResultsFieldName, inference, k, null, null);
495503
}
496504
default -> throw new IllegalStateException(
497505
"Field ["

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private QueryBuilder doRewriteBuildSemanticQuery(SearchExecutionContext searchEx
166166
);
167167
}
168168

169-
return semanticTextFieldType.semanticQuery(inferenceResults, boost(), queryName());
169+
return semanticTextFieldType.semanticQuery(inferenceResults, searchExecutionContext.requestSize(), boost(), queryName());
170170
} else {
171171
throw new IllegalArgumentException(
172172
"Field [" + fieldName + "] of type [" + fieldType.typeName() + "] does not support " + NAME + " queries"

x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/40_semantic_text_query.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,117 @@ setup:
477477

478478
- match: { error.type: "resource_not_found_exception" }
479479
- match: { error.reason: "Inference endpoint not found [invalid-inference-id]" }
480+
481+
---
482+
"Query using a sparse embedding model with size set to zero":
483+
- requires:
484+
cluster_features: "semantic_text.zero_size_fix"
485+
reason: zero size fix added in 8.16.1 & 8.15.5
486+
487+
- do:
488+
indices.create:
489+
index: test-sparse-index-with-agg-id
490+
body:
491+
mappings:
492+
properties:
493+
inference_field:
494+
type: semantic_text
495+
inference_id: sparse-inference-id
496+
non_inference_field:
497+
type: text
498+
agg_id:
499+
type: keyword
500+
501+
- do:
502+
index:
503+
index: test-sparse-index-with-agg-id
504+
id: doc_1
505+
body:
506+
inference_field: "inference test"
507+
agg_id: "doc_1"
508+
509+
- do:
510+
index:
511+
index: test-sparse-index-with-agg-id
512+
id: doc_2
513+
body:
514+
non_inference_field: "non-inference test"
515+
agg_id: "doc_2"
516+
refresh: true
517+
518+
- do:
519+
search:
520+
index: test-sparse-index-with-agg-id
521+
body:
522+
size: 0
523+
query:
524+
semantic:
525+
field: "inference_field"
526+
query: "inference test"
527+
aggs:
528+
agg_ids:
529+
terms:
530+
field: agg_id
531+
532+
- match: { hits.total.value: 1 }
533+
- length: { hits.hits: 0 }
534+
- length: { aggregations.agg_ids.buckets: 1 }
535+
- match: { aggregations.agg_ids.buckets.0.key: "doc_1" }
536+
- match: { aggregations.agg_ids.buckets.0.doc_count: 1 }
537+
538+
---
539+
"Query using a dense embedding model with size set to zero":
540+
- requires:
541+
cluster_features: "semantic_text.zero_size_fix"
542+
reason: zero size fix added in 8.16.1 & 8.15.5
543+
544+
- do:
545+
indices.create:
546+
index: test-dense-index-with-agg-id
547+
body:
548+
mappings:
549+
properties:
550+
inference_field:
551+
type: semantic_text
552+
inference_id: dense-inference-id
553+
non_inference_field:
554+
type: text
555+
agg_id:
556+
type: keyword
557+
558+
- do:
559+
index:
560+
index: test-dense-index-with-agg-id
561+
id: doc_1
562+
body:
563+
inference_field: "inference test"
564+
agg_id: "doc_1"
565+
566+
- do:
567+
index:
568+
index: test-dense-index-with-agg-id
569+
id: doc_2
570+
body:
571+
non_inference_field: "non-inference test"
572+
agg_id: "doc_2"
573+
refresh: true
574+
575+
- do:
576+
search:
577+
index: test-dense-index-with-agg-id
578+
body:
579+
size: 0
580+
query:
581+
semantic:
582+
field: "inference_field"
583+
query: "inference test"
584+
aggs:
585+
agg_ids:
586+
terms:
587+
field: agg_id
588+
589+
- match: { hits.total.value: 1 }
590+
- length: { hits.hits: 0 }
591+
- length: { aggregations.agg_ids.buckets: 1 }
592+
- match: { aggregations.agg_ids.buckets.0.key: "doc_1" }
593+
- match: { aggregations.agg_ids.buckets.0.doc_count: 1 }

0 commit comments

Comments
 (0)