Skip to content

Adding support to exclude semantic_text subfields #127664

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/127664.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 127664
summary: Exclude `semantic_text` subfields from field capabilities API
area: "Mapping"
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ static Map<String, IndexFieldCapabilities> retrieveFieldCaps(
MappedFieldType ft = entry.getValue();
if ((includeEmptyFields || ft.fieldHasValue(fieldInfos))
&& (fieldPredicate.test(ft.name()) || context.isMetadataField(ft.name()))
&& (filter == null || filter.test(ft))) {
&& (filter == null || filter.test(ft))
&& ft.excludeFromFieldCaps() == false) {
IndexFieldCapabilities fieldCap = new IndexFieldCapabilities(
field,
ft.familyTypeName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public static final class Builder extends FieldMapper.DimensionBuilder {
private final IndexVersion indexCreatedVersion;
private final boolean useDocValuesSkipper;
private final SourceKeepMode indexSourceKeepMode;
private boolean excludeFromFieldCaps = false;

public Builder(final String name, final MappingParserContext mappingParserContext) {
this(
Expand Down Expand Up @@ -300,6 +301,11 @@ public Builder ignoreAbove(int ignoreAbove) {
return this;
}

public Builder excludeFromFieldCaps(boolean value) {
this.excludeFromFieldCaps = value;
return this;
}

Builder normalizer(String normalizerName) {
this.normalizer.setValue(normalizerName);
return this;
Expand Down Expand Up @@ -409,7 +415,8 @@ private KeywordFieldType buildFieldType(MapperBuilderContext context, FieldType
searchAnalyzer,
quoteAnalyzer,
this,
context.isSourceSynthetic()
context.isSourceSynthetic(),
excludeFromFieldCaps
);
}

Expand Down Expand Up @@ -516,14 +523,28 @@ public KeywordFieldType(
NamedAnalyzer quoteAnalyzer,
Builder builder,
boolean isSyntheticSource
) {
this(name, fieldType, normalizer, searchAnalyzer, quoteAnalyzer, builder, isSyntheticSource, false);
}

public KeywordFieldType(
String name,
FieldType fieldType,
NamedAnalyzer normalizer,
NamedAnalyzer searchAnalyzer,
NamedAnalyzer quoteAnalyzer,
Builder builder,
boolean isSyntheticSource,
boolean excludeFromFieldCaps
) {
super(
name,
fieldType.indexOptions() != IndexOptions.NONE && builder.indexCreatedVersion.isLegacyIndexVersion() == false,
fieldType.stored(),
builder.hasDocValues.getValue(),
textSearchInfo(fieldType, builder.similarity.getValue(), searchAnalyzer, quoteAnalyzer),
builder.meta.getValue()
builder.meta.getValue(),
excludeFromFieldCaps
);
this.eagerGlobalOrdinals = builder.eagerGlobalOrdinals.getValue();
this.normalizer = normalizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public abstract class MappedFieldType {
private final boolean isStored;
private final TextSearchInfo textSearchInfo;
private final Map<String, String> meta;
private final boolean excludeFromFieldCaps;

public MappedFieldType(
String name,
Expand All @@ -76,6 +77,18 @@ public MappedFieldType(
boolean hasDocValues,
TextSearchInfo textSearchInfo,
Map<String, String> meta
) {
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, false);
}

public MappedFieldType(
String name,
boolean isIndexed,
boolean isStored,
boolean hasDocValues,
TextSearchInfo textSearchInfo,
Map<String, String> meta,
boolean excludeFromFieldCaps
) {
this.name = Mapper.internFieldName(name);
this.isIndexed = isIndexed;
Expand All @@ -85,6 +98,7 @@ public MappedFieldType(
// meta should be sorted but for the one item or empty case we can fall back to immutable maps to save some memory since order is
// irrelevant
this.meta = meta.size() <= 1 ? Map.copyOf(meta) : meta;
this.excludeFromFieldCaps = excludeFromFieldCaps;
}

/**
Expand Down Expand Up @@ -170,6 +184,13 @@ public final boolean isStored() {
return isStored;
}

/**
* @return true if the field should be excluded from field caps
*/
public boolean excludeFromFieldCaps() {
return excludeFromFieldCaps;
}

/**
* If the field supports using the indexed data to speed up operations related to ordering of data, such as sorting or aggs, return
* a function for doing that. If it is unsupported for this field type, there is no need to override this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ protected SimpleMappedFieldType(
TextSearchInfo textSearchInfo,
Map<String, String> meta
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, false);
}

protected SimpleMappedFieldType(
String name,
boolean isIndexed,
boolean isStored,
boolean hasDocValues,
TextSearchInfo textSearchInfo,
Map<String, String> meta,
boolean excludeFromFieldCaps
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, excludeFromFieldCaps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ public StringFieldType(
TextSearchInfo textSearchInfo,
Map<String, String> meta
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, false);
}

public StringFieldType(
String name,
boolean isIndexed,
boolean isStored,
boolean hasDocValues,
TextSearchInfo textSearchInfo,
Map<String, String> meta,
boolean excludeFromFieldCaps
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, excludeFromFieldCaps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public TermBasedFieldType(
TextSearchInfo textSearchInfo,
Map<String, String> meta
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, false);
}

public TermBasedFieldType(
String name,
boolean isIndexed,
boolean isStored,
boolean hasDocValues,
TextSearchInfo textSearchInfo,
Map<String, String> meta,
boolean excludeFromFieldCaps
) {
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, excludeFromFieldCaps);
}

/** Returns the indexed value used to construct search "values".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public static class Builder extends FieldMapper.Builder {
private final Parameter<Map<String, String>> meta = Parameter.metaParam();

final IndexVersion indexVersionCreated;
private boolean excludeFromFieldCaps = false;

public Builder(String name, IndexVersion indexVersionCreated) {
super(name);
Expand Down Expand Up @@ -361,23 +362,30 @@ public Builder indexOptions(IndexOptions indexOptions) {
return this;
}

public Builder excludeFromFieldCaps(boolean value) {
this.excludeFromFieldCaps = value;
return this;
}

@Override
public DenseVectorFieldMapper build(MapperBuilderContext context) {
DenseVectorFieldType denseVectorFieldType = new DenseVectorFieldType(
context.buildFullName(leafName()),
indexVersionCreated,
elementType.getValue(),
dims.getValue(),
indexed.getValue(),
similarity.getValue(),
indexOptions.getValue(),
meta.getValue(),
excludeFromFieldCaps
);
// Validate again here because the dimensions or element type could have been set programmatically,
// which affects index option validity
validate();
return new DenseVectorFieldMapper(
leafName(),
new DenseVectorFieldType(
context.buildFullName(leafName()),
indexVersionCreated,
elementType.getValue(),
dims.getValue(),
indexed.getValue(),
similarity.getValue(),
indexOptions.getValue(),
meta.getValue()
),
denseVectorFieldType,
builderParams(this, context),
indexOptions.getValue(),
indexVersionCreated
Expand Down Expand Up @@ -2149,7 +2157,21 @@ public DenseVectorFieldType(
IndexOptions indexOptions,
Map<String, String> meta
) {
super(name, indexed, false, indexed == false, TextSearchInfo.NONE, meta);
this(name, indexVersionCreated, elementType, dims, indexed, similarity, indexOptions, meta, false);
}

public DenseVectorFieldType(
String name,
IndexVersion indexVersionCreated,
ElementType elementType,
Integer dims,
boolean indexed,
VectorSimilarity similarity,
IndexOptions indexOptions,
Map<String, String> meta,
boolean excludeFromFieldCaps
) {
super(name, indexed, false, indexed == false, TextSearchInfo.NONE, meta, excludeFromFieldCaps);
this.elementType = elementType;
this.dims = dims;
this.indexed = indexed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private static SparseVectorFieldMapper toType(FieldMapper in) {
public static class Builder extends FieldMapper.Builder {
private final Parameter<Boolean> stored = Parameter.storeParam(m -> toType(m).fieldType().isStored(), false);
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
private boolean excludeFromFieldCaps = false;

public Builder(String name) {
super(name);
Expand All @@ -83,18 +84,25 @@ public Builder setStored(boolean value) {
return this;
}

public Builder excludeFromFieldCaps(boolean value) {
excludeFromFieldCaps = value;
return this;
}

@Override
protected Parameter<?>[] getParameters() {
return new Parameter<?>[] { stored, meta };
}

@Override
public SparseVectorFieldMapper build(MapperBuilderContext context) {
return new SparseVectorFieldMapper(
leafName(),
new SparseVectorFieldType(context.buildFullName(leafName()), stored.getValue(), meta.getValue()),
builderParams(this, context)
SparseVectorFieldType sparseVectorFieldType = new SparseVectorFieldType(
context.buildFullName(leafName()),
stored.getValue(),
meta.getValue(),
excludeFromFieldCaps
);
return new SparseVectorFieldMapper(leafName(), sparseVectorFieldType, builderParams(this, context));
}
}

Expand All @@ -111,7 +119,11 @@ public SparseVectorFieldMapper build(MapperBuilderContext context) {
public static final class SparseVectorFieldType extends MappedFieldType {

public SparseVectorFieldType(String name, boolean isStored, Map<String, String> meta) {
super(name, true, isStored, false, TextSearchInfo.SIMPLE_MATCH_ONLY, meta);
this(name, isStored, meta, false);
}

public SparseVectorFieldType(String name, boolean isStored, Map<String, String> meta, boolean excludeFromFieldCaps) {
super(name, true, isStored, false, TextSearchInfo.SIMPLE_MATCH_ONLY, meta, excludeFromFieldCaps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.Set;

import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.SEMANTIC_TEXT_EXCLUDE_SUB_FIELDS_FROM_FIELD_CAPS;
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.SEMANTIC_TEXT_SUPPORT_CHUNKING_CONFIG;
import static org.elasticsearch.xpack.inference.queries.SemanticKnnVectorQueryRewriteInterceptor.SEMANTIC_KNN_FILTER_FIX;
import static org.elasticsearch.xpack.inference.queries.SemanticKnnVectorQueryRewriteInterceptor.SEMANTIC_KNN_VECTOR_QUERY_REWRITE_INTERCEPTION_SUPPORTED;
Expand Down Expand Up @@ -57,7 +58,8 @@ public Set<NodeFeature> getTestFeatures() {
SemanticTextFieldMapper.SEMANTIC_TEXT_BIT_VECTOR_SUPPORT,
SemanticTextFieldMapper.SEMANTIC_TEXT_HANDLE_EMPTY_INPUT,
TEST_RULE_RETRIEVER_WITH_INDICES_THAT_DONT_RETURN_RANK_DOCS,
SEMANTIC_TEXT_SUPPORT_CHUNKING_CONFIG
SEMANTIC_TEXT_SUPPORT_CHUNKING_CONFIG,
SEMANTIC_TEXT_EXCLUDE_SUB_FIELDS_FROM_FIELD_CAPS
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,32 @@ public static class Builder extends FieldMapper.Builder {
CharsetFormat.class
);
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
private boolean excludeFromFieldCaps = false;

public Builder(String name) {
super(name);
}

public Builder excludeFromFieldCaps(boolean value) {
excludeFromFieldCaps = value;
return this;
}

@Override
protected Parameter<?>[] getParameters() {
return new Parameter<?>[] { meta, charset };
}

@Override
public OffsetSourceFieldMapper build(MapperBuilderContext context) {
return new OffsetSourceFieldMapper(
leafName(),
new OffsetSourceFieldType(context.buildFullName(leafName()), charset.get(), meta.getValue()),
builderParams(this, context)
OffsetSourceFieldType fieldType = new OffsetSourceFieldType(
context.buildFullName(leafName()),
charset.get(),
meta.getValue(),
this.excludeFromFieldCaps
);

return new OffsetSourceFieldMapper(leafName(), fieldType, builderParams(this, context));
}
}

Expand All @@ -133,7 +142,11 @@ public static final class OffsetSourceFieldType extends MappedFieldType {
private final CharsetFormat charset;

public OffsetSourceFieldType(String name, CharsetFormat charset, Map<String, String> meta) {
super(name, true, false, false, TextSearchInfo.NONE, meta);
this(name, charset, meta, false);
}

public OffsetSourceFieldType(String name, CharsetFormat charset, Map<String, String> meta, boolean excludeFromFieldCaps) {
super(name, true, false, false, TextSearchInfo.NONE, meta, excludeFromFieldCaps);
this.charset = charset;
}

Expand Down
Loading
Loading