diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-10-18 15:35:17 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-10-18 15:35:17 +0200 |
commit | 6f6f21414a004a967ce54ab2a0a47e29b1f8609a (patch) | |
tree | 9535901764581c6c9f1c36bc2c70836d69e3bd85 /config-model | |
parent | fdfdc83dab661fdef0da968392f3f50708b4b74f (diff) |
Inherit rank profiles
Diffstat (limited to 'config-model')
8 files changed, 58 insertions, 30 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/Application.java b/config-model/src/main/java/com/yahoo/searchdefinition/Application.java index 6b9034f35e0..19a536a18f7 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/Application.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/Application.java @@ -19,14 +19,23 @@ import java.util.Map; public class Application { private final ApplicationPackage applicationPackage; + private final RankProfileRegistry rankProfileRegistry; private final Map<String, Schema> schemas = new LinkedHashMap<>(); public Application(ApplicationPackage applicationPackage) { + this(applicationPackage, new RankProfileRegistry()); + } + + // TODO: Almost sure the rank profile registry passed is always new RankProfileRegistry() (apart from in some tests), so remove the parameter + public Application(ApplicationPackage applicationPackage, RankProfileRegistry rankProfileRegistry) { this.applicationPackage = applicationPackage; + this.rankProfileRegistry = rankProfileRegistry; } public ApplicationPackage applicationPackage() { return applicationPackage; } + public RankProfileRegistry rankProfileRegistry() { return rankProfileRegistry; } + public void add(Schema schema) { if (schemas.containsKey(schema.getName())) throw new IllegalArgumentException("Duplicate schema '" + schema.getName() + "' in " + this); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/ImmutableSearch.java b/config-model/src/main/java/com/yahoo/searchdefinition/ImmutableSearch.java index 1208707fe96..d0a39524a0e 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/ImmutableSearch.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/ImmutableSearch.java @@ -12,6 +12,7 @@ import com.yahoo.vespa.documentmodel.SummaryField; import java.io.Reader; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Stream; /** @@ -23,6 +24,7 @@ import java.util.stream.Stream; public interface ImmutableSearch { String getName(); + Optional<? extends ImmutableSearch> inherited(); Index getIndex(String name); ImmutableSDField getConcreteField(String name); //TODO split in mutating/immutable by returning List<ImmutableSDField> @@ -45,4 +47,5 @@ public interface ImmutableSearch { List<ImmutableSDField> allFieldsList(); Map<String, SummaryField> getSummaryFields(ImmutableSDField field); + } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java index d48b232cfef..889fd20cb92 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -166,7 +166,8 @@ public class RankProfile implements Cloneable { * * @param name the name of the new profile */ - public RankProfile(String name, ApplicationPackage applicationPackage, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, RankingConstants rankingConstants, OnnxModels onnxModels) { + public RankProfile(String name, ApplicationPackage applicationPackage, DeployLogger deployLogger, + RankProfileRegistry rankProfileRegistry, RankingConstants rankingConstants, OnnxModels onnxModels) { this.name = Objects.requireNonNull(name, "name cannot be null"); this.search = null; this.rankProfileRegistry = rankProfileRegistry; @@ -254,8 +255,8 @@ public class RankProfile implements Cloneable { } } - private RankProfile resolveInherited(ImmutableSearch search) { - SDDocumentType documentType = search.getDocument(); + private RankProfile resolveInherited(ImmutableSearch schema) { + SDDocumentType documentType = schema.getDocument(); if (documentType != null) { if (name.equals(inheritedName)) { // If you seemingly inherit yourself, you are actually referencing a rank-profile in one of your inherited schemas @@ -266,7 +267,7 @@ public class RankProfile implements Cloneable { } return rankProfileRegistry.resolve(documentType, inheritedName); } - return rankProfileRegistry.get(search.getName(), inheritedName); + return rankProfileRegistry.get(schema.getName(), inheritedName); } private RankProfile resolveInherited() { diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfileRegistry.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfileRegistry.java index 50ba7d07baa..90075343be9 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfileRegistry.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfileRegistry.java @@ -30,6 +30,10 @@ public class RankProfileRegistry { /* These rank profiles can be overridden: 'default' rank profile, as that is documented to work. And 'unranked'. */ static final Set<String> overridableRankProfileNames = new HashSet<>(Arrays.asList("default", "unranked")); + public RankProfileRegistry() { + + } + public static RankProfileRegistry createRankProfileRegistryWithBuiltinRankProfiles(Schema schema) { RankProfileRegistry rankProfileRegistry = new RankProfileRegistry(); rankProfileRegistry.add(new DefaultRankProfile(schema, rankProfileRegistry, schema.rankingConstants())); @@ -69,17 +73,21 @@ public class RankProfileRegistry { /** * Returns a named rank profile, null if the search definition doesn't have one with the given name * - * @param search the {@link Schema} that owns the rank profile. + * @param schema the {@link Schema} that owns the rank profile * @param name the name of the rank profile * @return the RankProfile to return. */ - public RankProfile get(String search, String name) { - Map<String, RankProfile> profiles = rankProfiles.get(search); + public RankProfile get(String schema, String name) { + Map<String, RankProfile> profiles = rankProfiles.get(schema); if (profiles == null) return null; return profiles.get(name); } - public RankProfile get(ImmutableSearch search, String name) { - return get(search.getName(), name); + + public RankProfile get(ImmutableSearch schema, String name) { + var profile = get(schema.getName(), name); + if (profile != null) return profile; + if (schema.inherited().isPresent()) return get(schema.inherited().get(), name); + return null; } public RankProfile getGlobal(String name) { 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 bbf17a44613..fcecac3fc2e 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java @@ -153,15 +153,16 @@ public class Schema implements ImmutableSearch { return name; } - /** - * Returns true if this doesn't define a search, just some documents - * - * @return if the searchdefinition only has documents - */ + /** Returns true if this only defines a document type, not a full schema */ public boolean isDocumentsOnly() { return documentsOnly; } + @Override + public Optional<Schema> inherited() { + return inherited.map(name -> owner.schemas().get(name)); + } + /** * Returns true if 'raw' fields shall be presented as base64 in summary * Note that this is temporary and will disappear on Vespa 8 as it will become default, and only option. @@ -186,7 +187,7 @@ public class Schema implements ImmutableSearch { public Stemming getStemming() { if (stemming != null) return stemming; if (inherited.isEmpty()) return Stemming.BEST; - return inherited().getStemming(); + return requireInherited().getStemming(); } /** @@ -369,7 +370,7 @@ public class Schema implements ImmutableSearch { public Collection<SDField> extraFieldList() { if (inherited.isEmpty()) return fields.values(); - var fields = new HashSet<>(inherited().extraFieldList()); + var fields = new HashSet<>(requireInherited().extraFieldList()); fields.addAll(this.fields.values()); return fields; } @@ -377,7 +378,7 @@ public class Schema implements ImmutableSearch { public Collection<SDField> allExtraFields() { Map<String, SDField> extraFields = new TreeMap<>(); if (inherited.isPresent()) - inherited().allExtraFields().forEach(field -> extraFields.put(field.getName(), field)); + requireInherited().allExtraFields().forEach(field -> extraFields.put(field.getName(), field)); for (Field field : documentType.fieldSet()) { SDField sdField = (SDField) field; if (sdField.isExtraField()) { @@ -400,7 +401,7 @@ public class Schema implements ImmutableSearch { SDField field = fields.get(fieldName); if (field != null) return field; if (inherited.isEmpty()) return null; - return inherited().getExtraField(fieldName); + return requireInherited().getExtraField(fieldName); } /** @@ -438,14 +439,14 @@ public class Schema implements ImmutableSearch { /** Returns the schema level index of this name, in this or any inherited schema, if any */ Optional<Index> getSchemaIndex(String name) { if (indices.containsKey(name)) return Optional.of(indices.get(name)); - if (inherited.isPresent()) return inherited().getSchemaIndex(name); + if (inherited.isPresent()) return requireInherited().getSchemaIndex(name); return Optional.empty(); } public boolean existsIndex(String name) { if (indices.get(name) != null) return true; - if (inherited.isPresent() && inherited().existsIndex(name)) + if (inherited.isPresent() && requireInherited().existsIndex(name)) return true; for (ImmutableSDField field : allConcreteFields()) { if (field.existsIndex(name)) @@ -498,7 +499,7 @@ public class Schema implements ImmutableSearch { List<Index> allIndices = new ArrayList<>(indices.values()); if (inherited.isPresent()) { - for (Index inheritedIndex : inherited().getExplicitIndices()) { + for (Index inheritedIndex : requireInherited().getExplicitIndices()) { if ( ! indices.containsKey(inheritedIndex.getName())) // child redefinitions shadows parents allIndices.add(inheritedIndex); } @@ -681,13 +682,13 @@ public class Schema implements ImmutableSearch { public FieldSets fieldSets() { if (inherited.isEmpty()) return fieldSets; - var fieldSets = new FieldSets(inherited().fieldSets()); + var fieldSets = new FieldSets(requireInherited().fieldSets()); fieldSets.add(this.fieldSets); return fieldSets; } /** Returns the schema inherited by this, or throws if none */ - private Schema inherited() { return owner.schemas().get(inherited.get()); } + private Schema requireInherited() { return owner.schemas().get(inherited.get()); } /** * For adding structs defined in document scope diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/SearchBuilder.java b/config-model/src/main/java/com/yahoo/searchdefinition/SearchBuilder.java index 91a95ef3f44..6b64a15640c 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/SearchBuilder.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/SearchBuilder.java @@ -48,7 +48,6 @@ public class SearchBuilder { private final DocumentTypeManager docTypeMgr = new DocumentTypeManager(); private final DocumentModel model = new DocumentModel(); private final Application application; - private final RankProfileRegistry rankProfileRegistry; private final QueryProfileRegistry queryProfileRegistry; private final FileRegistry fileRegistry; private final DeployLogger deployLogger; @@ -117,8 +116,7 @@ public class SearchBuilder { RankProfileRegistry rankProfileRegistry, QueryProfileRegistry queryProfileRegistry, boolean documentsOnly) { - this.application = new Application(applicationPackage); - this.rankProfileRegistry = rankProfileRegistry; + this.application = new Application(applicationPackage, rankProfileRegistry); this.queryProfileRegistry = queryProfileRegistry; this.fileRegistry = fileRegistry; this.deployLogger = deployLogger; @@ -170,7 +168,8 @@ public class SearchBuilder { private String importString(String str, String searchDefDir) throws ParseException { SimpleCharStream stream = new SimpleCharStream(str); try { - return importRawSearch(new SDParser(stream, fileRegistry, deployLogger, properties, application, rankProfileRegistry, documentsOnly) + return importRawSearch(new SDParser(stream, fileRegistry, deployLogger, properties, application, + application.rankProfileRegistry(), documentsOnly) .schema(docTypeMgr, searchDefDir)); } catch (TokenMgrException e) { throw new ParseException("Unknown symbol: " + e.getMessage()); @@ -266,7 +265,7 @@ public class SearchBuilder { * #build()} method so that subclasses can choose not to build anything. */ private void process(Schema schema, QueryProfiles queryProfiles, boolean validate) { - new Processing().process(schema, deployLogger, rankProfileRegistry, queryProfiles, validate, documentsOnly); + new Processing().process(schema, deployLogger, application.rankProfileRegistry(), queryProfiles, validate, documentsOnly); } /** @@ -533,7 +532,7 @@ public class SearchBuilder { } public RankProfileRegistry getRankProfileRegistry() { - return rankProfileRegistry; + return application.rankProfileRegistry(); } public QueryProfileRegistry getQueryProfileRegistry() { 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 07cba46c801..5799fdee238 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java @@ -85,6 +85,8 @@ public class SchemaTestCase { " field parent_field type string {" + " indexing: input pf1 | lowercase | index | attribute | summary" + " }" + + " rank-profile parent_profile {" + + " }" + "}"); String childLines = joinLines( "schema child inherits parent {" + @@ -101,6 +103,7 @@ public class SchemaTestCase { assertEquals(Stemming.BEST, child.getIndex("parent_index").getStemming()); assertNotNull(child.getField("parent_field")); assertNotNull(child.getExtraField("parent_field")); + assertNotNull(application.rankProfileRegistry().get(child, "parent_profile")); } } diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/ValidateFieldTypesTest.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/ValidateFieldTypesTest.java index 4c56d2a5ab0..a3032acd9cc 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/ValidateFieldTypesTest.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/ValidateFieldTypesTest.java @@ -49,7 +49,11 @@ public class ValidateFieldTypesTest { } private static Schema createSearch(String documentType) { - return new Schema(documentType, new Application(MockApplicationPackage.createEmpty()), new MockFileRegistry(), new TestableDeployLogger(), new TestProperties()); + return new Schema(documentType, + new Application(MockApplicationPackage.createEmpty()), + new MockFileRegistry(), + new TestableDeployLogger(), + new TestProperties()); } private static Schema createSearchWithDocument(String documentName) { |