11
11
import org .apache .http .entity .ContentType ;
12
12
import org .apache .http .entity .StringEntity ;
13
13
import org .apache .http .util .EntityUtils ;
14
+ import org .elasticsearch .client .Request ;
14
15
import org .elasticsearch .client .Response ;
15
16
import org .elasticsearch .client .RestClient ;
16
17
import org .elasticsearch .common .Strings ;
@@ -37,26 +38,28 @@ public class GeoDataLoader {
37
38
38
39
public static void main (String [] args ) throws Exception {
39
40
try (RestClient client = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ()) {
40
- loadDatasetIntoEs (client );
41
+ loadOGCDatasetIntoEs (client , "ogc" );
42
+ loadGeoDatasetIntoEs (client , "geo" );
41
43
Loggers .getLogger (GeoDataLoader .class ).info ("Geo data loaded" );
42
44
}
43
45
}
44
46
45
- protected static void loadDatasetIntoEs (RestClient client ) throws Exception {
46
- loadDatasetIntoEs (client , "ogc" );
47
- makeFilteredAlias (client , "lakes" , "ogc" , "\" term\" : { \" ogc_type\" : \" lakes\" }" );
48
- makeFilteredAlias (client , "road_segments" , "ogc" , "\" term\" : { \" ogc_type\" : \" road_segments\" }" );
49
- makeFilteredAlias (client , "divided_routes" , "ogc" , "\" term\" : { \" ogc_type\" : \" divided_routes\" }" );
50
- makeFilteredAlias (client , "forests" , "ogc" , "\" term\" : { \" ogc_type\" : \" forests\" }" );
51
- makeFilteredAlias (client , "bridges" , "ogc" , "\" term\" : { \" ogc_type\" : \" bridges\" }" );
52
- makeFilteredAlias (client , "streams" , "ogc" , "\" term\" : { \" ogc_type\" : \" streams\" }" );
53
- makeFilteredAlias (client , "buildings" , "ogc" , "\" term\" : { \" ogc_type\" : \" buildings\" }" );
54
- makeFilteredAlias (client , "ponds" , "ogc" , "\" term\" : { \" ogc_type\" : \" ponds\" }" );
55
- makeFilteredAlias (client , "named_places" , "ogc" , "\" term\" : { \" ogc_type\" : \" named_places\" }" );
56
- makeFilteredAlias (client , "map_neatlines" , "ogc" , "\" term\" : { \" ogc_type\" : \" map_neatlines\" }" );
47
+ protected static void loadOGCDatasetIntoEs (RestClient client , String index ) throws Exception {
48
+ createIndex (client , index , createOGCIndexRequest ());
49
+ loadData (client , index , readResource ("/ogc/ogc.json" ));
50
+ makeFilteredAlias (client , "lakes" , index , "\" term\" : { \" ogc_type\" : \" lakes\" }" );
51
+ makeFilteredAlias (client , "road_segments" , index , "\" term\" : { \" ogc_type\" : \" road_segments\" }" );
52
+ makeFilteredAlias (client , "divided_routes" , index , "\" term\" : { \" ogc_type\" : \" divided_routes\" }" );
53
+ makeFilteredAlias (client , "forests" , index , "\" term\" : { \" ogc_type\" : \" forests\" }" );
54
+ makeFilteredAlias (client , "bridges" , index , "\" term\" : { \" ogc_type\" : \" bridges\" }" );
55
+ makeFilteredAlias (client , "streams" , index , "\" term\" : { \" ogc_type\" : \" streams\" }" );
56
+ makeFilteredAlias (client , "buildings" , index , "\" term\" : { \" ogc_type\" : \" buildings\" }" );
57
+ makeFilteredAlias (client , "ponds" , index , "\" term\" : { \" ogc_type\" : \" ponds\" }" );
58
+ makeFilteredAlias (client , "named_places" , index , "\" term\" : { \" ogc_type\" : \" named_places\" }" );
59
+ makeFilteredAlias (client , "map_neatlines" , index , "\" term\" : { \" ogc_type\" : \" map_neatlines\" }" );
57
60
}
58
61
59
- protected static void loadDatasetIntoEs ( RestClient client , String index ) throws Exception {
62
+ private static String createOGCIndexRequest ( ) throws Exception {
60
63
XContentBuilder createIndex = JsonXContent .contentBuilder ().startObject ();
61
64
createIndex .startObject ("settings" );
62
65
{
@@ -101,12 +104,21 @@ protected static void loadDatasetIntoEs(RestClient client, String index) throws
101
104
createIndex .endObject ();
102
105
}
103
106
createIndex .endObject ().endObject ();
107
+ return Strings .toString (createIndex );
108
+ }
104
109
105
- client .performRequest ("PUT" , "/" + index , emptyMap (), new StringEntity (Strings .toString (createIndex ),
106
- ContentType .APPLICATION_JSON ));
110
+ private static void createIndex (RestClient client , String index , String settingsMappings ) throws IOException {
111
+ Request createIndexRequest = new Request ("PUT" , "/" + index );
112
+ createIndexRequest .setEntity (new StringEntity (settingsMappings , ContentType .APPLICATION_JSON ));
113
+ client .performRequest (createIndexRequest );
114
+ }
107
115
108
- String bulk = readResource ("/ogc/ogc.json" );
116
+ static void loadGeoDatasetIntoEs (RestClient client , String index ) throws Exception {
117
+ createIndex (client , index , readResource ("/geo/geosql.json" ));
118
+ loadData (client , index , readResource ("/geo/geosql-bulk.json" ));
119
+ }
109
120
121
+ private static void loadData (RestClient client , String index , String bulk ) throws IOException {
110
122
Response response = client .performRequest ("POST" , "/" + index + "/doc/_bulk" , singletonMap ("refresh" , "true" ),
111
123
new StringEntity (bulk , ContentType .APPLICATION_JSON ));
112
124
0 commit comments