summaryrefslogtreecommitdiffstats
path: root/container-search
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-04-09 13:44:59 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-04-09 13:44:59 +0200
commitd623049d2ba3b12c90a1592580e4b4f238f14731 (patch)
tree32db702508cf315d010eb92c695dda8fffcb9a0c /container-search
parentb8f02d9ef033d418764d1af6ebfd773fb2673086 (diff)
Make QueryTree.and public
- Make QueryTree.and public as it handles more cases than most alternatives - Implement QueryTreeUtils methodas by calling QueryTree.and - Deprecate QueryTreeUtils
Diffstat (limited to 'container-search')
-rw-r--r--container-search/src/main/java/com/yahoo/search/query/QueryTree.java13
-rw-r--r--container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java33
-rw-r--r--container-search/src/test/java/com/yahoo/search/query/QueryTreeTest.java26
-rw-r--r--container-search/src/test/java/com/yahoo/search/querytransform/TestUtils.java2
4 files changed, 55 insertions, 19 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/QueryTree.java b/container-search/src/main/java/com/yahoo/search/query/QueryTree.java
index bacfe8a949a..6eba6ae4837 100644
--- a/container-search/src/main/java/com/yahoo/search/query/QueryTree.java
+++ b/container-search/src/main/java/com/yahoo/search/query/QueryTree.java
@@ -108,9 +108,12 @@ public class QueryTree extends CompositeItem {
// -------------- Facade
- /** Modifies this query to become the current query AND the given item */
- // TODO: Make sure this is complete, unit test and make it public
- private void and(Item item) {
+ /**
+ * Modifies this query to become the current query AND the given item.
+ *
+ * @return the resulting root item in this
+ */
+ public Item and(Item item) {
if (isEmpty()) {
setRoot(item);
}
@@ -126,12 +129,16 @@ public class QueryTree extends CompositeItem {
notItem.addPositiveItem(getRoot());
setRoot(notItem);
}
+ else if (getRoot() instanceof AndItem) {
+ ((AndItem) getRoot()).addItem(item);
+ }
else {
AndItem andItem = new AndItem();
andItem.addItem(getRoot());
andItem.addItem(item);
setRoot(andItem);
}
+ return getRoot();
}
/** Returns a flattened list of all positive query terms under the given item */
diff --git a/container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java b/container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java
index e4841ae6bd1..759c8ba1ee4 100644
--- a/container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java
+++ b/container-search/src/main/java/com/yahoo/search/querytransform/QueryTreeUtil.java
@@ -3,6 +3,7 @@ package com.yahoo.search.querytransform;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.Item;
+import com.yahoo.prelude.query.QueryCanonicalizer;
import com.yahoo.search.Query;
import com.yahoo.search.query.QueryTree;
@@ -10,27 +11,27 @@ import com.yahoo.search.query.QueryTree;
* Utility class for manipulating a QueryTree.
*
* @author geirst
+ * @deprecated use QueryTree.and instead // TODO: Remove on Vespa 8
*/
+@Deprecated
public class QueryTreeUtil {
- static public void andQueryItemWithRoot(Query query, Item item) {
- andQueryItemWithRoot(query.getModel().getQueryTree(), item);
+ /**
+ * Adds the given item to this query
+ *
+ * @return the new root of the query tree
+ */
+ static public Item andQueryItemWithRoot(Query query, Item item) {
+ return andQueryItemWithRoot(query.getModel().getQueryTree(), item);
}
- static public void andQueryItemWithRoot(QueryTree tree, Item item) {
- if (tree.isEmpty()) {
- tree.setRoot(item);
- } else {
- Item oldRoot = tree.getRoot();
- if (oldRoot.getClass() == AndItem.class) {
- ((AndItem) oldRoot).addItem(item);
- } else {
- AndItem newRoot = new AndItem();
- newRoot.addItem(oldRoot);
- newRoot.addItem(item);
- tree.setRoot(newRoot);
- }
- }
+ /**
+ * Adds the given item to this query
+ *
+ * @return the new root of the query tree
+ */
+ static public Item andQueryItemWithRoot(QueryTree tree, Item item) {
+ return tree.and(item);
}
}
diff --git a/container-search/src/test/java/com/yahoo/search/query/QueryTreeTest.java b/container-search/src/test/java/com/yahoo/search/query/QueryTreeTest.java
new file mode 100644
index 00000000000..f929e54fd2d
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/query/QueryTreeTest.java
@@ -0,0 +1,26 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.query;
+
+import com.yahoo.prelude.query.NotItem;
+import com.yahoo.prelude.query.WordItem;
+import org.junit.Assert;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author bratseth
+ */
+public class QueryTreeTest {
+
+ @Test
+ public void testAddQueryItemWithRoot() {
+ Assert.assertEquals("AND a b",
+ new QueryTree(new WordItem("a")).and(new WordItem("b")).toString());
+
+ NotItem not = new NotItem();
+ not.addNegativeItem(new WordItem("b"));
+ assertEquals("+a -b",
+ new QueryTree(new WordItem("a")).and(not).toString());
+ }
+
+}
diff --git a/container-search/src/test/java/com/yahoo/search/querytransform/TestUtils.java b/container-search/src/test/java/com/yahoo/search/querytransform/TestUtils.java
index c7a44e8aceb..720dcd0c4bc 100644
--- a/container-search/src/test/java/com/yahoo/search/querytransform/TestUtils.java
+++ b/container-search/src/test/java/com/yahoo/search/querytransform/TestUtils.java
@@ -6,7 +6,9 @@ import com.yahoo.prelude.query.Item;
import com.yahoo.search.Result;
public class TestUtils {
+
public static Item getQueryTreeRoot(Result result) {
return result.getQuery().getModel().getQueryTree().getRoot();
}
+
}