diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-01-06 15:51:12 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-01-06 15:51:12 +0100 |
commit | e2e44dbccb30c3b9673518f3204699e137b81fcc (patch) | |
tree | c80664011b253a3f2ca2c6daf046fe659fdd00f2 | |
parent | 1366eb01c678345c2879ea092ab8737d0f087a89 (diff) |
Allow negative terms only in YQL
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/yql/YqlParser.java | 16 | ||||
-rw-r--r-- | container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java | 12 |
2 files changed, 20 insertions, 8 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java b/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java index 8334775b8e2..199cf7bb2a9 100644 --- a/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java +++ b/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java @@ -356,6 +356,8 @@ public class YqlParser implements Parser { return buildFunctionCall(ast); case LITERAL: return buildLiteral(ast); + case NOT: + return buildNot(ast); default: throw newUnexpectedArgumentException(ast.getOperator(), ExpressionOperator.AND, ExpressionOperator.CALL, @@ -1096,17 +1098,21 @@ public class YqlParser implements Parser { AndItem andItem = new AndItem(); NotItem notItem = new NotItem(); convertVarArgsAnd(ast, 0, andItem, notItem); - Preconditions - .checkArgument(andItem.getItemCount() > 0, - "Vespa does not support AND with no logically positive branches."); if (notItem.getItemCount() == 0) { return andItem; } if (andItem.getItemCount() == 1) { notItem.setPositiveItem(andItem.getItem(0)); - } else { + } else if (andItem.getItemCount() > 1) { notItem.setPositiveItem(andItem); - } + } // else no positives, which is ok + return notItem; + } + + /** Build a "pure" not, without any positive terms. */ + private CompositeItem buildNot(OperatorNode<ExpressionOperator> ast) { + NotItem notItem = new NotItem(); + notItem.addNegativeItem(convertExpression(ast.getArgument(0))); return notItem; } diff --git a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java index 351cf75b1b0..2a4980f6590 100644 --- a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java @@ -193,9 +193,15 @@ public class YqlParserTestCase { } @Test - public void testNot() { - assertParse("select foo from bar where title contains \"madonna\" and !(title contains \"saint\")", - "+title:madonna -title:saint"); + public void testSingleNot() { + assertParse("select foo from bar where !(title contains \"saint\")", + "+TRUE -title:saint"); + } + + @Test + public void testMultipleNot() { + assertParse("select foo from bar where !(title contains \"saint\") AND !(title contains \"etienne\")", + "+TRUE -title:saint -title:etienne"); } @Test |