Skip to content

Commit

Permalink
Add getInferenceFieldName to InferenceFieldMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikep86 committed Sep 23, 2024
1 parent b135d5f commit 94ec283
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ public interface InferenceFieldMapper {
Object getOriginalValue(Map<String, Object> sourceAsMap);

/**
* Get the field's embedding name
* Get the field's inference field name
*
* @param fieldName The inference field name
* @return The field's full path to embedding value
* @return The full path to the field's inference field name
*/
static String getInferenceFieldName(String fieldName) {
return fieldName + ".inference";
}
String getInferenceFieldName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.mapper.InferenceFieldMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MappingLookup;
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper;
Expand Down Expand Up @@ -152,20 +153,25 @@ public SourceFilter filter() {
}

public SourceFilter filter(MappingLookup mappingLookup) {
if (filterVectorFields() == Boolean.FALSE) {
if (filterVectorFields() == false) {
return new SourceFilter(includes, excludes);
} else {
if (mappingLookup == null) {
throw new IllegalStateException("MappingLookup must not be null when filtering vectors");
}

String[] excludeFields = excludes();
String[] inferenceFields = mappingLookup.inferenceFields()
.keySet()
.stream()
.map(InferenceFieldMapper::getInferenceFieldName)
.toArray(String[]::new);
excludeFields = ArrayUtils.concat(excludeFields, inferenceFields);
List<String> inferenceFieldNames = new ArrayList<>(mappingLookup.inferenceFields().size());
for (String inferenceField : mappingLookup.inferenceFields().keySet()) {
Mapper mapper = mappingLookup.getMapper(inferenceField);
if (mapper instanceof InferenceFieldMapper == false) {
throw new IllegalStateException("Mapper for field [" + inferenceField + "] is not an inference field mapper");
}

InferenceFieldMapper inferenceFieldMapper = (InferenceFieldMapper) mapper;
inferenceFieldNames.add(inferenceFieldMapper.getInferenceFieldName());
}
excludeFields = ArrayUtils.concat(excludeFields, inferenceFieldNames.toArray(new String[0]));

if (includeVectors != null) {
String[] denseVectors = mappingLookup.getFullNameToFieldType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public Object getOriginalValue(Map<String, Object> sourceAsMap) {
return null;
}

@Override
public String getInferenceFieldName() {
return fullPath() + ".inference";
}

@Override
protected void parseCreateField(DocumentParserContext context) throws IOException {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ public Object getOriginalValue(Map<String, Object> sourceAsMap) {
return XContentMapValues.extractValue(TEXT_FIELD, fieldValueMap);
}

@Override
public String getInferenceFieldName() {
return SemanticTextField.getInferenceFieldName(fullPath());
}

public static class SemanticTextFieldType extends SimpleMappedFieldType {
private final String inferenceId;
private final SemanticTextField.ModelSettings modelSettings;
Expand Down

0 comments on commit 94ec283

Please sign in to comment.