diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-10-19 13:37:47 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-10-19 13:37:47 +0200 |
commit | fb81412a6718ce2419ceee89a09c6705a4578487 (patch) | |
tree | 008b83708fef6c26b53a6512b84945ef62a5ae83 | |
parent | 5915b0d1470e7b6ae7e30ad4e532835843b75f63 (diff) |
Inherit document summaries
4 files changed, 47 insertions, 21 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java b/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java index f2cc7febb15..f5a2cd71c0f 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java @@ -531,7 +531,10 @@ public class Schema implements ImmutableSchema { * @return Summary found. */ public DocumentSummary getSummary(String name) { - return summaries.get(name); + var summary = summaries.get(name); + if (summary != null) return summary; + if (inherited.isEmpty()) return null; + return requireInherited().getSummary(name); } /** @@ -548,47 +551,56 @@ public class Schema implements ImmutableSchema { return summaryField; } } - return null; + if (inherited.isEmpty()) return null; + return requireInherited().getSummaryField(name); } /** * Returns the first explicit instance found of a summary field with this name, or null if not present explicitly in * any summary class * - * @param name Thge name of the explicit summary field to get. - * @return The SummaryField found. + * @param name the name of the explicit summary field to get. + * @return the SummaryField found. */ public SummaryField getExplicitSummaryField(String name) { for (DocumentSummary summary : summaries.values()) { SummaryField summaryField = summary.getSummaryField(name); - if (summaryField != null && !summaryField.isImplicit()) { + if (summaryField != null && !summaryField.isImplicit()) return summaryField; - } } - return null; + if (inherited.isEmpty()) return null; + return requireInherited().getExplicitSummaryField(name); } /** * Summaries defined by fields of this search definition. The default summary, named "default", is always the first * one in the returned iterator. - * - * @return The map of document summaries. */ public Map<String, DocumentSummary> getSummaries() { - return summaries; + // Shortcuts + if (inherited.isEmpty()) return summaries; + if (summaries.isEmpty()) return requireInherited().getSummaries(); + + var allSummaries = new LinkedHashMap<>(requireInherited().getSummaries()); + allSummaries.putAll(summaries); + return allSummaries; } + /** Returns the summaries defines in this only, not any that are inherited. */ + public Map<String, DocumentSummary> getSummariesInThis() { return Collections.unmodifiableMap(summaries); } + /** - * <p>Returns all summary fields, of all document summaries, which has the given field as source. If there are + * Returns all summary fields, of all document summaries, which has the given field as source. If there are * multiple summary fields with the same name, the last one will be used (they should all have the same content, if - * this is a valid search definition).</p> <p>The map gets owned by the receiver.</p> + * this is a valid search definition).The map becomes owned by the receiver. * - * @param field The source field. - * @return The map of summary fields found. + * @param field the source field + * @return the map of summary fields found */ @Override public Map<String, SummaryField> getSummaryFields(ImmutableSDField field) { - Map<String, SummaryField> summaryFields = new java.util.LinkedHashMap<>(); + Map<String, SummaryField> summaryFields = inherited.isPresent() ? requireInherited().getSummaryFields(field) + : new java.util.LinkedHashMap<>(); for (DocumentSummary documentSummary : summaries.values()) { for (SummaryField summaryField : documentSummary.getSummaryFields()) { if (summaryField.hasSource(field.getName())) { @@ -600,15 +612,14 @@ public class Schema implements ImmutableSchema { } /** - * <p>Returns one summary field for each summary field name. If there are multiple summary fields with the same + * Returns one summary field for each summary field name. If there are multiple summary fields with the same * name, the last one will be used. Multiple fields of the same name should all have the same content in a valid * search definition, except from the destination set. So this method can be used for all summary handling except - * processing the destination set.</p> <p>The map gets owned by the receiver.</p> - * - * @return Map of unique summary fields + * processing the destination set. The map becomes owned by the receiver. */ public Map<String, SummaryField> getUniqueNamedSummaryFields() { - Map<String, SummaryField> summaryFields = new java.util.LinkedHashMap<>(); + Map<String, SummaryField> summaryFields = inherited.isPresent() ? requireInherited().getUniqueNamedSummaryFields() + : new java.util.LinkedHashMap<>(); for (DocumentSummary documentSummary : summaries.values()) { for (SummaryField summaryField : documentSummary.getSummaryFields()) { summaryFields.put(summaryField.getName(), summaryField); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/processing/ImplicitSummaryFields.java b/config-model/src/main/java/com/yahoo/searchdefinition/processing/ImplicitSummaryFields.java index 7875bd6f4d5..ff0a0d07102 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/processing/ImplicitSummaryFields.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/processing/ImplicitSummaryFields.java @@ -22,7 +22,7 @@ public class ImplicitSummaryFields extends Processor { @Override public void process(boolean validate, boolean documentsOnly) { - for (DocumentSummary docsum : schema.getSummaries().values()) { + for (DocumentSummary docsum : schema.getSummariesInThis().values()) { if (docsum.getInherited() != null) continue; // Implicit fields are added to inheriting summaries through their parent addField(docsum, new SummaryField("rankfeatures", DataType.STRING, SummaryTransform.RANKFEATURES), validate); addField(docsum, new SummaryField("summaryfeatures", DataType.STRING, SummaryTransform.SUMMARYFEATURES), validate); @@ -36,4 +36,5 @@ public class ImplicitSummaryFields extends Processor { } docsum.add(field); } + } diff --git a/config-model/src/main/java/com/yahoo/vespa/documentmodel/DocumentSummary.java b/config-model/src/main/java/com/yahoo/vespa/documentmodel/DocumentSummary.java index e0f0178cee3..1ebaf5d65e6 100644 --- a/config-model/src/main/java/com/yahoo/vespa/documentmodel/DocumentSummary.java +++ b/config-model/src/main/java/com/yahoo/vespa/documentmodel/DocumentSummary.java @@ -55,6 +55,7 @@ public class DocumentSummary extends FieldView { } public SummaryField getSummaryField(String name) { + // TODO: This before parent? var parent = getInherited(); if (parent != null) { return parent.getSummaryField(name); @@ -62,6 +63,11 @@ public class DocumentSummary extends FieldView { return (SummaryField) get(name); } + /** Returns a summary field if defined in this, not any parent */ + public SummaryField getSummaryFieldInThis(String name) { + return (SummaryField) get(name); + } + public Collection<SummaryField> getSummaryFields() { var fields = new ArrayList<SummaryField>(getFields().size()); var parent = getInherited(); diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java index f3b07277d0c..5801f25f0af 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java @@ -95,6 +95,9 @@ public class SchemaTestCase { " onnx-model parent_model {" + " file: models/my_model.onnx" + " }" + + " document-summary parent_summary {" + + " summary pf1 type string {}" + + " }" + "}"); String childLines = joinLines( "schema child inherits parent {" + @@ -123,6 +126,11 @@ public class SchemaTestCase { assertTrue(child.rankingConstants().asMap().containsKey("parent_constant")); assertNotNull(child.onnxModels().get("parent_model")); assertTrue(child.onnxModels().asMap().containsKey("parent_model")); + assertNotNull(child.getSummary("parent_summary")); + assertTrue(child.getSummaries().containsKey("parent_summary")); + assertNotNull(child.getSummaryField("pf1")); + assertNotNull(child.getExplicitSummaryField("pf1")); + assertNotNull(child.getUniqueNamedSummaryFields().get("pf1")); } } |