From 49d1b033d715a31d2a3a917c370c4c3935cf7bbc Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 27 Jun 2018 14:01:36 +0200 Subject: [PATCH] Support top-level type restriction in OpenRefine reconciliation See https://github.com/hbz/lobid-gnd/issues/65 --- app/controllers/Reconcile.java | 10 +++++++--- app/modules/IndexComponent.java | 2 +- test/controllers/ReconcileTest.java | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/controllers/Reconcile.java b/app/controllers/Reconcile.java index 9a2b57a..0fc6e52 100644 --- a/app/controllers/Reconcile.java +++ b/app/controllers/Reconcile.java @@ -16,7 +16,9 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableMap; +import models.GndOntology; import modules.IndexComponent; import play.Logger; import play.libs.Json; @@ -39,7 +41,8 @@ public class Reconcile extends Controller { @Inject IndexComponent index; - private static final JsonNode TYPES = Json.toJson(Arrays.asList("lobid-gnd")); + private static final JsonNode TYPES = Json.toJson(HomeController.CONFIG.getStringList("topLevelTypes").stream() + .map(t -> ImmutableMap.of("id", t, "name", GndOntology.label(t)))); /** * @param callback @@ -95,8 +98,9 @@ private List mapToResults(String mainQuery, SearchHits searchHits) { private SearchResponse executeQuery(Entry entry, String queryString) { JsonNode limitNode = entry.getValue().get("limit"); int limit = limitNode == null ? -1 : limitNode.asInt(); - SearchResponse response = index.query(queryString, "", 0, limit); - return response; + JsonNode typeNode = entry.getValue().get("type"); + String filter = typeNode == null ? "" : "type:" + typeNode.asText(); + return index.query(queryString, filter, 0, limit); } private String buildQueryString(Entry entry) { diff --git a/app/modules/IndexComponent.java b/app/modules/IndexComponent.java index d6499a7..65bf97a 100644 --- a/app/modules/IndexComponent.java +++ b/app/modules/IndexComponent.java @@ -54,7 +54,7 @@ public interface IndexComponent { Client client(); - SearchResponse query(String q, String type, int from, int size); + SearchResponse query(String q, String filter, int from, int size); public default SearchResponse query(String q) { return query(q, "", 0, 10); diff --git a/test/controllers/ReconcileTest.java b/test/controllers/ReconcileTest.java index 88dbfef..d83940e 100644 --- a/test/controllers/ReconcileTest.java +++ b/test/controllers/ReconcileTest.java @@ -6,6 +6,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static play.test.Helpers.GET; @@ -66,4 +67,17 @@ public void reconcileRequest() { }); } + @Test + public void reconcileRequestWithType() { + Application application = fakeApplication(); + running(application, () -> { + Result result = route(application, fakeRequest(POST, "/gnd/reconcile").bodyForm( + ImmutableMap.of("queries", "{\"q99\":{\"query\":\"Twain, Mark\", \"type\":\"CorporateBody\"}}"))); + String content = contentAsString(result); + Logger.debug(Json.prettyPrint(Json.parse(content))); + assertThat(content, containsString("q99")); + assertFalse(Json.parse(content).findValue("result").elements().hasNext()); + }); + } + } \ No newline at end of file