13
13
import java .text .Collator ;
14
14
import java .util .ArrayList ;
15
15
import java .util .Arrays ;
16
+ import java .util .Collection ;
16
17
import java .util .Collections ;
17
18
import java .util .Comparator ;
18
19
import java .util .HashMap ;
42
43
import org .elasticsearch .action .search .SearchResponse ;
43
44
import org .elasticsearch .action .search .SearchType ;
44
45
import org .elasticsearch .client .Client ;
45
- import org .elasticsearch .common .settings .ImmutableSettings ;
46
46
import org .elasticsearch .common .settings .Settings ;
47
+ import org .elasticsearch .common .xcontent .XContentType ;
47
48
import org .elasticsearch .index .query .MatchAllQueryBuilder ;
48
49
import org .elasticsearch .index .query .QueryBuilder ;
49
50
import org .elasticsearch .index .query .QueryBuilders ;
51
+ import org .elasticsearch .node .InternalSettingsPreparer ;
50
52
import org .elasticsearch .node .Node ;
51
- import org .elasticsearch .node .NodeBuilder ;
53
+ import org .elasticsearch .node .NodeValidationException ;
54
+ import org .elasticsearch .plugins .Plugin ;
52
55
import org .elasticsearch .search .SearchHit ;
56
+ import org .elasticsearch .transport .Netty4Plugin ;
53
57
54
58
import com .fasterxml .jackson .databind .JsonNode ;
55
59
import com .github .jsonldjava .core .JsonLdError ;
@@ -194,6 +198,12 @@ public static Comparator<JsonNode> comparator(Function<JsonNode, String> fun) {
194
198
.compare (fun .apply (o1 ), fun .apply (o2 ));
195
199
}
196
200
201
+ private static class EmbeddedNode extends Node {
202
+ public EmbeddedNode (Settings preparedSettings , Collection <Class <? extends Plugin >> classpathPlugins ) {
203
+ super (InternalSettingsPreparer .prepareEnvironment (preparedSettings , null ), classpathPlugins );
204
+ }
205
+ }
206
+
197
207
private Classification () {
198
208
/* Use via static functions, no instantiation. */
199
209
}
@@ -237,7 +247,7 @@ public static List<String> toJsonLd(final URL turtleUrl) {
237
247
public static JsonNode ids (String q , String t ) {
238
248
QueryBuilder queryBuilder = QueryBuilders .boolQuery ()
239
249
.must (QueryBuilders .idsQuery (Type .NWBIB .elasticsearchType ,
240
- Type .SPATIAL .elasticsearchType ).ids (q ));
250
+ Type .SPATIAL .elasticsearchType ).addIds (q ));
241
251
SearchRequestBuilder requestBuilder = client .prepareSearch (INDEX )
242
252
.setSearchType (SearchType .DFS_QUERY_THEN_FETCH ).setQuery (queryBuilder );
243
253
if (t .isEmpty ()) {
@@ -473,16 +483,21 @@ public static String shortId(String uri) {
473
483
474
484
/** Start up the embedded Elasticsearch classification index. */
475
485
public static void indexStartup () {
476
- Settings clientSettings = ImmutableSettings .settingsBuilder ()
477
- .put ("path.home" , new File ("." ).getAbsolutePath ())
478
- .put ("http.port" ,
479
- play .Play .application ().isTest () ? "8855"
480
- : CONFIG .getString ("index.es.port.http" ))
481
- .put ("transport.tcp.port" , play .Play .application ().isTest () ? "8856"
482
- : CONFIG .getString ("index.es.port.tcp" ))
483
- .build ();
484
- node =
485
- NodeBuilder .nodeBuilder ().settings (clientSettings ).local (true ).node ();
486
+ boolean isTest = play .Play .application ().isTest ();
487
+ String httpPort = isTest ? "8855" : CONFIG .getString ("index.es.port.http" );
488
+ String tcpPort = isTest ? "8856" : CONFIG .getString ("index.es.port.tcp" );
489
+ node = new EmbeddedNode (Settings .builder ().put ("transport.type" , "netty4" )//
490
+ .put ("http.type" , "netty4" )//
491
+ .put ("http.enabled" , "true" )//
492
+ .put ("path.home" , new File ("." ).getAbsolutePath ())//
493
+ .put ("http.port" , httpPort )//
494
+ .put ("transport.tcp.port" , tcpPort )//
495
+ .build (), Arrays .asList (Netty4Plugin .class ));
496
+ try {
497
+ node .start ();
498
+ } catch (NodeValidationException e ) {
499
+ e .printStackTrace ();
500
+ }
486
501
client = node .client ();
487
502
client .admin ().cluster ().prepareHealth ().setWaitForYellowStatus ().execute ()
488
503
.actionGet ();
@@ -502,8 +517,8 @@ private static void indexData(String dataUrl, Type type) {
502
517
List <String > jsonLd = toJsonLd (new URL (dataUrl ));
503
518
for (String concept : jsonLd ) {
504
519
String id = Json .parse (concept ).findValue ("@id" ).textValue ();
505
- IndexRequestBuilder indexRequest = client
506
- .prepareIndex ( INDEX , type . elasticsearchType , id ). setSource ( concept );
520
+ IndexRequestBuilder indexRequest = client . prepareIndex ( INDEX , type . elasticsearchType , id )
521
+ .setSource ( concept . getBytes (), XContentType . JSON );
507
522
bulkRequest .add (indexRequest );
508
523
}
509
524
} catch (MalformedURLException e ) {
@@ -517,7 +532,11 @@ private static void indexData(String dataUrl, Type type) {
517
532
518
533
/** Shut down the embedded Elasticsearch classification index. */
519
534
public static void indexShutdown () {
520
- node .close ();
535
+ try {
536
+ node .close ();
537
+ } catch (IOException e ) {
538
+ e .printStackTrace ();
539
+ }
521
540
}
522
541
523
542
/**
0 commit comments