Skip to content

Commit

Permalink
Support Elasticsearch 6.x (fixes #158)
Browse files Browse the repository at this point in the history
  • Loading branch information
aecio committed May 31, 2018
1 parent 627d712 commit fa2549b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public ElasticsearchProxyResource(CrawlersManager crawlerManager) {
}
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(request.body(), "UTF-8"));
post.addHeader("Content-Type", "application/json"); // mandatory since ES 6.x
CloseableHttpResponse apiResponse = httpclient.execute(post);
try {
HttpEntity entity = apiResponse.getEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,22 @@ private void createIndexMapping(String indexName) {

String indexEndpoint = "/" + indexName;
boolean exists = false;
String esVersion = "5.x.x";
try {
Response existsResponse = client.performRequest("HEAD", indexEndpoint);
exists = (existsResponse.getStatusLine().getStatusCode() == 200);

Response rootResponse = client.performRequest("GET", "/");
String json = EntityUtils.toString(rootResponse.getEntity());
String versionNumber = mapper.readTree(json).path("version").path("number").asText();
if (versionNumber != null && !versionNumber.isEmpty()) {
esVersion = versionNumber;
}
logger.info("Elasticsearch version: {}", esVersion);
} catch (IOException e) {
throw new RuntimeException(
"Failed to check whether index already exists in Elasticsearch.", e);
}

int esMajorVersion;
try {
esMajorVersion = findEsMajorVersion();
logger.info("Elasticsearch version: {}", esMajorVersion);
} catch (Exception e) {
throw new RuntimeException("Failed to read Elasticsearch version.", e);
}

if (!exists) {
final String targetMapping1x = ""
+ "{"
Expand Down Expand Up @@ -110,7 +109,7 @@ private void createIndexMapping(String indexName) {
+ " }"
+ "}";

String pageProperties = esVersion.startsWith("5.") ? pageMapping5x : targetMapping1x;
String pageProperties = esMajorVersion >= 5 ? pageMapping5x : targetMapping1x;

String mapping =
"{"
Expand All @@ -132,6 +131,20 @@ private void createIndexMapping(String indexName) {
}
}

private int findEsMajorVersion() throws IOException {
Response rootResponse = client.performRequest("GET", "/");
String json = EntityUtils.toString(rootResponse.getEntity());
String versionNumber = mapper.readTree(json).path("version").path("number").asText();
if (versionNumber != null && !versionNumber.isEmpty()) {
String[] split = versionNumber.split("\\.");
if (split.length == 3) {
int majorVersion = Integer.parseInt(split[0]);
return majorVersion;
}
}
throw new RuntimeException("Failed to read Elaticsearch version.");
}

private AbstractHttpEntity createJsonEntity(String mapping) {
return new NStringEntity(mapping, ContentType.APPLICATION_JSON);
}
Expand Down

0 comments on commit fa2549b

Please sign in to comment.