Skip to content

Commit

Permalink
Reworked exception handling for recreateIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
svantulden committed Dec 19, 2014
1 parent 8b67963 commit 1a50011
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
19 changes: 16 additions & 3 deletions src/main/java/de/komoot/photon/importer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import spark.Route;

import java.io.FileNotFoundException;
import java.io.IOException;

import static spark.Spark.*;

Expand Down Expand Up @@ -57,14 +58,26 @@ public static void main(String[] rawArgs) {

Client esNodeClient = esServer.getClient();

if(args.isDeleteIndex()) {
esServer.recreateIndex();
if(args.isDeleteIndex()) {
try {
esServer.recreateIndex();
} catch (IOException e) {
log.error("cannot setup index, elastic search config files not readable", e);
return;
}

log.info("deleted photon index and created an empty new one.");
return;
}

if(args.isNominatimImport()) {
esServer.recreateIndex(); // dump previous data
try {
esServer.recreateIndex(); // dump previous data
} catch (IOException e) {
log.error("cannot setup index, elastic search config files not readable", e);
return;
}

log.info("starting import from nominatim to photon with languages: " + args.getLanguages());
Importer importer = new Importer(esNodeClient, args.getLanguages());
NominatimConnector nominatimConnector = new NominatimConnector(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/de/komoot/photon/importer/elasticsearch/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,20 @@ private File setupDirectories(URL directoryName) {
return mainDirectory;
}

public void recreateIndex() {
public void recreateIndex() throws IOException {
deleteIndex();

final Client client = this.getClient();
final InputStream mappings = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings.json");
final InputStream index_settings = Thread.currentThread().getContextClassLoader().getResourceAsStream("index_settings.json");

String mappingsString = "";
try {
// get mappings as JSONObject
mappingsString = IOUtils.toString(mappings);
} catch(IOException e) {
log.error("cannot setup index, elastic search config files not readable", e);
}
String mappingsString = IOUtils.toString(mappings);
JSONObject mappingsJSON = new JSONObject(mappingsString);

// add all langs to the mapping
mappingsJSON = addLangsToMapping(mappingsJSON);
try {
client.admin().indices().prepareCreate("photon").setSettings(IOUtils.toString(index_settings)).execute().actionGet();
client.admin().indices().preparePutMapping("photon").setType("place").setSource(mappingsJSON.toString()).execute().actionGet();
} catch(IOException e) {
log.error("cannot setup index, elastic search config files not readable", e);
}
client.admin().indices().prepareCreate("photon").setSettings(IOUtils.toString(index_settings)).execute().actionGet();
client.admin().indices().preparePutMapping("photon").setType("place").setSource(mappingsJSON.toString()).execute().actionGet();
}

public DeleteIndexResponse deleteIndex() {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/de/komoot/photon/ESBaseTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.komoot.photon.importer.elasticsearch.Server;
import java.io.File;
import java.io.IOException;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
Expand All @@ -26,7 +27,7 @@ public static void tearDownClass() {
shutdownES();
}

public void setUpES() {
public void setUpES() throws IOException {
if (server != null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.komoot.photon.ESBaseTester;
import de.komoot.photon.importer.model.PhotonDoc;
import java.io.IOException;
import org.json.JSONObject;
import org.junit.*;

Expand All @@ -17,7 +18,7 @@
public class ImporterTest extends ESBaseTester {

@Before
public void setUp() {
public void setUp() throws IOException {
setUpES();
deleteAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.vividsolutions.jts.geom.PrecisionModel;
import de.komoot.photon.ESBaseTester;
import de.komoot.photon.importer.model.PhotonDoc;
import java.io.IOException;
import org.json.JSONObject;
import org.junit.*;

Expand All @@ -20,7 +21,7 @@ public class RemoveStreetDuplicatesTest extends ESBaseTester {
GeometryFactory FACTORY = new GeometryFactory(new PrecisionModel(), 4326);

@Before
public void setUp() {
public void setUp() throws IOException {
setUpES();
deleteAll();
PhotonDoc street1 = this.createStreetDoc(1, "Walserstraße", "6993");
Expand Down

0 comments on commit 1a50011

Please sign in to comment.