Skip to content

Commit

Permalink
remove the inference metadata field mapper and implements all the log…
Browse files Browse the repository at this point in the history
…ic in the semantic text field mapper
  • Loading branch information
jimczi committed Apr 3, 2024
1 parent b6ca8d2 commit cfe1457
Show file tree
Hide file tree
Showing 15 changed files with 1,222 additions and 1,683 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.index.mapper.InferenceFieldMapper;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.IndicesService;
Expand Down Expand Up @@ -185,7 +184,7 @@ protected void shardOperation(final UpdateRequest request, final ActionListener<
final UpdateHelper.Result result = updateHelper.prepare(request, indexShard, threadPool::absoluteTimeInMillis);
switch (result.getResponseResult()) {
case CREATED -> {
IndexRequest upsertRequest = removeInferenceMetadataField(indexService, result.action());
IndexRequest upsertRequest = result.action();
// we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
final BytesReference upsertSourceBytes = upsertRequest.source();
client.bulk(
Expand Down Expand Up @@ -227,7 +226,7 @@ protected void shardOperation(final UpdateRequest request, final ActionListener<
);
}
case UPDATED -> {
IndexRequest indexRequest = removeInferenceMetadataField(indexService, result.action());
IndexRequest indexRequest = result.action();
// we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
final BytesReference indexSourceBytes = indexRequest.source();
client.bulk(
Expand Down Expand Up @@ -336,15 +335,4 @@ private void handleUpdateFailureWithRetry(
}
listener.onFailure(cause instanceof Exception ? (Exception) cause : new NotSerializableExceptionWrapper(cause));
}

private IndexRequest removeInferenceMetadataField(IndexService service, IndexRequest request) {
var inferenceMetadata = service.getIndexSettings().getIndexMetadata().getInferenceFields();
if (inferenceMetadata.isEmpty()) {
return request;
}
Map<String, Object> docMap = request.sourceAsMap();
docMap.remove(InferenceFieldMapper.NAME);
request.source(docMap);
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InferenceFieldMetadata that = (InferenceFieldMetadata) o;
return inferenceId.equals(that.inferenceId) && Arrays.equals(sourceFields, that.sourceFields);
return Objects.equals(name, that.name)
&& Objects.equals(inferenceId, that.inferenceId)
&& Arrays.equals(sourceFields, that.sourceFields);
}

@Override
public int hashCode() {
int result = Objects.hash(inferenceId);
int result = Objects.hash(name, inferenceId);
result = 31 * result + Arrays.hashCode(sourceFields);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ private static void failIfMatchesRoutingPath(DocumentParserContext context, Stri
*/
private static void parseCopyFields(DocumentParserContext context, List<String> copyToFields) throws IOException {
for (String field : copyToFields) {
if (context.mappingLookup().getMapper(field) instanceof InferenceFieldMapper) {
// ignore copy_to that targets inference fields, values are already extracted in the coordinating node to perform inference.
continue;
}
// In case of a hierarchy of nested documents, we need to figure out
// which document the field should go to
LuceneDocument targetDoc = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.features.NodeFeature;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.indices.SystemIndexDescriptor;
import org.elasticsearch.inference.InferenceServiceExtension;
import org.elasticsearch.inference.InferenceServiceRegistry;
Expand Down Expand Up @@ -55,7 +54,6 @@
import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSender;
import org.elasticsearch.xpack.inference.external.http.sender.RequestExecutorServiceSettings;
import org.elasticsearch.xpack.inference.logging.ThrottlerManager;
import org.elasticsearch.xpack.inference.mapper.InferenceMetadataFieldMapper;
import org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper;
import org.elasticsearch.xpack.inference.registry.ModelRegistry;
import org.elasticsearch.xpack.inference.rest.RestDeleteInferenceModelAction;
Expand Down Expand Up @@ -282,14 +280,6 @@ public Map<String, Mapper.TypeParser> getMappers() {
return Map.of();
}

@Override
public Map<String, MetadataFieldMapper.TypeParser> getMetadataMappers() {
if (SemanticTextFeature.isEnabled()) {
return Map.of(InferenceMetadataFieldMapper.NAME, InferenceMetadataFieldMapper.PARSER);
}
return Map.of();
}

@Override
public Collection<ActionFilter> getActionFilters() {
if (SemanticTextFeature.isEnabled()) {
Expand Down
Loading

0 comments on commit cfe1457

Please sign in to comment.