More testings for cutoff and returning -1 in case of a cutoff
diff --git a/src/main/java/de/ids_mannheim/korap/KorapIndex.java b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
index 9359848..a2a5c7f 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
@@ -666,7 +666,7 @@
match.setAuthor(doc.get("author"));
match.setTextClass(doc.get("textClass"));
- match.setID(doc.get("ID"));
+ match.setDocID(doc.get("ID"));
match.setTitle(doc.get("title"));
match.setSubTitle(doc.get("subTitle"));
match.setPubPlace(doc.get("pubPlace"));
@@ -682,10 +682,9 @@
match.setLayerInfo(doc.get("layerInfo"));
match.setPrimaryData(
- new KorapPrimaryData(doc.get(foundry))
+ new KorapPrimaryData(doc.get(foundry))
);
atomicMatches.add(match);
- // kr.add(match);
};
// Benchmark till now
@@ -709,7 +708,8 @@
if (kr.getBenchmarkSearchResults().length() == 0) {
kr.setBenchmarkSearchResults(t2, t1);
};
- kr.setTotalResults(i);
+
+ kr.setTotalResults(cutoff ? -1 : i);
}
catch (IOException e) {
kr.setError("There was an IO error");
@@ -718,38 +718,4 @@
return kr;
};
-
-
- /*
-
- public void getFoundryStatistics {
-- provides statistical information:
- - which documents have which foundries
- - Collect all Bitvectors of each foundry and make the intersections afterwards
- - ???
- };
-
-
- public KorapResult search (Bits bits, KorapQuery query) {
- // this.search(bits, query);
- };
-
- // countAllTokens
- public int getNumberOfTokens (String corpus) throws IOException {
- return this.getNumberOf("token", "base", corpus);
- };
-
-
- // retrieveTokens(docname, startOffset, endOffset, layer);
-
-
- /*
-
-
- // todo mit pagesize und offset
-
-
- };
-*/
-
};
diff --git a/src/main/java/de/ids_mannheim/korap/KorapMatch.java b/src/main/java/de/ids_mannheim/korap/KorapMatch.java
index 38e32d2..b87a076 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapMatch.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapMatch.java
@@ -120,6 +120,10 @@
return super.getID();
};
+ public void setDocID (String id) {
+ super.setID(id);
+ };
+
@Override
@JsonProperty("ID")
public String getID () {
diff --git a/src/main/java/de/ids_mannheim/korap/KorapSearch.java b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
index d7401ed..8733a38 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapSearch.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
@@ -16,32 +16,34 @@
/*
Todo: Let this class extend KorapResult!
- Todo: implement an empty new Result Thingy!
KorapResult = new KorapSearch(String json).run(KorapIndex ki);
- startPage!!!
*/
+/**
+ * @author Nils Diewald
+ *
+ * KoraSearch implements an object for all search relevant parameters.
+ */
public class KorapSearch {
- private int startIndex;
- private short count = 25;
- private short countMax = 50;
- private int limit = 0;
+ private int startIndex = 0, limit = 0;
+ private short count = 25,
+ countMax = 50;
private boolean cutoff = false;
private SpanQuery query;
- public KorapSearchContext leftContext, rightContext;
private KorapCollection collection;
private KorapIndex index;
private String error;
+ public KorapSearchContext leftContext, rightContext;
+
{
- leftContext = new KorapSearchContext();
+ leftContext = new KorapSearchContext();
rightContext = new KorapSearchContext();
};
public class KorapSearchContext {
private boolean type = true;
- private short length = 6;
- private short maxLength = 300;
+ private short length = 6, maxLength = 300;
public boolean isToken () {
return this.type;
@@ -72,7 +74,7 @@
}
else {
this.length = this.maxLength;
- }
+ };
};
return this;
};
@@ -99,6 +101,7 @@
try {
JsonNode json = mapper.readValue(jsonString, JsonNode.class);
+ // "query" value
if (json.has("query")) {
try {
this.query = new KorapQuery("tokens").fromJSON(json.get("query")).toQuery();
@@ -111,10 +114,9 @@
this.error = "No query defined";
};
- if (json.has("meta")) {
- KorapCollection kc = new KorapCollection(jsonString);
- this.setCollection(kc);
- };
+ // "meta" virtual collections
+ if (json.has("meta"))
+ this.setCollection(new KorapCollection(jsonString));
if (this.error == null) {
@@ -139,12 +141,14 @@
JsonNode context = json.get("context");
if (context.has("left"))
this.leftContext.fromJSON(context.get("left"));
+
if (context.has("right"))
this.rightContext.fromJSON(context.get("right"));
-
};
};
}
+
+ // Unable to parse JSON
catch (IOException e) {
this.error = e.getMessage();
};
@@ -160,6 +164,7 @@
this.query = sq;
};
+ // Empty constructor
public KorapSearch () { };
public SpanQuery getQuery () {
@@ -237,12 +242,10 @@
public KorapSearch setCount (short value) {
if (value > 0) {
- if (value <= this.countMax) {
+ if (value <= this.countMax)
this.count = value;
- }
- else {
+ else
this.count = this.countMax;
- };
};
return this;
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
index 370be81..0fd90e8 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
@@ -33,8 +33,7 @@
/** Matches spans which are directly next to each other.
*/
public class SpanNextQuery extends SpanQuery implements Cloneable {
- private SpanQuery firstClause;
- private SpanQuery secondClause;
+ private SpanQuery firstClause, secondClause;
public String field;
private boolean collectPayloads;
@@ -42,8 +41,7 @@
private final static Logger log = LoggerFactory.getLogger(SpanNextQuery.class);
// Constructor
- public SpanNextQuery(SpanQuery firstClause,
- SpanQuery secondClause) {
+ public SpanNextQuery(SpanQuery firstClause, SpanQuery secondClause) {
this(firstClause, secondClause, true);
};
@@ -54,7 +52,7 @@
this.field = secondClause.getField();
if (!firstClause.getField().equals(field)) {
- throw new IllegalArgumentException("Clauses must have same field.");
+ throw new IllegalArgumentException("Clauses must have same field");
};
this.collectPayloads = collectPayloads;
@@ -79,14 +77,14 @@
@Override
public String toString(String field) {
- StringBuilder buffer = new StringBuilder();
- buffer.append("spanNext(");
- buffer.append(firstClause.toString(field));
- buffer.append(", ");
- buffer.append(secondClause.toString(field));
- buffer.append(")");
- buffer.append(ToStringUtils.boost(getBoost()));
- return buffer.toString();
+ StringBuilder sb = new StringBuilder();
+ sb.append("spanNext(")
+ .append(firstClause.toString(field))
+ .append(", ")
+ .append(secondClause.toString(field))
+ .append(")")
+ .append(ToStringUtils.boost(getBoost()));
+ return sb.toString();
};
@Override
@@ -96,17 +94,14 @@
log.trace("Get Spans");
return (Spans) new NextSpans (
- this, context, acceptDocs, termContexts, collectPayloads
+ this, context, acceptDocs, termContexts, collectPayloads
);
};
@Override
public Query rewrite (IndexReader reader) throws IOException {
- // System.err.println(">> Rewrite query");
-
SpanNextQuery clone = null;
- // System.err.println(">> Rewrite first clause");
SpanQuery query = (SpanQuery) firstClause.rewrite(reader);
if (query != firstClause) {
@@ -115,7 +110,6 @@
clone.firstClause = query;
};
- // System.err.println(">> Rewrite second clause");
query = (SpanQuery) secondClause.rewrite(reader);
if (query != secondClause) {
if (clone == null)
@@ -123,10 +117,8 @@
clone.secondClause = query;
};
- if (clone != null) {
- // System.err.println(">> Clone is not null");
+ if (clone != null)
return clone;
- };
return this;
};
@@ -153,7 +145,7 @@
final SpanNextQuery spanNextQuery = (SpanNextQuery) o;
if (collectPayloads != spanNextQuery.collectPayloads) return false;
- if (!firstClause.equals(spanNextQuery.firstClause)) return false;
+ if (!firstClause.equals(spanNextQuery.firstClause)) return false;
if (!secondClause.equals(spanNextQuery.secondClause)) return false;
return getBoost() == spanNextQuery.getBoost();
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
index f70ef99..9ce6090 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
@@ -213,6 +213,36 @@
assertEquals(5, kr.getItemsPerPage());
};
+ @Test
+ public void searchJSONstartPage () throws IOException {
+
+ // Construct index
+ KorapIndex ki = new KorapIndex();
+ // Indexing test files
+ for (String i : new String[] {"00001", "00002", "00003", "00004", "00005", "00006", "02439"}) {
+ ki.addDocFile(
+ getClass().getResource("/wiki/" + i + ".json.gz").getFile(), true
+ );
+ };
+ ki.commit();
+
+ String json = getString(getClass().getResource("/queries/bsp-paging.json").getFile());
+
+ KorapSearch ks = new KorapSearch(json);
+ KorapResult kr = ks.run(ki);
+ assertEquals(10, kr.getTotalResults());
+ assertEquals(5, kr.getStartIndex());
+ assertEquals(5, kr.getItemsPerPage());
+
+ json = getString(getClass().getResource("/queries/bsp-cutoff.json").getFile());
+ ks = ks = new KorapSearch(json);
+
+ kr = ks.run(ki);
+ assertEquals(-1, kr.getTotalResults());
+ assertEquals(2, kr.getStartIndex());
+ assertEquals(2, kr.getItemsPerPage());
+
+ };
public static String getString (String path) {
diff --git a/src/test/resources/queries/bsp-cutoff.json b/src/test/resources/queries/bsp-cutoff.json
new file mode 100644
index 0000000..0f1f834
--- /dev/null
+++ b/src/test/resources/queries/bsp-cutoff.json
@@ -0,0 +1,36 @@
+{
+ "@context": {
+ "@language": "de",
+ "class": {
+ "@id": "korap:class",
+ "@type": "xsd:integer"
+ },
+ "filter": "korap:filter",
+ "korap": "http://korap.ids-mannheim.de/ns/query",
+ "meta": "korap:meta",
+ "operands": {
+ "@container": "@list",
+ "@id": "korap:operands"
+ },
+ "query": "korap:query",
+ "relation": {
+ "@id": "korap:relation",
+ "@type": "korap:relation#types"
+ }
+ },
+ "query": {
+ "@type": "korap:token",
+ "@value": {
+ "@type": "korap:term",
+ "@value": "base:alphabet",
+ "relation": "="
+ }
+ },
+ "startPage":2,
+ "count": 2,
+ "cutOff": true,
+ "context":{
+ "left":["char",90],
+ "right":["char",90]
+ }
+}
diff --git a/src/test/resources/queries/bsp-paging.json b/src/test/resources/queries/bsp-paging.json
new file mode 100644
index 0000000..ac352a7
--- /dev/null
+++ b/src/test/resources/queries/bsp-paging.json
@@ -0,0 +1,35 @@
+{
+ "@context": {
+ "@language": "de",
+ "class": {
+ "@id": "korap:class",
+ "@type": "xsd:integer"
+ },
+ "filter": "korap:filter",
+ "korap": "http://korap.ids-mannheim.de/ns/query",
+ "meta": "korap:meta",
+ "operands": {
+ "@container": "@list",
+ "@id": "korap:operands"
+ },
+ "query": "korap:query",
+ "relation": {
+ "@id": "korap:relation",
+ "@type": "korap:relation#types"
+ }
+ },
+ "query": {
+ "@type": "korap:token",
+ "@value": {
+ "@type": "korap:term",
+ "@value": "base:alphabet",
+ "relation": "="
+ }
+ },
+ "startPage":2,
+ "count": 5,
+ "context":{
+ "left":["char",90],
+ "right":["char",90]
+ }
+}