diff options
Diffstat (limited to 'config-model/src')
5 files changed, 83 insertions, 5 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolver.java b/config-model/src/main/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolver.java index 8bb834d4697..d86ed265b77 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolver.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolver.java @@ -2,8 +2,13 @@ package com.yahoo.searchdefinition.processing; import com.yahoo.config.application.api.DeployLogger; +import com.yahoo.document.ArrayDataType; +import com.yahoo.document.DataType; +import com.yahoo.document.WeightedSetDataType; import com.yahoo.searchdefinition.RankProfileRegistry; import com.yahoo.searchdefinition.Search; +import com.yahoo.searchdefinition.document.ComplexAttributeFieldUtils; +import com.yahoo.searchdefinition.document.ImmutableSDField; import com.yahoo.vespa.documentmodel.DocumentSummary; import com.yahoo.vespa.documentmodel.SummaryField; import com.yahoo.vespa.documentmodel.SummaryTransform; @@ -43,15 +48,40 @@ public class MatchedElementsOnlyResolver extends Processor { if (isComplexFieldWithOnlyStructFieldAttributes(sourceField)) { field.setTransform(SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER); } + } else if (isSupportedAttributeField(sourceField)) { + field.setTransform(SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER); } else if (validate) { fail(summary, field, "'matched-elements-only' is not supported for this field type. " + - "Supported field types are array of simple struct, map of primitive type to simple struct, " + + "Supported field types are: array attribute, weighted set attribute, " + + "array of simple struct, map of primitive type to simple struct, " + "and map of primitive type to primitive type"); } } // else case is handled in SummaryFieldsMustHaveValidSource } + private boolean isSupportedAttributeField(ImmutableSDField sourceField) { + var type = sourceField.getDataType(); + return sourceField.doesAttributing() && + (isArrayOfPrimitiveType(type) || isWeightedsetOfPrimitiveType(type)); + } + + private boolean isArrayOfPrimitiveType(DataType type) { + if (type instanceof ArrayDataType) { + var arrayType = (ArrayDataType) type; + return ComplexAttributeFieldUtils.isPrimitiveType(arrayType.getNestedType()); + } + return false; + } + + private boolean isWeightedsetOfPrimitiveType(DataType type) { + if (type instanceof WeightedSetDataType) { + var wsetType = (WeightedSetDataType) type; + return ComplexAttributeFieldUtils.isPrimitiveType(wsetType.getNestedType()); + } + return false; + } + private void fail(DocumentSummary summary, SummaryField field, String msg) { throw new IllegalArgumentException(formatError(search, summary, field, msg)); } diff --git a/config-model/src/test/derived/importedfields/child.sd b/config-model/src/test/derived/importedfields/child.sd index d541ba8fc8c..95fe44d333a 100644 --- a/config-model/src/test/derived/importedfields/child.sd +++ b/config-model/src/test/derived/importedfields/child.sd @@ -23,5 +23,9 @@ search child { summary my_int_array_field type array<int> {} summary my_int_wset_field type weightedset<int> {} summary my_ancient_int_field type int {} + summary my_filtered_int_array_field type array<int> { + source: my_int_array_field + matched-elements-only + } } } diff --git a/config-model/src/test/derived/importedfields/summary.cfg b/config-model/src/test/derived/importedfields/summary.cfg index 68ad270a314..f95949cfa62 100644 --- a/config-model/src/test/derived/importedfields/summary.cfg +++ b/config-model/src/test/derived/importedfields/summary.cfg @@ -9,7 +9,7 @@ classes[].fields[].name "summaryfeatures" classes[].fields[].type "featuredata" classes[].fields[].name "documentid" classes[].fields[].type "longstring" -classes[].id 1660388492 +classes[].id 159551552 classes[].name "mysummary" classes[].fields[].name "a_ref" classes[].fields[].type "longstring" @@ -25,6 +25,8 @@ classes[].fields[].name "my_int_wset_field" classes[].fields[].type "jsonstring" classes[].fields[].name "my_ancient_int_field" classes[].fields[].type "integer" +classes[].fields[].name "my_filtered_int_array_field" +classes[].fields[].type "jsonstring" classes[].fields[].name "rankfeatures" classes[].fields[].type "featuredata" classes[].fields[].name "summaryfeatures" diff --git a/config-model/src/test/derived/importedfields/summarymap.cfg b/config-model/src/test/derived/importedfields/summarymap.cfg index d038e2313a8..03ae5e2676e 100644 --- a/config-model/src/test/derived/importedfields/summarymap.cfg +++ b/config-model/src/test/derived/importedfields/summarymap.cfg @@ -14,6 +14,9 @@ override[].arguments "my_int_wset_field" override[].field "my_ancient_int_field" override[].command "attribute" override[].arguments "my_ancient_int_field" +override[].field "my_filtered_int_array_field" +override[].command "matchedattributeelementsfilter" +override[].arguments "my_int_array_field" override[].field "rankfeatures" override[].command "rankfeatures" override[].arguments "" diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolverTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolverTestCase.java index 3b6918c04ae..0ef696df6cf 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolverTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/MatchedElementsOnlyResolverTestCase.java @@ -75,7 +75,7 @@ public class MatchedElementsOnlyResolverTestCase { } @Test - public void explicit_summary_field_can_use_filter_transform_with_reference_to_source_field() throws ParseException { + public void explicit_complex_summary_field_can_use_filter_transform_with_reference_to_source_field() throws ParseException { String documentSummary = joinLines("document-summary my_summary {", " summary my_filter_field type map<string, string> {", " source: my_field", @@ -108,12 +108,51 @@ public class MatchedElementsOnlyResolverTestCase { } @Test + public void primitive_array_attribute_field_gets_attribute_transform() throws ParseException { + assertSummaryField(joinLines("field my_field type array<string> {", + " indexing: attribute | summary", + " summary: matched-elements-only", + "}"), + "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER); + } + + @Test + public void primitive_weighted_set_attribute_field_gets_attribute_transform() throws ParseException { + assertSummaryField(joinLines("field my_field type weightedset<string> {", + " indexing: attribute | summary", + " summary: matched-elements-only", + "}"), + "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER); + } + + @Test + public void explicit_summary_field_can_use_filter_transform_with_reference_to_attribute_source_field() throws ParseException { + String documentSummary = joinLines("document-summary my_summary {", + " summary my_filter_field type array<string> {", + " source: my_field", + " matched-elements-only", + " }", + "}"); + + var search = buildSearch(joinLines("field my_field type array<string> {", + " indexing: attribute | summary", + "}"), + documentSummary); + assertSummaryField(search.getSummaryField("my_filter_field"), + SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER, "my_field"); + assertSummaryField(search.getSummaryField("my_field"), + SummaryTransform.ATTRIBUTE, "my_field"); + } + + @Test public void unsupported_field_type_throws() throws ParseException { exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage("For search 'test', document summary 'default', summary field 'my_field': " + "'matched-elements-only' is not supported for this field type. " + - "Supported field types are array of simple struct, map of primitive type to simple struct, and map of primitive type to primitive type"); - buildSearch(joinLines("field my_field type string {", + "Supported field types are: array attribute, weighted set attribute, " + + "array of simple struct, map of primitive type to simple struct, " + + "and map of primitive type to primitive type"); + buildSearch(joinLines("field my_field type array<string> {", " indexing: summary", " summary: matched-elements-only", "}")); |