meta query serialization and builders
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder.java
index c48f365..fb1a0e3 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder.java
@@ -6,7 +6,6 @@
import com.google.common.collect.Multiset;
import de.ids_mannheim.korap.resource.Relation;
import de.ids_mannheim.korap.utils.JsonUtils;
-import lombok.Data;
import java.io.IOException;
import java.util.*;
@@ -17,6 +16,7 @@
* @author hanl
* @date 06/12/2013
*/
+@Deprecated
public class CollectionQueryBuilder {
private CollectionTypes types;
@@ -55,6 +55,7 @@
return this;
}
+ @Deprecated
public CollectionQueryBuilder addMetaFilterQuery(String queries) {
this.mfilter.putAll(resRel(queries));
return this;
@@ -77,12 +78,13 @@
return this;
}
+ @Deprecated
public CollectionQueryBuilder addMetaExtendQuery(String queries) {
this.mextension.putAll(resRel(queries));
return this;
}
-
+ @Deprecated
private List<Map> createFilter(Relation rel) {
String relation = rel == Relation.AND ? "and" : "or";
List<Map> mfil = new ArrayList();
@@ -109,6 +111,7 @@
return mfil;
}
+ @Deprecated
private List<Map> createExtender(Relation rel) {
String relation = rel == Relation.AND ? "and" : "or";
List<Map> mex = new ArrayList();
@@ -233,7 +236,7 @@
*
* @return
*/
- public JsonNode buildNode() {
+ public JsonNode toNode() {
return JsonUtils.valueToTree(join());
}
@@ -299,27 +302,9 @@
}
-
public void clear() {
this.rq.clear();
this.mfilter.clear();
this.mextension.clear();
}
-
-
- private static interface Value {
- }
-
- @Data
- private static class Group implements Value {
- private Relation relation;
- private List<Term> _terms;
-
- }
-
- @Data
- private static class Term implements Value {
-
- private String _value;
- }
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder2.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder2.java
new file mode 100644
index 0000000..7f68b64
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryBuilder2.java
@@ -0,0 +1,77 @@
+package de.ids_mannheim.korap.query.serialize;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @author hanl
+ * @date 13/05/2014
+ */
+public class CollectionQueryBuilder2 {
+
+ private List<Map> rq;
+ private Map groups;
+ private CollectionTypes types;
+
+ public CollectionQueryBuilder2() {
+ this.rq = new ArrayList<>();
+ this.groups = new HashMap();
+ this.types = new CollectionTypes();
+ }
+
+
+ public CollectionQueryBuilder2 addResource(String collections) {
+ try {
+ List v = JsonUtils.read(collections, LinkedList.class);
+ this.rq.addAll(v);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Conversion went wrong!");
+ }
+ return this;
+ }
+
+ public CollectionQueryBuilder2 addResources(List<String> queries) {
+ for (String query : queries)
+ addResource(query);
+ return this;
+ }
+
+ public CollectionQueryBuilder2 setQuery(String query) throws QueryException {
+ CollectionQueryTree tree = new CollectionQueryTree();
+ tree.process(query);
+ this.groups = (Map) tree.getRequestMap().get("query");
+ System.out.println("RAW QUERY: " + this.groups);
+ return this;
+ }
+
+ public List raw() {
+ List list = new ArrayList(this.rq);
+ list.add(types.createMetaFilter(this.groups));
+ return list;
+ }
+
+ public String toCollections() {
+ Map value = new HashMap();
+ value.put("collections", raw());
+ return JsonUtils.toJSON(value);
+ }
+
+ public JsonNode toNode() {
+ return JsonUtils.valueToTree(raw());
+ }
+
+ public String toJSON() {
+ return JsonUtils.toJSON(raw());
+ }
+
+
+ // add public filter to original query
+ private void addToGroup() {
+ Map first = this.rq.get(0);
+
+ }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/ExpertFilter.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryTree.java
similarity index 92%
rename from src/main/java/de/ids_mannheim/korap/query/serialize/ExpertFilter.java
rename to src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryTree.java
index 5dcf845..588f1a6 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/ExpertFilter.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryTree.java
@@ -13,7 +13,7 @@
* @author hanl
* @date 06/12/2013
*/
-public class ExpertFilter extends Antlr4AbstractSyntaxTree {
+public class CollectionQueryTree extends Antlr4AbstractSyntaxTree {
private Parser parser;
private boolean verbose = false;
@@ -36,10 +36,6 @@
LinkedList<Integer> objectsToPop = new LinkedList<Integer>();
Integer stackedObjects = 0;
-
- public ExpertFilter() {
- }
-
@Override
public void process(String query) throws QueryException {
ParseTree tree = parseCollectionQuery(query);
@@ -96,7 +92,7 @@
if (valueNodes.size() == 1) {
LinkedHashMap<String, Object> term = makeTerm();
- term.put("attribute", field);
+ term.put("attribute", "korap:field#" + field);
term.put("key", valueNodes.get(0).getChild(0).toStringTree(parser));
String match = operatorNodes.get(0).getChild(0).toStringTree(parser);
term.put("match", "match:" + interpretMatch(match));
@@ -106,14 +102,14 @@
ArrayList<Object> termGroupOperands = (ArrayList<Object>) termGroup.get("operands");
LinkedHashMap<String, Object> term1 = makeTerm();
- term1.put("attribute", field);
+ term1.put("attribute", "korap:field#" + field);
term1.put("key", valueNodes.get(0).getChild(0).toStringTree(parser));
String match1 = operatorNodes.get(0).getChild(0).toStringTree(parser);
term1.put("match", "match:" + invertInequation(interpretMatch(match1)));
termGroupOperands.add(term1);
LinkedHashMap<String, Object> term2 = makeTerm();
- term2.put("attribute", field);
+ term2.put("attribute", "korap:field#" + field);
term2.put("key", valueNodes.get(1).getChild(0).toStringTree(parser));
String match2 = operatorNodes.get(1).getChild(0).toStringTree(parser);
term2.put("match", "match:" + interpretMatch(match2));
@@ -124,9 +120,9 @@
}
objectsToPop.push(stackedObjects);
-
+
/*
- ****************************************************************
+ ****************************************************************
****************************************************************
* recursion until 'request' node (root of tree) is processed *
****************************************************************
@@ -138,7 +134,7 @@
}
/*
- **************************************************************
+ **************************************************************
* Stuff that happens after processing the children of a node *
**************************************************************
*/
@@ -206,8 +202,8 @@
if (objectStack.size() > objStackPosition) {
ArrayList<Object> topObjectOperands = (ArrayList<Object>) objectStack.get(objStackPosition).get("operands");
topObjectOperands.add(0, object);
-
} else {
+ // I want the raw object, not a wrapped
requestMap.put("query", object);
}
}
@@ -234,10 +230,10 @@
}
// Some things went wrong ...
catch (Exception e) {
- System.err.println(e.getMessage());
+ System.err.println("Parsing exception message: " + e.getMessage());
}
if (tree == null) {
- throw new QueryException("Could not parse expert filter query. Make sure it is correct syntax.");
+ throw new QueryException("Could not parse query. Make sure it is correct syntax.");
}
// Return the generated tree
return tree;
@@ -248,7 +244,7 @@
query = "(1990<year<2010&genre=Sport)|textClass=politk";
query = "(textClass=wissenschaft & textClass=politik) | textClass=ausland";
query = "1990<year<2010 & genre=Sport";
- ExpertFilter filter = new ExpertFilter();
+ CollectionQueryTree filter = new CollectionQueryTree();
// filter.verbose = true;
try {
filter.process(query);
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index e3162c1..68173c3 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -177,14 +177,27 @@
return this;
}
- public QuerySerializer setCollection(String collection) {
+ @Deprecated
+ public QuerySerializer setCollectionSimple(String collection) {
CollectionQueryBuilder qobj = new CollectionQueryBuilder();
qobj.addResource(collection);
this.collection = qobj.raw();
return this;
}
- public QuerySerializer setCollection(CollectionQueryBuilder collections) {
+ public QuerySerializer setCollection(String collection) throws QueryException {
+ CollectionQueryTree tree = new CollectionQueryTree();
+ tree.process(collection);
+ this.collection = tree.getRequestMap();
+ return this;
+ }
+
+ public QuerySerializer setCollection(CollectionQueryBuilder2 collections) {
+ this.collection = collections.raw();
+ return this;
+ }
+
+ public QuerySerializer setDeprCollection(CollectionQueryBuilder collections) {
this.collection = collections.raw();
return this;
}
diff --git a/src/test/java/CollectionQueryTreeTest.java b/src/test/java/CollectionQueryTreeTest.java
new file mode 100644
index 0000000..81f6eee
--- /dev/null
+++ b/src/test/java/CollectionQueryTreeTest.java
@@ -0,0 +1,85 @@
+import de.ids_mannheim.korap.query.serialize.CollectionQueryBuilder;
+import de.ids_mannheim.korap.query.serialize.CollectionQueryBuilder2;
+import de.ids_mannheim.korap.query.serialize.CollectionQueryTree;
+import de.ids_mannheim.korap.resource.Relation;
+import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.utils.JsonUtils;
+import de.ids_mannheim.korap.utils.TimeUtils;
+import org.junit.Test;
+
+public class CollectionQueryTreeTest {
+
+ CollectionQueryTree ef;
+ String map;
+ private String query;
+
+ private boolean equalsQueryContent(String res, String query) throws QueryException {
+ res = res.replaceAll(" ", "");
+ ef = new CollectionQueryTree();
+ ef.process(query);
+ String queryMap = ef.getRequestMap().get("query").toString().replaceAll(" ", "");
+ return res.equals(queryMap);
+ }
+
+ @Test
+ public void testSimple() throws QueryException {
+ query = "textClass=Sport";
+ String regex1 = "{@type=korap:filter, filter={@type=korap:term, attribute=textClass, key=Sport, match=match:eq}}";
+ ef = new CollectionQueryTree();
+ ef.process(query);
+ map = JsonUtils.toJSON(ef.getRequestMap());
+// assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+ System.out.println("THE QUERY: " + map);
+ }
+
+
+ @Test
+ public void testComplex() throws QueryException {
+ query = "(textClass=Sport | textClass=ausland) & corpusID=WPD";
+ String regex1 = "{@type=korap:filter, filter={@type=korap:term, attribute=textClass, key=Sport, match=match:eq}}";
+ ef = new CollectionQueryTree();
+ ef.process(query);
+ map = JsonUtils.toJSON(ef.getRequestMap());
+// assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+ System.out.println("THE QUERY 1: " + map);
+ }
+
+ @Test
+ public void testBuilder() throws QueryException {
+ CollectionQueryBuilder2 builder = new CollectionQueryBuilder2();
+ builder.setQuery("(textClass=Sport | textClass=ausland) & corpusID=WPD");
+ System.out.println("BUILDER RESULT: " + builder.toJSON());
+ }
+
+ @Test
+ public void testSimpleBuilder() {
+ CollectionQueryBuilder b = new CollectionQueryBuilder();
+ b.addMetaFilter("corpusID", "WPD");
+ b.addMetaFilter("textClass", "wissenschaft");
+ b.setFilterAttributeRelation(Relation.AND);
+ System.out.println("SIMPLE BUILDER RESULT: " + b.toCollections());
+ }
+
+ // old builder pubDate query
+ @Test
+ public void testDateQuery() {
+ CollectionQueryBuilder b = new CollectionQueryBuilder();
+ String query = "pubDate=>" + TimeUtils.getNow().getMillis();
+ query = query + " AND pubDate=<" + TimeUtils.getNow().getMillis();
+ b.addMetaFilterQuery(query);
+ b.setFilterAttributeRelation(Relation.AND);
+ System.out.println("FINAL RESOURCE: " + b.toCollections());
+ }
+
+ @Test
+ public void testDateNewQuery() throws QueryException {
+ // search for pubDate between 1990 and 2010!
+ String query = "1990<pubDate<2010 & genre=Sport";
+ CollectionQueryBuilder2 q = new CollectionQueryBuilder2();
+ q.setQuery(query);
+ System.out.println("DATE QUERY RESULT: " + q.toJSON());
+ }
+
+
+}
+
diff --git a/src/test/java/ExpertFilterTest.java b/src/test/java/ExpertFilterTest.java
deleted file mode 100644
index 1182f93..0000000
--- a/src/test/java/ExpertFilterTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-import static org.junit.Assert.*;
-
-import org.junit.Test;
-
-import de.ids_mannheim.korap.query.serialize.ExpertFilter;
-import de.ids_mannheim.korap.util.QueryException;
-
-public class ExpertFilterTest {
-
- ExpertFilter ef;
- String map;
- private String query;
-
- private boolean equalsQueryContent(String res, String query) throws QueryException {
- res = res.replaceAll(" ", "");
- ef = new ExpertFilter();
- ef.process(query);
- String queryMap = ef.getRequestMap().get("query").toString().replaceAll(" ", "");
- return res.equals(queryMap);
- }
-
- @Test
- public void testSimple() throws QueryException {
- query = "textClass=Sport";
- String regex1 = "{@type=korap:filter, filter={@type=korap:term, attribute=textClass, key=Sport, match=match:eq}}";
- ef = new ExpertFilter();
- ef.process(query);
- map = ef.getRequestMap().toString();
- assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
- }
-
-
-}
-