Skip to content

Commit

Permalink
Added config for total result cached and fixed #774.
Browse files Browse the repository at this point in the history
Change-Id: I9be13e78c42f60664073b2b693ca90590c99cdf6
  • Loading branch information
margaretha committed Oct 7, 2024
1 parent ea6b8ce commit e6ebfbc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class KustvaktConfiguration {
// EM: metadata restriction
// another variable might be needed to define which metadata fields are restricted
private boolean isMetadataRestricted = false;
private boolean totalResultCacheEnabled;

// EM: Maybe needed when we support pipe registration
@Deprecated
Expand Down Expand Up @@ -220,6 +221,9 @@ protected void load (Properties properties) throws Exception {

// network endpoint
networkEndpointURL = properties.getProperty("network.endpoint.url", "");
// cache
totalResultCacheEnabled = Boolean.valueOf(properties.getProperty(
"cache.total.results.enabled","true"));
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public String search (String engine, String username, HttpHeaders headers,

int hashedKoralQuery = createTotalResultCacheKey(query);
boolean hasCutOff = hasCutOff(query);
if (!hasCutOff) {
if (config.isTotalResultCacheEnabled() && !hasCutOff) {
query = precheckTotalResultCache(hashedKoralQuery, query);
}

Expand All @@ -229,7 +229,10 @@ else if (searchEngine.equals(KustvaktConfiguration.BACKENDS.NETWORK)) {
}
// jlog.debug("Query result: " + result);

result = afterCheckTotalResultCache(hashedKoralQuery, result);
if (config.isTotalResultCacheEnabled()) {
result = afterCheckTotalResultCache(hashedKoralQuery, result);
}

if (!hasCutOff) {
result = removeCutOff(result);
}
Expand All @@ -248,7 +251,7 @@ public int createTotalResultCacheKey (String query)
throws KustvaktException {
ObjectNode queryNode = (ObjectNode) JsonUtils.readTree(query);
queryNode.remove("meta");
return queryNode.hashCode();
return queryNode.toString().hashCode();
}

private String afterCheckTotalResultCache (int hashedKoralQuery,
Expand Down
85 changes: 83 additions & 2 deletions src/test/java/de/ids_mannheim/korap/cache/TotalResultTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package de.ids_mannheim.korap.cache;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import de.ids_mannheim.korap.config.KustvaktConfiguration;
import de.ids_mannheim.korap.config.SpringJerseyTest;
import de.ids_mannheim.korap.core.service.SearchService;
import de.ids_mannheim.korap.exceptions.KustvaktException;
Expand All @@ -17,6 +21,9 @@ public class TotalResultTest extends SpringJerseyTest {

@Autowired
private SearchService searchService;

@Autowired
private KustvaktConfiguration config;

@Test
public void testClearCache () {
Expand Down Expand Up @@ -54,8 +61,7 @@ public void testSearchWithPaging () throws KustvaktException {
assertEquals(Status.OK.getStatusCode(), response.getStatus());
entity = response.readEntity(String.class);
node = JsonUtils.readTree(entity);
assertTrue(node.at("/meta/totalResults").isNumber(),
"totalResults should be a number");
assertTrue(node.at("/meta/totalResults").isNumber());
assertEquals(totalResults, node.at("/meta/totalResults").asInt());
assertEquals(1, searchService.getTotalResultCache()
.getAllCacheElements().size());
Expand Down Expand Up @@ -108,4 +114,79 @@ private void testSearchWithCutOff () throws KustvaktException {
node = JsonUtils.readTree(entity);
assertTrue(node.at("/meta/cutOff").asBoolean());
}

@Test
public void testCacheDisabled () throws KustvaktException {
searchService.getTotalResultCache().clearCache();
assertEquals(0, searchService.getTotalResultCache()
.getAllCacheElements().size());

config.setTotalResultCacheEnabled(false);

Response response = target().path(API_VERSION).path("search")
.queryParam("q", "[orth=zu]").queryParam("ql", "poliqarp")
.queryParam("page", "1").request().get();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
String entity = response.readEntity(String.class);
JsonNode node = JsonUtils.readTree(entity);
assertTrue(node.at("/meta/totalResults").isNumber(),
"totalResults should be a number");
assertEquals(0, searchService.getTotalResultCache()
.getAllCacheElements().size());

config.setTotalResultCacheEnabled(true);

response = target().path(API_VERSION).path("search")
.queryParam("q", "[orth=zu]").queryParam("ql", "poliqarp")
.queryParam("page", "1").request().get();
assertEquals(Status.OK.getStatusCode(), response.getStatus());

assertEquals(1, searchService.getTotalResultCache()
.getAllCacheElements().size());

searchService.getTotalResultCache().clearCache();
}

@Test
public void testCacheKey () throws KustvaktException {
Response response = target().path(API_VERSION).path("search")
.queryParam("q", "[orth=populistischer]")
.queryParam("ql", "poliqarp")
.queryParam("cq", "availability!=QAO-NC-LOC:ids & corpusSigle = "
+ "/SOL|[UTSZ][0-9][0-9]/ & pubDate in 1976")
//.queryParam("fields", "corpusSigle,textSigle,pubDate,pubPlace,"
// + "availability,textClass")
.queryParam("access-rewrite-disabled", "true")
.queryParam("page", "1").request().get();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
String entity = response.readEntity(String.class);

ObjectNode queryNode = (ObjectNode) JsonUtils.readTree(entity);
queryNode.remove("meta");
queryNode.remove("matches");
int queryHashCode1 = queryNode.hashCode();
int queryStringHashCode1 = queryNode.toString().hashCode();

response = target().path(API_VERSION).path("search")
.queryParam("q", "[orth=populistisches]")
.queryParam("ql", "poliqarp")
.queryParam("cq", "availability!=QAO-NC-LOC:ids & corpusSigle = "
+ "/SOL|[UTSZ][0-9][0-9]/ & pubDate in 1975")
//.queryParam("fields", "corpusSigle,textSigle,pubDate,pubPlace,"
// + "availability,textClass")
.queryParam("access-rewrite-disabled", "true")
.queryParam("page", "1").request().get();
assertEquals(Status.OK.getStatusCode(), response.getStatus());

entity = response.readEntity(String.class);

queryNode = (ObjectNode) JsonUtils.readTree(entity);
queryNode.remove("meta");
queryNode.remove("matches");
int queryHashCode2 = queryNode.hashCode();
int queryStringHashCode2 = queryNode.toString().hashCode();

assertEquals(queryHashCode1, queryHashCode2);
assertNotEquals(queryStringHashCode1, queryStringHashCode2);
}
}
3 changes: 3 additions & 0 deletions src/test/resources/kustvakt-test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ supported.api.versions = v0.1, v1.0
server.port=8089
server.host=localhost

## Cache
cache.total.results.enabled = true

# Default foundries for specific layers (optional)
#
default.foundry.partOfSpeech = tt
Expand Down

0 comments on commit e6ebfbc

Please sign in to comment.