From abea2d0c3b3e42e2dd0e8a0cd41a44caf97198ba Mon Sep 17 00:00:00 2001 From: takamabe <7151956+takamabe@users.noreply.github.com> Date: Wed, 1 Sep 2021 08:20:00 +0900 Subject: correct heuristic for -(N) --- .../src/main/java/com/yahoo/prelude/query/parser/AllParser.java | 4 ++++ .../java/com/yahoo/prelude/query/parser/test/ParseTestCase.java | 7 +++++++ 2 files changed, 11 insertions(+) (limited to 'container-search/src') diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/AllParser.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/AllParser.java index 49bdba2c90f..793d394801f 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/parser/AllParser.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/AllParser.java @@ -112,6 +112,7 @@ public class AllParser extends SimpleParser { protected Item negativeItem() { int position = tokens.getPosition(); Item item = null; + boolean isComposited = false; try { if ( ! tokens.skip(MINUS)) return null; if (tokens.currentIsNoIgnore(SPACE)) return null; @@ -121,6 +122,7 @@ public class AllParser extends SimpleParser { item = compositeItem(); if (item != null) { + isComposited = true; if (item instanceof OrItem) { // Turn into And AndItem and = new AndItem(); @@ -137,9 +139,11 @@ public class AllParser extends SimpleParser { // Heuristic overdrive engaged! // Interpret -N as a positive item matching a negative number (by backtracking out of this) // but not if there is an explicit index (such as -a:b) + // but interpret -(N) as a negative item matching a positive number // but interpret --N as a negative item matching a negative number if (item instanceof IntItem && ((IntItem)item).getIndexName().isEmpty() && + ! isComposited && ! ((IntItem)item).getNumber().startsWith(("-"))) item = null; diff --git a/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParseTestCase.java b/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParseTestCase.java index cef8ae1751c..8ca711297d3 100644 --- a/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParseTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParseTestCase.java @@ -1969,6 +1969,13 @@ public class ParseTestCase { tester.assertParsed("+a -normal:-12", "a -normal:-12", Query.Type.ALL); } + @Test + public void testNegativeTermPositiveNumberInParentheses() { + tester.assertParsed("+a -12", "a -(12)", Query.Type.ALL); + tester.assertParsed("+a -(AND 12 15)", "a -(12 15)", Query.Type.ALL); + tester.assertParsed("+a -12 -15", "a -(12) -(15)", Query.Type.ALL); + } + @Test public void testSingleNegativeNumberLikeTerm() { tester.assertParsed("-12", "-12", Query.Type.ALL); -- cgit v1.2.3