Skip to content

Commit 2ece781

Browse files
authored
Merge pull request #32674 from vespa-engine/bratseth/more-validate-sorting
Bratseth/more validate sorting
2 parents 16ec113 + 558fc5e commit 2ece781

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

config-model/src/main/java/com/yahoo/schema/document/Attribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public enum Type {
109109
private final String exportAttributeTypeName;
110110

111111
Type(String name, String exportAttributeTypeName) {
112-
this.myName=name;
112+
this.myName = name;
113113
this.exportAttributeTypeName = exportAttributeTypeName;
114114
}
115115

container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,32 @@
2929
@After(ACCENT_REMOVAL)
3030
public class ValidateSortingSearcher extends Searcher {
3131

32+
// TODO: Use SchemaInfo instead and validate with streaming as well
33+
34+
private final boolean isStreaming;
35+
3236
private Map<String, AttributesConfig.Attribute> attributeNames = null;
3337
private String clusterName = "";
34-
private final boolean enabled;
3538

3639
public ValidateSortingSearcher(ClusterConfig clusterConfig, AttributesConfig attributesConfig) {
3740
initAttributeNames(attributesConfig);
3841
setClusterName(clusterConfig.clusterName());
39-
enabled = clusterConfig.indexMode() != ClusterConfig.IndexMode.Enum.STREAMING;
42+
isStreaming = clusterConfig.indexMode() == ClusterConfig.IndexMode.Enum.STREAMING;
4043
}
4144

42-
public String getClusterName() {
43-
return clusterName;
45+
@Override
46+
public Result search(Query query, Execution execution) {
47+
ErrorMessage error = validate(query);
48+
if (! isStreaming && error != null)
49+
return new Result(query, error);
50+
return execution.search(query);
4451
}
4552

46-
public void setClusterName(String clusterName) {
47-
this.clusterName = clusterName;
48-
}
53+
public String getClusterName() { return clusterName; }
4954

50-
private Map<String, AttributesConfig.Attribute> getAttributeNames() {
51-
return attributeNames;
52-
}
55+
public void setClusterName(String clusterName) { this.clusterName = clusterName; }
56+
57+
private Map<String, AttributesConfig.Attribute> getAttributeNames() { return attributeNames; }
5358

5459
public void setAttributeNames(Map<String, AttributesConfig.Attribute> attributeNames) {
5560
this.attributeNames = attributeNames;
@@ -64,32 +69,6 @@ public void initAttributeNames(AttributesConfig config) {
6469
setAttributeNames(attributes);
6570
}
6671

67-
@Override
68-
public Result search(Query query, Execution execution) {
69-
ErrorMessage e = validate(query);
70-
if (enabled && e != null) {
71-
Result r = new Result(query);
72-
r.hits().addError(e);
73-
return r;
74-
}
75-
return execution.search(query);
76-
}
77-
78-
private static Sorting.UcaSorter.Strength config2Strength(AttributesConfig.Attribute.Sortstrength.Enum s) {
79-
if (s == AttributesConfig.Attribute.Sortstrength.PRIMARY) {
80-
return Sorting.UcaSorter.Strength.PRIMARY;
81-
} else if (s == AttributesConfig.Attribute.Sortstrength.SECONDARY) {
82-
return Sorting.UcaSorter.Strength.SECONDARY;
83-
} else if (s == AttributesConfig.Attribute.Sortstrength.TERTIARY) {
84-
return Sorting.UcaSorter.Strength.TERTIARY;
85-
} else if (s == AttributesConfig.Attribute.Sortstrength.QUATERNARY) {
86-
return Sorting.UcaSorter.Strength.QUATERNARY;
87-
} else if (s == AttributesConfig.Attribute.Sortstrength.IDENTICAL) {
88-
return Sorting.UcaSorter.Strength.IDENTICAL;
89-
}
90-
return Sorting.UcaSorter.Strength.PRIMARY;
91-
}
92-
9372
private ErrorMessage validate(Query query) {
9473
Sorting sorting = query.getRanking().getSorting();
9574
List<Sorting.FieldOrder> l = (sorting != null) ? sorting.fieldOrders() : null;
@@ -145,6 +124,11 @@ private ErrorMessage validate(Query query) {
145124
f.setSorter(new Sorting.LowerCaseSorter(name));
146125
}
147126
}
127+
else if (attrConfig.datatype() == AttributesConfig.Attribute.Datatype.TENSOR) {
128+
throw new IllegalArgumentException("Cannot sort on field '" + attrConfig.name() +
129+
"' because it is a tensor");
130+
}
131+
148132
}
149133
if (f.getSorter() instanceof Sorting.UcaSorter sorter) {
150134
String locale = sorter.getLocale();
@@ -178,4 +162,19 @@ private ErrorMessage validate(Query query) {
178162
return null;
179163
}
180164

165+
private static Sorting.UcaSorter.Strength config2Strength(AttributesConfig.Attribute.Sortstrength.Enum s) {
166+
if (s == AttributesConfig.Attribute.Sortstrength.PRIMARY) {
167+
return Sorting.UcaSorter.Strength.PRIMARY;
168+
} else if (s == AttributesConfig.Attribute.Sortstrength.SECONDARY) {
169+
return Sorting.UcaSorter.Strength.SECONDARY;
170+
} else if (s == AttributesConfig.Attribute.Sortstrength.TERTIARY) {
171+
return Sorting.UcaSorter.Strength.TERTIARY;
172+
} else if (s == AttributesConfig.Attribute.Sortstrength.QUATERNARY) {
173+
return Sorting.UcaSorter.Strength.QUATERNARY;
174+
} else if (s == AttributesConfig.Attribute.Sortstrength.IDENTICAL) {
175+
return Sorting.UcaSorter.Strength.IDENTICAL;
176+
}
177+
return Sorting.UcaSorter.Strength.PRIMARY;
178+
}
179+
181180
}

container-search/src/test/java/com/yahoo/prelude/searcher/test/ValidateSortingSearcherTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ void testBasicValidation() {
4747
assertEquals("[ASCENDING:[rank]]", quoteAndTransform("+[relevance]"));
4848
}
4949

50+
@Test
51+
void testDisallowSortingOnTensors() {
52+
try {
53+
quoteAndTransform("aTensor");
54+
fail("Expected exception");
55+
}
56+
catch (IllegalArgumentException e) {
57+
assertEquals("Cannot sort on field 'aTensor' because it is a tensor", e.getMessage());
58+
}
59+
}
60+
5061
@Test
5162
void testInvalidSpec() {
5263
assertNull(quoteAndTransform("+a -e +c"));

container-search/src/test/java/com/yahoo/prelude/searcher/test/validate_sorting.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
attribute[4]
1+
attribute[5]
22
attribute[0].name title
33
attribute[0].datatype STRING
44
attribute[0].collectiontype SINGLE
@@ -15,3 +15,6 @@ attribute[2].collectiontype SINGLE
1515
attribute[3].name c
1616
attribute[3].datatype STRING
1717
attribute[3].collectiontype SINGLE
18+
attribute[4].name aTensor
19+
attribute[4].datatype TENSOR
20+
attribute[4].collectiontype SINGLE

0 commit comments

Comments
 (0)