Skip to content

Commit

Permalink
Use a system property to enable inference metadata fields (elastic#11…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikep86 authored Dec 18, 2024
1 parent f52a5a7 commit 6c9edd0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

package org.elasticsearch.index.mapper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.BitSetProducer;
import org.elasticsearch.common.util.FeatureFlag;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand All @@ -26,7 +28,37 @@
* the field name for removal from _source.
*/
public abstract class InferenceMetadataFieldsMapper extends MetadataFieldMapper {
public static final FeatureFlag INFERENCE_METADATA_FIELDS_FEATURE_FLAG = new FeatureFlag("inference_metadata_fields");
public static class SystemProperty {
private static final Logger logger = LogManager.getLogger(SystemProperty.class);

private final boolean enabled;

private SystemProperty(String name) {
String propertyName = "es." + name;
this.enabled = parseSystemProperty(propertyName, false);
if (this.enabled) {
logger.info("Feature " + name + " (via system property '" + propertyName + "') is enabled");
} else {
logger.debug("Feature " + name + " (via system property '" + propertyName + "') is disabled");
}
}

private boolean parseSystemProperty(String propertyName, boolean defaultValue) {
final String propertyValue = System.getProperty(propertyName);
logger.trace("System property [{}] is set to [{}]", propertyName, propertyValue);
try {
return Booleans.parseBoolean(propertyValue, defaultValue);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid value [" + propertyValue + "] for system property [" + propertyName + "]", e);
}
}

public boolean isEnabled() {
return enabled;
}
}

public static final SystemProperty INFERENCE_METADATA_FIELDS_SYSTEM_PROPERTY = new SystemProperty("inference_metadata_fields");

public static final String NAME = "_inference_fields";
public static final String CONTENT_TYPE = "_inference_fields";
Expand Down Expand Up @@ -61,6 +93,6 @@ public abstract ValueFetcher valueFetcher(
}

public static boolean isEnabled(IndexVersion indexVersion) {
return indexVersion.onOrAfter(IndexVersions.INFERENCE_METADATA_FIELDS) && INFERENCE_METADATA_FIELDS_FEATURE_FLAG.isEnabled();
return indexVersion.onOrAfter(IndexVersions.INFERENCE_METADATA_FIELDS) && INFERENCE_METADATA_FIELDS_SYSTEM_PROPERTY.isEnabled();
}
}
10 changes: 10 additions & 0 deletions x-pack/plugin/inference/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,17 @@ tasks.named("thirdPartyAudit").configure {
)
}

tasks.named('test') {
if (buildParams.isSnapshotBuild()) {
systemProperty('es.inference_metadata_fields', 'true')
systemProperty('tests.jvm.argline', '-Des.inference_metadata_fields=true')
}
}

tasks.named('yamlRestTest') {
usesDefaultDistribution()
if (buildParams.isSnapshotBuild()) {
systemProperty('tests.jvm.argline', '-Des.inference_metadata_fields=true')
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.List;
import java.util.Set;

import static org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper.INFERENCE_METADATA_FIELDS_FEATURE_FLAG;
import static org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper.INFERENCE_METADATA_FIELDS_SYSTEM_PROPERTY;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.xpack.inference.rest.Paths.INFERENCE_ID;
import static org.elasticsearch.xpack.inference.rest.Paths.INFERENCE_ID_PATH;
Expand Down Expand Up @@ -72,7 +72,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
public Set<String> supportedCapabilities() {
Set<String> capabilities = new HashSet<>();
capabilities.add(DEFAULT_ELSER_2_CAPABILITY);
if (INFERENCE_METADATA_FIELDS_FEATURE_FLAG.isEnabled()) {
if (INFERENCE_METADATA_FIELDS_SYSTEM_PROPERTY.isEnabled()) {
capabilities.add(INFERENCE_METADATA_FIELDS_CAPABILITY);
}

Expand Down

0 comments on commit 6c9edd0

Please sign in to comment.