summaryrefslogtreecommitdiffstats
path: root/container-search
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2023-02-15 11:50:07 +0100
committerTor Egge <Tor.Egge@online.no>2023-02-15 11:50:07 +0100
commit76a847e31d8d4a2a79c967864e7d7466e67610f2 (patch)
treeab46cbc297b7eb5ad3ce24891e498aeeebc1d225 /container-search
parenta9486b78d445db8279169a8cb47575471ca3232d (diff)
Add string command to index info config.
Use index info to validate fuzzy query terms.
Diffstat (limited to 'container-search')
-rw-r--r--container-search/abi-spec.json2
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/Index.java10
-rw-r--r--container-search/src/main/java/com/yahoo/search/searchers/ValidateFuzzySearcher.java33
-rw-r--r--container-search/src/test/java/com/yahoo/search/searchers/ValidateFuzzySearcherTestCase.java33
4 files changed, 46 insertions, 32 deletions
diff --git a/container-search/abi-spec.json b/container-search/abi-spec.json
index 785475ae77e..40c86902f5c 100644
--- a/container-search/abi-spec.json
+++ b/container-search/abi-spec.json
@@ -8561,7 +8561,7 @@
"public"
],
"methods" : [
- "public void <init>(com.yahoo.vespa.config.search.AttributesConfig)",
+ "public void <init>()",
"public com.yahoo.search.Result search(com.yahoo.search.Query, com.yahoo.search.searchchain.Execution)"
],
"fields" : [ ]
diff --git a/container-search/src/main/java/com/yahoo/prelude/Index.java b/container-search/src/main/java/com/yahoo/prelude/Index.java
index 48d8e382722..e245faec919 100644
--- a/container-search/src/main/java/com/yahoo/prelude/Index.java
+++ b/container-search/src/main/java/com/yahoo/prelude/Index.java
@@ -46,7 +46,9 @@ public class Index {
private boolean normalize = false;
private boolean literalBoost = false;
private boolean numerical = false;
+ private boolean string = false;
private boolean predicate = false;
+
private long predicateUpperBound = Long.MAX_VALUE;
private long predicateLowerBound = Long.MIN_VALUE;
@@ -68,6 +70,8 @@ public class Index {
/** All the commands added to this, including those converted to fields above */
private final List<String> allCommands = new java.util.ArrayList<>();
+ private static final String CMD_STRING = "string";
+
public Index(String name) {
this.name = name;
}
@@ -175,6 +179,8 @@ public class Index {
setPhraseSegmenting(true);
} else if (command.startsWith("phrase-segmenting ")) {
setPhraseSegmenting(Boolean.parseBoolean(command.substring("phrase-segmenting ".length())));
+ } else if (command.equals(CMD_STRING)) {
+ setString(true);
} else {
commands.add(command);
}
@@ -296,6 +302,10 @@ public class Index {
public boolean isNumerical() { return numerical; }
+ public void setString(boolean string) { this.string = string; }
+
+ public boolean isString() { return string; }
+
public void setPredicate(boolean isPredicate) { this.predicate = isPredicate; }
public boolean isPredicate() { return predicate; }
diff --git a/container-search/src/main/java/com/yahoo/search/searchers/ValidateFuzzySearcher.java b/container-search/src/main/java/com/yahoo/search/searchers/ValidateFuzzySearcher.java
index 249a6342da6..43caaacc324 100644
--- a/container-search/src/main/java/com/yahoo/search/searchers/ValidateFuzzySearcher.java
+++ b/container-search/src/main/java/com/yahoo/search/searchers/ValidateFuzzySearcher.java
@@ -2,6 +2,8 @@
package com.yahoo.search.searchers;
import com.yahoo.prelude.query.FuzzyItem;
+import com.yahoo.prelude.Index;
+import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.ToolBox;
import com.yahoo.search.Query;
@@ -26,24 +28,17 @@ import java.util.Set;
@Before(GroupingExecutor.COMPONENT_NAME) // Must happen before query.prepare()
public class ValidateFuzzySearcher extends Searcher {
- private final Set<String> validAttributes = new HashSet<>();
-
- public ValidateFuzzySearcher(AttributesConfig attributesConfig) {
- for (AttributesConfig.Attribute a : attributesConfig.attribute()) {
- if (a.datatype() == AttributesConfig.Attribute.Datatype.STRING) {
- validAttributes.add(a.name());
- }
- }
+ public ValidateFuzzySearcher() {
}
@Override
public Result search(Query query, Execution execution) {
- Optional<ErrorMessage> e = validate(query);
+ Optional<ErrorMessage> e = validate(query, execution.context().getIndexFacts().newSession(query));
return e.isEmpty() ? execution.search(query) : new Result(query, e.get());
}
- private Optional<ErrorMessage> validate(Query query) {
- FuzzyVisitor visitor = new FuzzyVisitor(query.getRanking().getProperties(), validAttributes, query);
+ private Optional<ErrorMessage> validate(Query query, IndexFacts.Session indexFacts) {
+ FuzzyVisitor visitor = new FuzzyVisitor(indexFacts);
ToolBox.visit(visitor, query.getModel().getQueryTree().getRoot());
return visitor.errorMessage;
}
@@ -52,12 +47,10 @@ public class ValidateFuzzySearcher extends Searcher {
public Optional<ErrorMessage> errorMessage = Optional.empty();
- private final Set<String> validAttributes;
- private final Query query;
+ private final IndexFacts.Session indexFacts;
- public FuzzyVisitor(RankProperties rankProperties, Set<String> validAttributes, Query query) {
- this.validAttributes = validAttributes;
- this.query = query;
+ public FuzzyVisitor(IndexFacts.Session indexFacts) {
+ this.indexFacts = indexFacts;
}
@Override
@@ -72,7 +65,9 @@ public class ValidateFuzzySearcher extends Searcher {
/** Returns an error message if this is invalid, or null if it is valid */
private String validate(FuzzyItem item) {
- if (!validAttributes.contains(item.getIndexName())) {
+ String indexName = item.getIndexName();
+ Index index = getIndexFromUnionOfDocumentTypes(indexName);
+ if (!index.isAttribute() || !index.isString()) {
return item + " field is not a string attribute";
}
if (item.getPrefixLength() < 0) {
@@ -87,6 +82,10 @@ public class ValidateFuzzySearcher extends Searcher {
return null;
}
+ private Index getIndexFromUnionOfDocumentTypes(String indexName) {
+ return indexFacts.getIndex(indexName);
+ }
+
@Override
public void onExit() {}
diff --git a/container-search/src/test/java/com/yahoo/search/searchers/ValidateFuzzySearcherTestCase.java b/container-search/src/test/java/com/yahoo/search/searchers/ValidateFuzzySearcherTestCase.java
index 1cf8159e19e..c4d9734fb5d 100644
--- a/container-search/src/test/java/com/yahoo/search/searchers/ValidateFuzzySearcherTestCase.java
+++ b/container-search/src/test/java/com/yahoo/search/searchers/ValidateFuzzySearcherTestCase.java
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchers;
+import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
@@ -12,7 +13,6 @@ import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.yql.YqlParser;
-import com.yahoo.vespa.config.search.AttributesConfig;
import com.yahoo.vespa.config.search.AttributesConfig.Attribute;
import org.junit.jupiter.api.Test;
@@ -31,26 +31,28 @@ public class ValidateFuzzySearcherTestCase {
List<String> attributes;
+ private List<Index> indexes;
+
+ private static final String CMD_ATTRIBUTE = "attribute";
+ private static final String CMD_STRING = "string";
+
public ValidateFuzzySearcherTestCase() {
attributes = new ArrayList<>();
- AttributesConfig.Builder configBuilder = new AttributesConfig.Builder();
- List<AttributesConfig.Attribute.Builder> attributesList = new ArrayList<>();
+ indexes = new ArrayList<>();
for (Attribute.Datatype.Enum attr: Attribute.Datatype.Enum.values()) {
for (Attribute.Collectiontype.Enum ctype: Attribute.Collectiontype.Enum.values()) {
- AttributesConfig.Attribute.Builder attributesBuilder = new AttributesConfig.Attribute.Builder();
String attributeName = attr.name().toLowerCase() + "_" + ctype.name().toLowerCase();
- attributesBuilder.name(attributeName);
- attributesBuilder.datatype(attr);
- attributesBuilder.collectiontype(ctype);
- attributesList.add(attributesBuilder);
-
attributes.add(attributeName);
+
+ Index index = new Index(attributeName);
+ index.addCommand(CMD_ATTRIBUTE);
+ if (attr == Attribute.Datatype.STRING) {
+ index.addCommand(CMD_STRING);
+ }
+ indexes.add(index);
}
}
- configBuilder.attribute(attributesList);
- AttributesConfig config = configBuilder.build();
-
- searcher = new ValidateFuzzySearcher(config);
+ searcher = new ValidateFuzzySearcher();
}
private String makeQuery(String attribute, String query, int maxEditDistance, int prefixLength) {
@@ -111,11 +113,14 @@ public class ValidateFuzzySearcherTestCase {
assertEquals(ErrorMessage.createIllegalQuery(message), r.hits().getError());
}
- private static Result doSearch(ValidateFuzzySearcher searcher, String yqlQuery) {
+ private Result doSearch(ValidateFuzzySearcher searcher, String yqlQuery) {
QueryTree queryTree = new YqlParser(new ParserEnvironment()).parse(new Parsable().setQuery(yqlQuery));
Query query = new Query();
query.getModel().getQueryTree().setRoot(queryTree.getRoot());
SearchDefinition searchDefinition = new SearchDefinition("document");
+ for (Index index : indexes) {
+ searchDefinition.addIndex(index);
+ }
IndexFacts indexFacts = new IndexFacts(new IndexModel(searchDefinition));
return new Execution(searcher, Execution.Context.createContextStub(indexFacts)).search(query);
}