Skip to content

Commit

Permalink
Elasticsearch index creation - log the exception when a parsing error…
Browse files Browse the repository at this point in the history
… of the index configuration file occurs
  • Loading branch information
josegar74 committed Dec 6, 2023
1 parent c8eb905 commit cd17016
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jeeves.server.context.ServiceContext;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
Expand Down Expand Up @@ -271,11 +272,11 @@ private void createIndex(String indexId, String indexName, boolean dropIndexFirs
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
AcknowledgedResponse deleteIndexResponse = client.getClient().indices().delete(request, RequestOptions.DEFAULT);
if (deleteIndexResponse.isAcknowledged()) {
LOGGER.debug("Index '{}' removed.", new Object[]{indexName});
LOGGER.debug("Index '{}' removed.", indexName);
}
} catch (Exception e) {
// index does not exist ?
LOGGER.debug("Error during index '{}' removal. Error is: {}", new Object[]{indexName, e.getMessage()});
LOGGER.debug("Error during index '{}' removal. Error is: {}", indexName, e.getMessage());
}
}

Expand Down Expand Up @@ -304,7 +305,7 @@ private void createIndex(String indexId, String indexName, boolean dropIndexFirs
CreateIndexResponse createIndexResponse = client.getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);

if (createIndexResponse.isAcknowledged()) {
LOGGER.debug("Index '{}' created", new Object[]{indexName});
LOGGER.debug("Index '{}' created", indexName);
} else {
final String message = String.format("Index '%s' was not created. Error is: %s", indexName, createIndexResponse.toString());
LOGGER.error(message);
Expand All @@ -317,10 +318,13 @@ private void createIndex(String indexId, String indexName, boolean dropIndexFirs
indexName));
}
}
} catch (ElasticsearchParseException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new IOException(ex.getMessage());
} catch (Exception cnce) {
final String message = String.format("Could not connect to index '%s'. Error is %s. Is the index server up and running?",
defaultIndex, cnce.getMessage());
LOGGER.error(message);
LOGGER.error(message, cnce);
throw new IOException(message);
}
}
Expand Down Expand Up @@ -444,7 +448,7 @@ private void sendDocumentsToIndex() {
} catch (Exception e) {
LOGGER.error(
"An error occurred while indexing {} documents in current indexing list. Error is {}.",
new Object[]{listOfDocumentsToIndex.size(), e.getMessage()});
listOfDocumentsToIndex.size(), e.getMessage());
} finally {
// TODO: Trigger this async ?
documents.keySet().forEach(uuid -> overviewFieldUpdater.process(uuid));
Expand Down Expand Up @@ -489,14 +493,14 @@ private void checkIndexResponse(BulkResponse bulkItemResponses,
// TODO: Report the JSON which was causing the error ?

LOGGER.error("Document with error #{}: {}.",
new Object[]{e.getId(), e.getFailureMessage()});
e.getId(), e.getFailureMessage());
LOGGER.error(failureDoc);

try {
listErrorOfDocumentsToIndex.put(e.getId(), mapper.writeValueAsString(docWithErrorInfo));
} catch (JsonProcessingException e1) {
LOGGER.error("Generated document for the index is not properly formatted. Check document #{}: {}.",
new Object[]{e.getId(), e1.getMessage()});
e.getId(), e1.getMessage());
}
}
});
Expand All @@ -505,7 +509,7 @@ private void checkIndexResponse(BulkResponse bulkItemResponses,
BulkResponse response = client.bulkRequest(defaultIndex, listErrorOfDocumentsToIndex);
if (response.status().getStatus() != 201) {
LOGGER.error("Failed to save error documents {}.",
new Object[]{Arrays.toString(errorDocumentIds.toArray())});
Arrays.toString(errorDocumentIds.toArray()));
}
}
}
Expand Down Expand Up @@ -638,7 +642,7 @@ public ObjectNode documentToJson(Element xml) {
mapper.readTree(node.getText()));
} catch (IOException e) {
LOGGER.error("Parsing invalid JSON node {} for property {}. Error is: {}",
new Object[]{node.getTextNormalize(), propertyName, e.getMessage()});
node.getTextNormalize(), propertyName, e.getMessage());
}
} else {
arrayNode.add(
Expand All @@ -657,7 +661,7 @@ public ObjectNode documentToJson(Element xml) {
doc.set("geom", mapper.readTree(nodeElements.get(0).getTextNormalize()));
} catch (IOException e) {
LOGGER.error("Parsing invalid geometry for JSON node {}. Error is: {}",
new Object[]{nodeElements.get(0).getTextNormalize(), e.getMessage()});
nodeElements.get(0).getTextNormalize(), e.getMessage());
}
continue;
}
Expand All @@ -670,7 +674,7 @@ public ObjectNode documentToJson(Element xml) {
));
} catch (IOException e) {
LOGGER.error("Parsing invalid JSON node {} for property {}. Error is: {}",
new Object[]{nodeElements.get(0).getTextNormalize(), propertyName, e.getMessage()});
nodeElements.get(0).getTextNormalize(), propertyName, e.getMessage());
}
} else {
doc.put(propertyName,
Expand Down

0 comments on commit cd17016

Please sign in to comment.