-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semantic text - field mapper #102971
Merged
carlosdelest
merged 20 commits into
elastic:main
from
carlosdelest:carlosdelest/semantic-text-field-mapper
Dec 12, 2023
Merged
Semantic text - field mapper #102971
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ffd86f7
Feature Flag implementation
carlosdelest 085751e
SemanticTextFieldMapper implementation
carlosdelest 03423e3
Added tests
carlosdelest 64efbeb
Spotless
carlosdelest 34360e2
Added javadoc and comments
carlosdelest 8280753
Use feature flag to add new field type
carlosdelest 66fa2cc
Fix check when adding a new MapperPlugin
carlosdelest 048779b
Extracted a new interface for getInferenceModel()
carlosdelest 55c7ae1
Removing changes to LocalStateCompositeXPackPlugin
carlosdelest e448d3d
Removed unnecessary builder
carlosdelest fd235f5
Add semantic text feature flag to REST IT for completeness
carlosdelest b93cf5d
Revert "Removing changes to LocalStateCompositeXPackPlugin"
carlosdelest 43c5cdb
Merge branch 'main' into carlosdelest/semantic-text-field-mapper
carlosdelest c0c1125
Revert "Add semantic text feature flag to REST IT for completeness"
carlosdelest 4b06655
Moved FieldType to the bottom of the file
carlosdelest 39e061f
Removed redundant modelId
carlosdelest 07613ae
Fix merge from main
carlosdelest d7d8d5e
Fix merge from main
carlosdelest fd83fe9
Add back filtering for actual getFieldFilter() overriding
carlosdelest d8ae624
Spotless
carlosdelest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
server/src/main/java/org/elasticsearch/index/mapper/InferenceModelFieldType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
/** | ||
* Field type that uses an inference model. | ||
*/ | ||
public interface InferenceModelFieldType { | ||
/** | ||
* Retrieve inference model used by the field type. | ||
* | ||
* @return model id used by the field type | ||
*/ | ||
String getInferenceModel(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/SemanticTextFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml; | ||
|
||
import org.elasticsearch.common.util.FeatureFlag; | ||
|
||
/** | ||
* semantic_text feature flag. When the feature is complete, this flag will be removed. | ||
*/ | ||
public class SemanticTextFeature { | ||
|
||
private SemanticTextFeature() {} | ||
|
||
private static final FeatureFlag FEATURE_FLAG = new FeatureFlag("semantic_text"); | ||
|
||
public static boolean isEnabled() { | ||
return FEATURE_FLAG.isEnabled(); | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
...ck/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/mapper/SemanticTextFieldMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.mapper; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.index.fielddata.FieldDataContext; | ||
import org.elasticsearch.index.fielddata.IndexFieldData; | ||
import org.elasticsearch.index.mapper.DocumentParserContext; | ||
import org.elasticsearch.index.mapper.FieldMapper; | ||
import org.elasticsearch.index.mapper.InferenceModelFieldType; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.MapperBuilderContext; | ||
import org.elasticsearch.index.mapper.SimpleMappedFieldType; | ||
import org.elasticsearch.index.mapper.SourceValueFetcher; | ||
import org.elasticsearch.index.mapper.TextSearchInfo; | ||
import org.elasticsearch.index.mapper.ValueFetcher; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link FieldMapper} for semantic text fields. These fields have a model id reference, that is used for performing inference | ||
* at ingestion and query time. | ||
* For now, it is compatible with text expansion models only, but will be extended to support dense vector models as well. | ||
* This field mapper performs no indexing, as inference results will be included as a different field in the document source, and will | ||
* be indexed using a different field mapper. | ||
*/ | ||
public class SemanticTextFieldMapper extends FieldMapper { | ||
|
||
public static final String CONTENT_TYPE = "semantic_text"; | ||
|
||
private static SemanticTextFieldMapper toType(FieldMapper in) { | ||
return (SemanticTextFieldMapper) in; | ||
} | ||
|
||
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n), notInMultiFields(CONTENT_TYPE)); | ||
|
||
private SemanticTextFieldMapper(String simpleName, MappedFieldType mappedFieldType, CopyTo copyTo) { | ||
super(simpleName, mappedFieldType, MultiFields.empty(), copyTo); | ||
} | ||
|
||
@Override | ||
public FieldMapper.Builder getMergeBuilder() { | ||
return new Builder(simpleName()).init(this); | ||
} | ||
|
||
@Override | ||
protected void parseCreateField(DocumentParserContext context) throws IOException { | ||
// Just parses text - no indexing is performed | ||
context.parser().textOrNull(); | ||
} | ||
|
||
@Override | ||
protected String contentType() { | ||
return CONTENT_TYPE; | ||
} | ||
|
||
@Override | ||
public SemanticTextFieldType fieldType() { | ||
return (SemanticTextFieldType) super.fieldType(); | ||
} | ||
|
||
public static class Builder extends FieldMapper.Builder { | ||
|
||
private final Parameter<String> modelId = Parameter.stringParam("model_id", false, m -> toType(m).fieldType().modelId, null) | ||
.addValidator(v -> { | ||
if (Strings.isEmpty(v)) { | ||
throw new IllegalArgumentException("field [model_id] must be specified"); | ||
} | ||
}); | ||
|
||
private final Parameter<Map<String, String>> meta = Parameter.metaParam(); | ||
|
||
public Builder(String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
protected Parameter<?>[] getParameters() { | ||
return new Parameter<?>[] { modelId, meta }; | ||
} | ||
|
||
@Override | ||
public SemanticTextFieldMapper build(MapperBuilderContext context) { | ||
return new SemanticTextFieldMapper(name(), new SemanticTextFieldType(name(), modelId.getValue(), meta.getValue()), copyTo); | ||
} | ||
} | ||
|
||
public static class SemanticTextFieldType extends SimpleMappedFieldType implements InferenceModelFieldType { | ||
|
||
private final String modelId; | ||
|
||
public SemanticTextFieldType(String name, String modelId, Map<String, String> meta) { | ||
super(name, false, false, false, TextSearchInfo.NONE, meta); | ||
this.modelId = modelId; | ||
} | ||
|
||
@Override | ||
public String typeName() { | ||
return CONTENT_TYPE; | ||
} | ||
|
||
@Override | ||
public String getInferenceModel() { | ||
return modelId; | ||
} | ||
|
||
@Override | ||
public Query termQuery(Object value, SearchExecutionContext context) { | ||
throw new IllegalArgumentException("termQuery not implemented yet"); | ||
} | ||
|
||
@Override | ||
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { | ||
return SourceValueFetcher.toString(name(), context, format); | ||
} | ||
|
||
@Override | ||
public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) { | ||
throw new IllegalArgumentException("[semantic_text] fields do not support sorting, scripting or aggregating"); | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...ugin/ml/src/test/java/org/elasticsearch/xpack/ml/mapper/SemanticTextFieldMapperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.mapper; | ||
|
||
import org.apache.lucene.index.IndexableField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.mapper.DocumentMapper; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.MapperParsingException; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.index.mapper.MapperTestCase; | ||
import org.elasticsearch.index.mapper.ParsedDocument; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.ml.MachineLearning; | ||
import org.junit.AssumptionViolatedException; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class SemanticTextFieldMapperTests extends MapperTestCase { | ||
|
||
public void testDefaults() throws Exception { | ||
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); | ||
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString()); | ||
|
||
ParsedDocument doc1 = mapper.parse(source(this::writeField)); | ||
List<IndexableField> fields = doc1.rootDoc().getFields("field"); | ||
|
||
// No indexable fields | ||
assertTrue(fields.isEmpty()); | ||
} | ||
|
||
public void testModelIdNotPresent() throws IOException { | ||
Exception e = expectThrows( | ||
MapperParsingException.class, | ||
() -> createMapperService(fieldMapping(b -> b.field("type", "semantic_text"))) | ||
); | ||
assertThat(e.getMessage(), containsString("field [model_id] must be specified")); | ||
} | ||
|
||
public void testCannotBeUsedInMultiFields() { | ||
Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> { | ||
b.field("type", "text"); | ||
b.startObject("fields"); | ||
b.startObject("semantic"); | ||
b.field("type", "semantic_text"); | ||
b.endObject(); | ||
b.endObject(); | ||
}))); | ||
assertThat(e.getMessage(), containsString("Field [semantic] of type [semantic_text] can't be used in multifields")); | ||
} | ||
|
||
public void testUpdatesToModelIdNotSupported() throws IOException { | ||
MapperService mapperService = createMapperService( | ||
fieldMapping(b -> b.field("type", "semantic_text").field("model_id", "test_model")) | ||
); | ||
Exception e = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> merge(mapperService, fieldMapping(b -> b.field("type", "semantic_text").field("model_id", "another_model"))) | ||
); | ||
assertThat(e.getMessage(), containsString("Cannot update parameter [model_id] from [test_model] to [another_model]")); | ||
} | ||
|
||
@Override | ||
protected Collection<? extends Plugin> getPlugins() { | ||
return singletonList(new MachineLearning(Settings.EMPTY)); | ||
} | ||
|
||
@Override | ||
protected void minimalMapping(XContentBuilder b) throws IOException { | ||
b.field("type", "semantic_text").field("model_id", "test_model"); | ||
} | ||
|
||
@Override | ||
protected Object getSampleValueForDocument() { | ||
return "value"; | ||
} | ||
|
||
@Override | ||
protected boolean supportsIgnoreMalformed() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean supportsStoredFields() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void registerParameters(ParameterChecker checker) throws IOException {} | ||
|
||
@Override | ||
protected Object generateRandomInputValue(MappedFieldType ft) { | ||
assumeFalse("doc_values are not supported in semantic_text", true); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed) { | ||
throw new AssumptionViolatedException("not supported"); | ||
} | ||
|
||
@Override | ||
protected IngestScriptSupport ingestScriptSupport() { | ||
throw new AssumptionViolatedException("not supported"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move this nested class to the bottom of the file please? I know we have it like this in other mappers but it makes the code very hard to follow as can be seen by the accidental introduction of duplicate state across the field and mapper here IMO :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense - done in 4b06655