Skip to content

Commit

Permalink
Set up index tests and custom query method in IndexComponent
Browse files Browse the repository at this point in the history
See #20
  • Loading branch information
fsteeg committed Aug 23, 2017
1 parent e27b2a8 commit 943a9b2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 12 deletions.
10 changes: 2 additions & 8 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

import org.apache.jena.atlas.web.HttpException;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;

Expand Down Expand Up @@ -51,7 +48,7 @@
*/
public class HomeController extends Controller {

private static final String TYPE = "type";
public static final String TYPE = "type";

@Inject
Environment env;
Expand Down Expand Up @@ -146,10 +143,7 @@ public Result gnd(String id) {
}

public Result search(String q, int from, int size, String format) {
SearchRequestBuilder requestBuilder = index.client().prepareSearch(config("index.name"))
.setQuery(QueryBuilders.queryStringQuery(q)).setFrom(from).setSize(size);
requestBuilder.addAggregation(AggregationBuilders.terms(TYPE).field(TYPE + ".raw").size(1000));
SearchResponse response = requestBuilder.get();
SearchResponse response = index.query(q, from, size);
response().setHeader("Access-Control-Allow-Origin", "*");
return format.equals("html") ? htmlSearch(q, from, size, format, response)
: ok(returnAsJson(q, response)).as(config("index.content"));
Expand Down
31 changes: 30 additions & 1 deletion app/modules/IndexComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,34 @@
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.aggregations.AggregationBuilders;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import controllers.HomeController;
import play.Logger;
import play.inject.ApplicationLifecycle;

public interface IndexComponent {
Client client();

SearchResponse query(String q, int from, int size);

public default SearchResponse query(String q) {
return query(q, 0, 10);
}
}

class EmbeddedIndex implements IndexComponent {
Expand Down Expand Up @@ -86,7 +100,8 @@ private void startup() {
indexData(client, pathToJson, indexName);
}
} else {
Logger.info("Index exists. Delete the 'data/' directory to reindexfrom " + pathToJson);
Logger.info("Index exists. Delete the '" + config("index.home") + "/data' directory to reindex from "
+ pathToJson);
}
if (new File(pathToUpdates).exists()) {
Logger.info("Indexing updates from " + pathToUpdates);
Expand Down Expand Up @@ -178,4 +193,18 @@ private static void executeBulk(int pendingIndexRequests) {
}
Logger.info("Indexed {} docs, took: {}", pendingIndexRequests, bulkResponse.getTook());
}

@Override
public SearchResponse query(String q, int from, int size) {
MatchQueryBuilder preferredName = QueryBuilders.matchQuery("preferredName", q).boost(2);
QueryStringQueryBuilder queryStringQuery = QueryBuilders.queryStringQuery(q);
QueryBuilder query = QueryBuilders.boolQuery().should(preferredName).must(queryStringQuery)
.minimumNumberShouldMatch(0);
SearchRequestBuilder requestBuilder = client().prepareSearch(config("index.name")).setQuery(query).setFrom(from)
.setSize(size);
requestBuilder.addAggregation(
AggregationBuilders.terms(HomeController.TYPE).field(HomeController.TYPE + ".raw").size(1000));
SearchResponse response = requestBuilder.get();
return response;
}
}
45 changes: 42 additions & 3 deletions test/modules/IndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.io.FileWriter;
import java.io.IOException;

import org.elasticsearch.action.search.SearchResponse;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -15,26 +17,33 @@
import com.hp.hpl.jena.rdf.model.ModelFactory;

import apps.Convert;
import models.AuthorityResource;
import play.Application;
import play.api.inject.BindingKey;
import play.api.inject.DefaultApplicationLifecycle;
import play.inject.guice.GuiceApplicationBuilder;
import play.libs.Json;
import play.test.WithApplication;

public class IndexTest extends WithApplication {

private static final File[] TEST_FILES = new File("test/ttl").listFiles();
private static final String PATH = "GND.jsonl";

private IndexComponent index;

@Override
protected Application provideApplication() {
// See
// https://www.playframework.com/documentation/2.6.1/JavaDependencyInjection
// https://www.playframework.com/documentation/2.6.x/JavaTestingWithGuice
return new GuiceApplicationBuilder().build();
}

@BeforeClass
public static void convert() throws IOException {
try (FileWriter out = new FileWriter(PATH)) {
for (File file : new File("test/ttl").listFiles()) {
for (File file : TEST_FILES) {
Model sourceModel = ModelFactory.createDefaultModel();
sourceModel.read(new FileReader(file), null, "TTL");
String id = file.getName().split("\\.")[0];
Expand All @@ -50,14 +59,44 @@ public static void convert() throws IOException {
@AfterClass
public static void cleanup() throws IOException {
// TODO: use separate dir, override config in provideApplication
// https://www.playframework.com/documentation/2.6.x/JavaTestingWithGuice#configuration
// FileUtils.deleteDirectory(new File("data"));
}

@Before
public void setup() {
app.injector().instanceOf(DefaultApplicationLifecycle.class).stop();
index = app.injector().instanceOf(new BindingKey<>(IndexComponent.class));
}

@Test
public void test() {
public void testIndexData() {
Assert.assertTrue("Index data file should exist", new File(PATH).exists());
System.out.println("Indexed from: " + PATH);
// TODO: inject index, check number of docs
}

@Test
public void testTotalHits() {
Assert.assertEquals(TEST_FILES.length, index.query("*").getHits().getTotalHits());
}

@Test
public void testFieldQuery() {
Assert.assertEquals(1, index.query("preferredName:\"Weizenbaum, Joseph\"").getHits().getTotalHits());
Assert.assertEquals(0, index.query("id:\"Weizenbaum, Joseph\"").getHits().getTotalHits());
}

@Test
public void testContextQuery() {
Assert.assertEquals(0, index.query("jsonld").getHits().getTotalHits());
}

@Test
public void testPerfectFieldMatch() {
SearchResponse response = index.query("london");
Assert.assertEquals(2, response.getHits().getTotalHits());
Assert.assertEquals("London", Json.fromJson(Json.parse(response.getHits().getHits()[0].getSourceAsString()),
AuthorityResource.class).preferredName.get(0));
}

}
37 changes: 37 additions & 0 deletions test/ttl/1045328480.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@prefix schema: <http://schema.org/> .
@prefix gndo: <http://d-nb.info/standards/elementset/gnd#> .
@prefix lib: <http://purl.org/library/> .
@prefix marcRole: <http://id.loc.gov/vocabulary/relators/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix dcmitype: <http://purl.org/dc/dcmitype/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix umbel: <http://umbel.org/umbel#> .
@prefix dbp: <http://dbpedia.org/property/> .
@prefix dnbt: <http://d-nb.info/standards/elementset/dnb#> .
@prefix rdau: <http://rdaregistry.info/Elements/u/> .
@prefix sf: <http://www.opengis.net/ont/sf#> .
@prefix dnb_intern: <http://dnb.de/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix bibo: <http://purl.org/ontology/bibo/> .
@prefix gbv: <http://purl.org/ontology/gbv/> .
@prefix isbd: <http://iflastandards.info/ns/isbd/elements/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://d-nb.info/gnd/1045328480> a gndo:BuildingOrMemorial ;
foaf:page <http://de.wikipedia.org/wiki/The_Shard> ;
gndo:gndIdentifier "1045328480" ;
gndo:architect <http://d-nb.info/gnd/11889787X> , <http://d-nb.info/gnd/5071828-9> ;
gndo:broaderTermInstantial <http://d-nb.info/gnd/4025210-3> ;
gndo:gndSubjectCategory <http://d-nb.info/standards/vocab/gnd/gnd-sc#31.3ab> ;
gndo:geographicAreaCode <http://d-nb.info/standards/vocab/gnd/geographic-area-code#XA-GB> ;
gndo:definition "72-stöckiges u. 310 m hohes, multifunktionales Hochhaus am Südufer d. Themse i.d. Nähe d. London Bridge in London-Southwark"@de ;
gndo:dateOfProduction "16.03.2009-01.02.2013" ;
gndo:variantNameForThePlaceOrGeographicName "London Bridge Tower (London)" , "Shard London Bridge (London)" , "Shard of Glass (London)" , "32 London Bridge (London)" , "Thirty-two London Bridge (London)" ;
gndo:preferredNameForThePlaceOrGeographicName "The Shard (London)" ;
gndo:place <http://d-nb.info/gnd/4074335-4> .

0 comments on commit 943a9b2

Please sign in to comment.