Skip to content

Commit

Permalink
iter
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Nov 30, 2024
1 parent 78ff84f commit 1d1e819
Show file tree
Hide file tree
Showing 40 changed files with 2,026 additions and 3,054 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private static UpdateHelper.Result deleteInferenceResults(
// This has two important side effects:
// - The inference field value will remain parsable by its mapper
// - The inference results will be removed, forcing them to be re-generated downstream
updatedSource.put(inferenceFieldName, inferenceFieldMapper.getOriginalValue(updatedSource));
updatedSource.put(inferenceFieldName, getOriginalValueLegacy(inferenceFieldName, updatedSource));
updatedSourceModified = true;
break;
}
Expand All @@ -435,4 +435,23 @@ private static UpdateHelper.Result deleteInferenceResults(

return returnedResult;
}

/**
* Get the field's original value (i.e. the value the user specified) from the provided source.
*
* @param sourceAsMap The source as a map
* @return The field's original value, or {@code null} if none was provided
*/
private static Object getOriginalValueLegacy(String fullPath, Map<String, Object> sourceAsMap) {
Object fieldValue = sourceAsMap.get(fullPath);
if (fieldValue == null) {
return null;
} else if (fieldValue instanceof Map<?, ?> == false) {
// Don't try to further validate the non-map value, that will be handled when the source is fully parsed
return fieldValue;
}

Map<String, Object> fieldValueMap = XContentMapValues.nodeMapValue(fieldValue, "Field [" + fullPath + "]");
return XContentMapValues.extractValue("text", fieldValueMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public boolean isWithinInferenceMetadata() {
}

@Override
public void markInferenceMetadata() {
in.markInferenceMetadata();
public void markInferenceMetadataField() {
in.markInferenceMetadataField();
}

@Override
Expand Down Expand Up @@ -155,7 +155,7 @@ private enum Scope {
// Indicates if the source for this context has been marked to be recorded. Applies to synthetic source only.
private boolean recordedSource;

private boolean inferenceMetadata;
private boolean hasInferenceMetadata;

private DocumentParserContext(
MappingLookup mappingLookup,
Expand Down Expand Up @@ -349,12 +349,19 @@ public final DocumentParserContext addIgnoredFieldFromContext(IgnoredSourceField
return this;
}

public void markInferenceMetadata() {
this.inferenceMetadata = true;
/**
* Called by {@link InferenceMetadataFieldsMapper} to indicate whether the metadata field is present
* in _source.
*/
public void markInferenceMetadataField() {
this.hasInferenceMetadata = true;
}

public final boolean hasInferenceMetadata() {
return false;// TODO: inferenceMetadata;
/**
* Returns whether the _source contains an inference metadata field.
*/
public final boolean hasInferenceMetadataField() {
return hasInferenceMetadata;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.cluster.metadata.InferenceFieldMetadata;
import org.elasticsearch.inference.InferenceService;

import java.util.Map;
import java.util.Set;

/**
Expand All @@ -26,12 +25,4 @@ public interface InferenceFieldMapper {
* @param sourcePaths The source path that populates the input for the field (before inference)
*/
InferenceFieldMetadata getMetadata(Set<String> sourcePaths);

/**
* Get the field's original value (i.e. the value the user specified) from the provided source.
*
* @param sourceAsMap The source as a map
* @return The field's original value, or {@code null} if none was provided
*/
Object getOriginalValue(Map<String, Object> sourceAsMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ protected boolean supportsParsingObject() {
protected void parseCreateField(DocumentParserContext context) throws IOException {
XContentParser parser = context.parser();
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
context.markInferenceMetadata();
context.markInferenceMetadataField();
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser);
String fieldName = parser.currentName();
// TODO: Find the leaf field under objects
Mapper mapper = context.mappingLookup().getMapper(fieldName);
if (mapper != null && mapper instanceof InferenceFieldMapper && mapper instanceof FieldMapper fieldMapper) {
fieldMapper.parseCreateField(new DocumentParserContext.Wrapper(context.parent(), context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public BytesReference applyFilters(
return null;
}
var modSourceFilter = sourceFilter;
if (context != null && context.hasInferenceMetadata()) {
if (context != null && context.hasInferenceMetadataField()) {
String[] modExcludes = new String[excludes != null ? excludes.length + 1 : 1];
if (excludes != null) {
System.arraycopy(excludes, 0, modExcludes, 0, excludes.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,10 @@ public void testSupportsParsingObject() throws IOException {
Object sampleValueForDocument = getSampleObjectForDocument();
assertThat(sampleValueForDocument, instanceOf(Map.class));
SourceToParse source = source(builder -> {
builder.startObject(InferenceMetadataFieldsMapper.NAME);
builder.field("field");
builder.value(sampleValueForDocument);
builder.endObject();
});
ParsedDocument doc = mapper.parse(source);
assertNotNull(doc);
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
testImplementation project(path: ':modules:analysis-common')
testImplementation project(path: ':modules:rest-root')
testImplementation project(path: ':modules:health-shards-availability')
testImplementation project(path: ':modules:mapper-extras')
// Needed for Fips140ProviderVerificationTests
testCompileOnly('org.bouncycastle:bc-fips:1.0.2.5')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -33,7 +33,6 @@
import org.elasticsearch.xpack.core.ml.inference.results.TextExpansionResults;
import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults;
import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TextExpansionConfigUpdate;
import org.elasticsearch.xpack.core.ml.search.WeightedToken;

import java.io.IOException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.apache.lucene.search.Query;
import org.apache.lucene.util.SetOnce;
Expand Down Expand Up @@ -39,7 +39,7 @@

import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN;
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
import static org.elasticsearch.xpack.inference.queries.WeightedTokensQueryBuilder.PRUNING_CONFIG;
import static org.elasticsearch.xpack.core.ml.search.WeightedTokensQueryBuilder.PRUNING_CONFIG;

/**
* @deprecated Replaced by sparse_vector query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -22,7 +22,7 @@
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.xpack.inference.queries.WeightedTokensQueryBuilder.PRUNING_CONFIG;
import static org.elasticsearch.xpack.core.ml.search.WeightedTokensQueryBuilder.PRUNING_CONFIG;

public class TokenPruningConfig implements Writeable, ToXContentObject {
public static final ParseField TOKENS_FREQ_RATIO_THRESHOLD = new ParseField("tokens_freq_ratio_threshold");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
Expand All @@ -24,7 +24,6 @@
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ml.search.WeightedToken;

import java.io.IOException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.inference.queries;
package org.elasticsearch.xpack.core.ml.search;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
Expand All @@ -16,7 +16,6 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xpack.core.ml.search.WeightedToken;

import java.io.IOException;
import java.util.List;
Expand Down
Loading

0 comments on commit 1d1e819

Please sign in to comment.