Skip to content

Commit

Permalink
Always Emit Inference ID in Semantic Text Mapping (elastic#117294)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikep86 authored and alexey-ivanov-es committed Nov 28, 2024
1 parent a59ba8d commit ba4fd86
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/117294.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 117294
summary: Always Emit Inference ID in Semantic Text Mapping
area: Mapping
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public Set<NodeFeature> getTestFeatures() {
SemanticTextFieldMapper.SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX,
SemanticTextFieldMapper.SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX,
SemanticTextFieldMapper.SEMANTIC_TEXT_DELETE_FIX,
SemanticTextFieldMapper.SEMANTIC_TEXT_ZERO_SIZE_FIX
SemanticTextFieldMapper.SEMANTIC_TEXT_ZERO_SIZE_FIX,
SemanticTextFieldMapper.SEMANTIC_TEXT_ALWAYS_EMIT_INFERENCE_ID_FIX
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public class SemanticTextFieldMapper extends FieldMapper implements InferenceFie
public static final NodeFeature SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX = new NodeFeature("semantic_text.single_field_update_fix");
public static final NodeFeature SEMANTIC_TEXT_DELETE_FIX = new NodeFeature("semantic_text.delete_fix");
public static final NodeFeature SEMANTIC_TEXT_ZERO_SIZE_FIX = new NodeFeature("semantic_text.zero_size_fix");
public static final NodeFeature SEMANTIC_TEXT_ALWAYS_EMIT_INFERENCE_ID_FIX = new NodeFeature(
"semantic_text.always_emit_inference_id_fix"
);

public static final String CONTENT_TYPE = "semantic_text";
public static final String DEFAULT_ELSER_2_INFERENCE_ID = DEFAULT_ELSER_ID;
Expand All @@ -119,7 +122,7 @@ public static class Builder extends FieldMapper.Builder {
"[" + INFERENCE_ID_FIELD + "] on mapper [" + leafName() + "] of type [" + CONTENT_TYPE + "] must not be empty"
);
}
});
}).alwaysSerialize();

private final Parameter<String> searchInferenceId = Parameter.stringParam(
SEARCH_INFERENCE_ID_FIELD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ protected String minimalIsInvalidRoutingPathErrorMessage(Mapper mapper) {
return "cannot have nested fields when index is in [index.mode=time_series]";
}

@Override
protected void metaMapping(XContentBuilder b) throws IOException {
super.metaMapping(b);
b.field(INFERENCE_ID_FIELD, DEFAULT_ELSER_2_INFERENCE_ID);
}

@Override
protected Object getSampleValueForDocument() {
return null;
Expand Down Expand Up @@ -166,10 +172,11 @@ protected void assertSearchable(MappedFieldType fieldType) {
public void testDefaults() throws Exception {
final String fieldName = "field";
final XContentBuilder fieldMapping = fieldMapping(this::minimalMapping);
final XContentBuilder expectedMapping = fieldMapping(this::metaMapping);

MapperService mapperService = createMapperService(fieldMapping);
DocumentMapper mapper = mapperService.documentMapper();
assertEquals(Strings.toString(fieldMapping), mapper.mappingSource().toString());
assertEquals(Strings.toString(expectedMapping), mapper.mappingSource().toString());
assertSemanticTextField(mapperService, fieldName, false);
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, DEFAULT_ELSER_2_INFERENCE_ID);

Expand Down Expand Up @@ -208,10 +215,15 @@ public void testSetInferenceEndpoints() throws IOException {
final XContentBuilder fieldMapping = fieldMapping(
b -> b.field("type", "semantic_text").field(SEARCH_INFERENCE_ID_FIELD, searchInferenceId)
);
final XContentBuilder expectedMapping = fieldMapping(
b -> b.field("type", "semantic_text")
.field(INFERENCE_ID_FIELD, DEFAULT_ELSER_2_INFERENCE_ID)
.field(SEARCH_INFERENCE_ID_FIELD, searchInferenceId)
);
final MapperService mapperService = createMapperService(fieldMapping);
assertSemanticTextField(mapperService, fieldName, false);
assertInferenceEndpoints(mapperService, fieldName, DEFAULT_ELSER_2_INFERENCE_ID, searchInferenceId);
assertSerialization.accept(fieldMapping, mapperService);
assertSerialization.accept(expectedMapping, mapperService);
}
{
final XContentBuilder fieldMapping = fieldMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,29 @@ setup:
- match: { error.type: illegal_argument_exception }
- match: { error.reason: "semantic_text field [level_1.level_2.sparse_field] cannot be in an object field with
subobjects disabled" }

---
"Mapping always includes inference ID":
- requires:
cluster_features: "semantic_text.always_emit_inference_id_fix"
reason: always emit inference ID fix added in 8.17.0
test_runner_features: [capabilities]
capabilities:
- method: GET
path: /_inference
capabilities: [default_elser_2]

- do:
indices.create:
index: test-always-include-inference-id-index
body:
mappings:
properties:
semantic_field:
type: semantic_text

- do:
indices.get_mapping:
index: test-always-include-inference-id-index

- exists: test-always-include-inference-id-index.mappings.properties.semantic_field.inference_id

0 comments on commit ba4fd86

Please sign in to comment.