forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an inference metadata fields instead of storing the inference in …
…the original field
- Loading branch information
Showing
10 changed files
with
169 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
85 changes: 85 additions & 0 deletions
85
server/src/main/java/org/elasticsearch/index/mapper/InferenceMetadataFieldsMapper.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,85 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
import org.elasticsearch.index.query.QueryShardException; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class InferenceMetadataFieldsMapper extends MetadataFieldMapper { | ||
public static final String NAME = "_inference_fields"; | ||
public static final String CONTENT_TYPE = "_inference_fields"; | ||
|
||
private static final InferenceMetadataFieldsMapper INSTANCE = new InferenceMetadataFieldsMapper(); | ||
|
||
public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE); | ||
|
||
public static final class InferenceFieldType extends MappedFieldType { | ||
private static InferenceFieldType INSTANCE = new InferenceFieldType(); | ||
|
||
public InferenceFieldType() { | ||
super(NAME, false, false, false, null, Map.of()); | ||
} | ||
|
||
@Override | ||
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { | ||
// TODO: return the map from the individual semantic text fields? | ||
return null; | ||
} | ||
|
||
@Override | ||
public String typeName() { | ||
return CONTENT_TYPE; | ||
} | ||
|
||
@Override | ||
public Query termQuery(Object value, SearchExecutionContext context) { | ||
throw new QueryShardException( | ||
context, | ||
"[" + name() + "] field which is of type [" + typeName() + "], does not support term queries" | ||
); | ||
} | ||
} | ||
|
||
private InferenceMetadataFieldsMapper() { | ||
super(InferenceFieldType.INSTANCE); | ||
} | ||
|
||
@Override | ||
protected String contentType() { | ||
return CONTENT_TYPE; | ||
} | ||
|
||
@Override | ||
protected void parseCreateField(DocumentParserContext context) throws IOException { | ||
XContentParser parser = context.parser(); | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser); | ||
String fieldName = parser.currentName(); | ||
Mapper mapper = context.mappingLookup().getMapper(fieldName); | ||
if (mapper != null && mapper instanceof InferenceFieldMapper && mapper instanceof FieldMapper fieldMapper) { | ||
fieldMapper.parseCreateField(new DocumentParserContext.Wrapper(context.parent(), context) { | ||
@Override | ||
public boolean isWithinInferenceMetadata() { | ||
return true; | ||
} | ||
}); | ||
} else { | ||
throw new IllegalArgumentException("Illegal inference field [" + fieldName + "] found."); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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