Skip to content

Commit 073b188

Browse files
Mikep86alexey-ivanov-es
authored andcommitted
Update Semantic Query To Handle Zero Size Responses (elastic#116277)
1 parent 80336a7 commit 073b188

File tree

5 files changed

+133
-4
lines changed

5 files changed

+133
-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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public Set<NodeFeature> getTestFeatures() {
3838
return Set.of(
3939
SemanticTextFieldMapper.SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX,
4040
SemanticTextFieldMapper.SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX,
41-
SemanticTextFieldMapper.SEMANTIC_TEXT_DELETE_FIX
41+
SemanticTextFieldMapper.SEMANTIC_TEXT_DELETE_FIX,
42+
SemanticTextFieldMapper.SEMANTIC_TEXT_ZERO_SIZE_FIX
4243
);
4344
}
4445
}

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
@@ -69,6 +69,7 @@
6969
import java.util.Set;
7070
import java.util.function.Function;
7171

72+
import static org.elasticsearch.search.SearchService.DEFAULT_SIZE;
7273
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_EMBEDDINGS_FIELD;
7374
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_TEXT_FIELD;
7475
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKS_FIELD;
@@ -91,6 +92,7 @@ public class SemanticTextFieldMapper extends FieldMapper implements InferenceFie
9192
public static final NodeFeature SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX = new NodeFeature("semantic_text.in_object_field_fix");
9293
public static final NodeFeature SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX = new NodeFeature("semantic_text.single_field_update_fix");
9394
public static final NodeFeature SEMANTIC_TEXT_DELETE_FIX = new NodeFeature("semantic_text.delete_fix");
95+
public static final NodeFeature SEMANTIC_TEXT_ZERO_SIZE_FIX = new NodeFeature("semantic_text.zero_size_fix");
9496

9597
public static final String CONTENT_TYPE = "semantic_text";
9698
public static final String DEFAULT_ELSER_2_INFERENCE_ID = DEFAULT_ELSER_ID;
@@ -507,7 +509,7 @@ public boolean fieldHasValue(FieldInfos fieldInfos) {
507509
return fieldInfos.fieldInfo(getEmbeddingsFieldName(name())) != null;
508510
}
509511

510-
public QueryBuilder semanticQuery(InferenceResults inferenceResults, float boost, String queryName) {
512+
public QueryBuilder semanticQuery(InferenceResults inferenceResults, Integer requestSize, float boost, String queryName) {
511513
String nestedFieldPath = getChunksFieldName(name());
512514
String inferenceResultsFieldName = getEmbeddingsFieldName(name());
513515
QueryBuilder childQueryBuilder;
@@ -551,7 +553,13 @@ public QueryBuilder semanticQuery(InferenceResults inferenceResults, float boost
551553
);
552554
}
553555

554-
yield new KnnVectorQueryBuilder(inferenceResultsFieldName, inference, null, null, null);
556+
Integer k = requestSize;
557+
if (k != null) {
558+
// Ensure that k is at least the default size so that aggregations work when size is set to 0 in the request
559+
k = Math.max(k, DEFAULT_SIZE);
560+
}
561+
562+
yield new KnnVectorQueryBuilder(inferenceResultsFieldName, inference, k, null, null);
555563
}
556564
default -> throw new IllegalStateException(
557565
"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
@@ -170,7 +170,7 @@ private QueryBuilder doRewriteBuildSemanticQuery(SearchExecutionContext searchEx
170170
);
171171
}
172172

173-
return semanticTextFieldType.semanticQuery(inferenceResults, boost(), queryName());
173+
return semanticTextFieldType.semanticQuery(inferenceResults, searchExecutionContext.requestSize(), boost(), queryName());
174174
} else {
175175
throw new IllegalArgumentException(
176176
"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
@@ -878,3 +878,117 @@ setup:
878878

879879
- match: { hits.total.value: 1 }
880880
- match: { hits.hits.0._id: "doc_1" }
881+
882+
---
883+
"Query using a sparse embedding model with size set to zero":
884+
- requires:
885+
cluster_features: "semantic_text.zero_size_fix"
886+
reason: zero size fix added in 8.16.1 & 8.15.5
887+
888+
- do:
889+
indices.create:
890+
index: test-sparse-index-with-agg-id
891+
body:
892+
mappings:
893+
properties:
894+
inference_field:
895+
type: semantic_text
896+
inference_id: sparse-inference-id
897+
non_inference_field:
898+
type: text
899+
agg_id:
900+
type: keyword
901+
902+
- do:
903+
index:
904+
index: test-sparse-index-with-agg-id
905+
id: doc_1
906+
body:
907+
inference_field: "inference test"
908+
agg_id: "doc_1"
909+
910+
- do:
911+
index:
912+
index: test-sparse-index-with-agg-id
913+
id: doc_2
914+
body:
915+
non_inference_field: "non-inference test"
916+
agg_id: "doc_2"
917+
refresh: true
918+
919+
- do:
920+
search:
921+
index: test-sparse-index-with-agg-id
922+
body:
923+
size: 0
924+
query:
925+
semantic:
926+
field: "inference_field"
927+
query: "inference test"
928+
aggs:
929+
agg_ids:
930+
terms:
931+
field: agg_id
932+
933+
- match: { hits.total.value: 1 }
934+
- length: { hits.hits: 0 }
935+
- length: { aggregations.agg_ids.buckets: 1 }
936+
- match: { aggregations.agg_ids.buckets.0.key: "doc_1" }
937+
- match: { aggregations.agg_ids.buckets.0.doc_count: 1 }
938+
939+
---
940+
"Query using a dense embedding model with size set to zero":
941+
- requires:
942+
cluster_features: "semantic_text.zero_size_fix"
943+
reason: zero size fix added in 8.16.1 & 8.15.5
944+
945+
- do:
946+
indices.create:
947+
index: test-dense-index-with-agg-id
948+
body:
949+
mappings:
950+
properties:
951+
inference_field:
952+
type: semantic_text
953+
inference_id: dense-inference-id
954+
non_inference_field:
955+
type: text
956+
agg_id:
957+
type: keyword
958+
959+
- do:
960+
index:
961+
index: test-dense-index-with-agg-id
962+
id: doc_1
963+
body:
964+
inference_field: "inference test"
965+
agg_id: "doc_1"
966+
967+
- do:
968+
index:
969+
index: test-dense-index-with-agg-id
970+
id: doc_2
971+
body:
972+
non_inference_field: "non-inference test"
973+
agg_id: "doc_2"
974+
refresh: true
975+
976+
- do:
977+
search:
978+
index: test-dense-index-with-agg-id
979+
body:
980+
size: 0
981+
query:
982+
semantic:
983+
field: "inference_field"
984+
query: "inference test"
985+
aggs:
986+
agg_ids:
987+
terms:
988+
field: agg_id
989+
990+
- match: { hits.total.value: 1 }
991+
- length: { hits.hits: 0 }
992+
- length: { aggregations.agg_ids.buckets: 1 }
993+
- match: { aggregations.agg_ids.buckets.0.key: "doc_1" }
994+
- match: { aggregations.agg_ids.buckets.0.doc_count: 1 }

0 commit comments

Comments
 (0)