Reduce code duplication in the HTTP Controller
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchHttpController.java
index a93274e..a4a0faa 100644 (file)
 
 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
 
+import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -47,6 +51,8 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.atomic.AtomicBoolean;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.Response.Status.Family;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
@@ -80,7 +86,6 @@ import org.onap.aai.sa.searchdbabstraction.util.AggregationParsingUtil;
 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
 import org.onap.aai.sa.searchdbabstraction.util.ElasticSearchPayloadTranslator;
 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
-import org.springframework.http.HttpStatus;
 
 /**
  * This class has the Elasticsearch implementation of the DB operations defined in DocumentStoreInterface.
@@ -94,6 +99,24 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     private static final Logger metricsLogger =
             LoggerFactory.getInstance().getMetricsLogger(ElasticSearchHttpController.class.getName());
 
+    private static final String JSON_ATTR_VERSION = "_version";
+    private static final String JSON_ATTR_ERROR = "error";
+    private static final String JSON_ATTR_REASON = "reason";
+
+    private static final String DEFAULT_TYPE = "default";
+    private static final String QUERY_PARAM_VERSION = "?version=";
+
+    private static final String MSG_RESOURCE_MISSING = "Specified resource does not exist: ";
+    private static final String MSG_RESPONSE_CODE = "Response Code : ";
+    private static final String MSG_INVALID_DOCUMENT_URL = "Invalid document URL: ";
+    private static final String MSG_HTTP_PUT_FAILED = "Failed to set HTTP request method to PUT.";
+    private static final String MSG_HTTP_POST_FAILED = "Failed to set HTTP request method to POST.";
+    private static final String INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT =
+            "Internal Error: ElasticSearch operation fault occurred";
+    private static final String FAILED_TO_GET_THE_RESPONSE_CODE_FROM_THE_CONNECTION =
+            "Failed to get the response code from the connection.";
+    private static final String FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE = "Failed to parse Elastic Search response.";
+
     private static final String BULK_CREATE_WITHOUT_INDEX_TEMPLATE =
             "{\"create\":{\"_index\" : \"%s\", \"_type\" : \"%s\"} }\n";
     private static final String BULK_CREATE_WITH_INDEX_TEMPLATE =
@@ -103,20 +126,28 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     private static final String BULK_DELETE_TEMPLATE =
             "{ \"delete\": { \"_index\": \"%s\", \"_type\": \"%s\", \"_id\": \"%s\", \"_version\":\"%s\"}}\n";
 
-    private static final String INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT =
-            "Internal Error: ElasticSearch operation fault occurred";
     private final ElasticSearchConfig config;
 
-    private static final String DEFAULT_TYPE = "default";
-
     protected AnalysisConfiguration analysisConfig;
 
-    public static ElasticSearchHttpController getInstance() {
 
-        synchronized (ElasticSearchHttpController.class) {
+    public ElasticSearchHttpController(ElasticSearchConfig config) {
+        this.config = config;
+        analysisConfig = new AnalysisConfiguration();
 
-            if (instance == null) {
+        try {
+            logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_ATTEMPT, getFullUrl("", false));
+            checkConnection();
+            logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_SUCCESS, getFullUrl("", false));
+        } catch (Exception e) {
+            logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, getFullUrl("", false),
+                    e.getMessage());
+        }
+    }
 
+    public static ElasticSearchHttpController getInstance() {
+        synchronized (ElasticSearchHttpController.class) {
+            if (instance == null) {
                 Properties properties = new Properties();
                 File file = new File(SearchDbConstants.ES_CONFIG_FILE);
                 try {
@@ -134,33 +165,16 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         return instance;
     }
 
-    public ElasticSearchHttpController(ElasticSearchConfig config) {
-        this.config = config;
-        analysisConfig = new AnalysisConfiguration();
-
-        try {
-            logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_ATTEMPT, getFullUrl("", false));
-            checkConnection();
-            logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_SUCCESS, getFullUrl("", false));
-        } catch (Exception e) {
-            logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, getFullUrl("", false),
-                    e.getMessage());
-        }
-    }
-
-
     public AnalysisConfiguration getAnalysisConfig() {
         return analysisConfig;
     }
 
     @Override
     public OperationResult createIndex(String index, DocumentSchema documentSchema) {
-
         OperationResult result = new OperationResult();
-        result.setResultCode(500);
+        result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
 
         try {
-
             // Submit the request to ElasticSearch to create the index using a
             // default document type.
             result = createTable(index, DEFAULT_TYPE, analysisConfig.getEsIndexSettings(),
@@ -168,13 +182,14 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
             // ElasticSearch will return us a 200 code on success when we
             // want to report a 201, so translate the result here.
-            result.setResultCode((result.getResultCode() == 200) ? 201 : result.getResultCode());
+            if (result.getResultCode() == Status.OK.getStatusCode()) {
+                result.setResultCode(Status.CREATED.getStatusCode());
+            }
+
             if (isSuccess(result)) {
                 result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}");
             }
-
         } catch (DocumentStoreOperationException | IOException e) {
-
             result.setFailureCause("Document store operation failure.  Cause: " + e.getMessage());
         }
 
@@ -184,14 +199,16 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     @Override
     public OperationResult createDynamicIndex(String index, String dynamicSchema) {
         OperationResult result = new OperationResult();
-        result.setResultCode(500);
+        result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
 
         try {
             result = createTable(index, dynamicSchema);
 
             // ElasticSearch will return us a 200 code on success when we
             // want to report a 201, so translate the result here.
-            result.setResultCode((result.getResultCode() == 200) ? 201 : result.getResultCode());
+            if (result.getResultCode() == Status.OK.getStatusCode()) {
+                result.setResultCode(Status.CREATED.getStatusCode());
+            }
             if (isSuccess(result)) {
                 result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}");
             }
@@ -202,13 +219,12 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         return result;
     }
 
-
     @Override
     public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
 
         // Initialize operation result with a failure codes / fault string
         OperationResult opResult = new OperationResult();
-        opResult.setResultCode(500);
+        opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
         opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -228,21 +244,14 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         handleResponse(conn, opResult);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.DELETE_INDEX_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.DELETE_INDEX_TIME, opResult, indexName);
 
         shutdownConnection(conn);
 
         return opResult;
     }
 
-
-    private OperationResult checkConnection() throws Exception {
-
+    private OperationResult checkConnection() throws IOException {
         String fullUrl = getFullUrl("/_cluster/health", false);
         URL url = null;
         HttpURLConnection conn = null;
@@ -280,20 +289,20 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             return;
         }
 
+        final String methodName = "shutdownConnection";
         InputStream inputstream = null;
         OutputStream outputstream = null;
 
         try {
             inputstream = connection.getInputStream();
         } catch (IOException e) {
-            logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection", e.getLocalizedMessage());
+            logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
         } finally {
             if (inputstream != null) {
                 try {
                     inputstream.close();
                 } catch (IOException e) {
-                    logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection",
-                            e.getLocalizedMessage());
+                    logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
                 }
             }
         }
@@ -301,14 +310,13 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         try {
             outputstream = connection.getOutputStream();
         } catch (IOException e) {
-            logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection", e.getLocalizedMessage());
+            logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
         } finally {
             if (outputstream != null) {
                 try {
                     outputstream.close();
                 } catch (IOException e) {
-                    logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection",
-                            e.getLocalizedMessage());
+                    logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
                 }
             }
         }
@@ -331,7 +339,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         OperationResult opResult = new OperationResult();
 
         // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
+        opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
         opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -344,7 +352,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("PUT");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_PUT_FAILED, e);
         }
 
         StringBuilder sb = new StringBuilder(128);
@@ -371,12 +379,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         shutdownConnection(conn);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.CREATE_INDEX_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, opResult, indexName);
 
         return opResult;
     }
@@ -393,7 +396,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     protected OperationResult createTable(String indexName, String settingsAndMappings)
             throws DocumentStoreOperationException {
         OperationResult result = new OperationResult();
-        result.setResultCode(500);
+        result.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
         result.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -406,7 +409,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("PUT");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_PUT_FAILED, e);
         }
 
         try {
@@ -417,12 +420,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         }
         handleResponse(conn, result);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.CREATE_INDEX_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, result, indexName);
 
         return result;
     }
@@ -432,18 +430,17 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
 
         if (!allowImplicitIndexCreation) {
-
             // Before we do anything, make sure that the specified index actually exists in the
             // document store - we don't want to rely on ElasticSearch to fail the document
             // create because it could be configured to implicitly create a non-existent index,
             // which can lead to hard-to-debug behaviour with queries down the road.
             OperationResult indexExistsResult = checkIndexExistence(indexName);
-            if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) {
-
+            if (!isSuccess(indexExistsResult)) {
                 DocumentOperationResult opResult = new DocumentOperationResult();
-                opResult.setResultCode(HttpStatus.NOT_FOUND.value());
-                opResult.setResult("Document Index '" + indexName + "' does not exist.");
-                opResult.setFailureCause("Document Index '" + indexName + "' does not exist.");
+                opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
+                String resultMsg = "Document Index '" + indexName + "' does not exist.";
+                opResult.setResult(resultMsg);
+                opResult.setFailureCause(resultMsg);
                 return opResult;
             }
         }
@@ -460,21 +457,16 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         // check if the document already exists
         DocumentOperationResult opResult = checkDocumentExistence(indexName, document.getId());
 
-
-        if (opResult.getResultCode() != HttpStatus.NOT_FOUND.value()) {
-            if (opResult.getResultCode() == HttpStatus.CONFLICT.value()) {
+        if (opResult.getResultCode() != Status.NOT_FOUND.getStatusCode()) {
+            if (opResult.getResultCode() == Status.CONFLICT.getStatusCode()) {
                 opResult.setFailureCause("A document with the same id already exists.");
             } else {
                 opResult.setFailureCause("Failed to verify a document with the specified id does not already exist.");
             }
-            opResult.setResultCode(HttpStatus.CONFLICT.value());
+            opResult.setResultCode(Status.CONFLICT.getStatusCode());
             return opResult;
         }
 
-        opResult = new DocumentOperationResult();
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
 
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
@@ -486,37 +478,25 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("PUT");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_PUT_FAILED, e);
         }
 
         attachDocument(conn, document);
 
         logger.debug("Sending 'PUT' request to: " + conn.getURL());
 
-        handleResponse(conn, opResult);
+        opResult = getOperationResult(conn);
         buildDocumentResult(opResult, indexName);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.CREATE_DOCUMENT_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, opResult, indexName);
 
         shutdownConnection(conn);
 
         return opResult;
-
     }
 
     private DocumentOperationResult createDocumentWithoutId(String indexName, DocumentStoreDataEntity document)
             throws DocumentStoreOperationException {
-
-        DocumentOperationResult response = new DocumentOperationResult();
-        // Initialize operation result with a failure codes / fault string
-        response.setResultCode(500);
-        response.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
@@ -527,22 +507,17 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("POST");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_POST_FAILED, e);
         }
 
         attachDocument(conn, document);
 
         logger.debug("Sending 'POST' request to: " + conn.getURL());
 
-        handleResponse(conn, response);
+        DocumentOperationResult response = getOperationResult(conn);
         buildDocumentResult(response, indexName);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.CREATE_DOCUMENT_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getResult()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, response, indexName);
 
         shutdownConnection(conn);
 
@@ -557,11 +532,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
     private DocumentOperationResult checkDocumentExistence(String indexName, String docId)
             throws DocumentStoreOperationException {
-        DocumentOperationResult opResult = new DocumentOperationResult();
-
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
@@ -582,19 +552,15 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             resultCode = conn.getResponseCode();
         } catch (IOException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to get the response code from the connection.", e);
+            throw new DocumentStoreOperationException(FAILED_TO_GET_THE_RESPONSE_CODE_FROM_THE_CONNECTION, e);
         }
 
-        logger.debug("Response Code : " + resultCode);
+        logger.debug(MSG_RESPONSE_CODE + resultCode);
 
+        DocumentOperationResult opResult = createDefaultOperationResult();
         opResult.setResultCode(resultCode);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.GET_DOCUMENT_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                        override, indexName, docId);
+        logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, docId);
 
         shutdownConnection(conn);
 
@@ -611,49 +577,38 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             // create because it could be configured to implicitly create a non-existent index,
             // which can lead to hard-to-debug behaviour with queries down the road.
             OperationResult indexExistsResult = checkIndexExistence(indexName);
-            if ((indexExistsResult.getResultCode() < 200) || (indexExistsResult.getResultCode() >= 300)) {
-
+            if (!isSuccess(indexExistsResult)) {
                 DocumentOperationResult opResult = new DocumentOperationResult();
-                opResult.setResultCode(HttpStatus.NOT_FOUND.value());
-                opResult.setResult("Document Index '" + indexName + "' does not exist.");
-                opResult.setFailureCause("Document Index '" + indexName + "' does not exist.");
+                opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
+                String resultMsg = "Document Index '" + indexName + "' does not exist.";
+                opResult.setResult(resultMsg);
+                opResult.setFailureCause(resultMsg);
                 return opResult;
             }
         }
 
-        DocumentOperationResult opResult = new DocumentOperationResult();
-
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
-        String fullUrl = getFullUrl(
-                "/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + "?version=" + document.getVersion(),
-                false);
+        String fullUrl = getFullUrl("/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + QUERY_PARAM_VERSION
+                + document.getVersion(), false);
         HttpURLConnection conn = initializeConnection(fullUrl);
 
         try {
             conn.setRequestMethod("PUT");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_PUT_FAILED, e);
         }
 
         attachDocument(conn, document);
 
         logger.debug("Sending 'PUT' request to: " + conn.getURL());
 
-        handleResponse(conn, opResult);
+        DocumentOperationResult opResult = getOperationResult(conn);
         buildDocumentResult(opResult, indexName);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger.info(SearchDbMsgs.UPDATE_DOCUMENT_TIME,
-                new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                        .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                override, indexName, document.getId());
+        logMetricsInfo(override, SearchDbMsgs.UPDATE_DOCUMENT_TIME, opResult, indexName, document.getId());
 
         shutdownConnection(conn);
 
@@ -663,18 +618,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     @Override
     public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
             throws DocumentStoreOperationException {
-        DocumentOperationResult opResult = new DocumentOperationResult();
-
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
-        String fullUrl = getFullUrl(
-                "/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + "?version=" + document.getVersion(),
-                false);
+        String fullUrl = getFullUrl("/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + QUERY_PARAM_VERSION
+                + document.getVersion(), false);
         HttpURLConnection conn = initializeConnection(fullUrl);
 
         try {
@@ -686,7 +634,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         logger.debug("\nSending 'DELETE' request to " + conn.getURL());
 
-        handleResponse(conn, opResult);
+        DocumentOperationResult opResult = getOperationResult(conn);
         buildDocumentResult(opResult, indexName);
         // supress the etag and url in response for delete as they are not required
         if (opResult.getDocument() != null) {
@@ -694,11 +642,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             opResult.getDocument().setUrl(null);
         }
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger.info(SearchDbMsgs.DELETE_DOCUMENT_TIME,
-                new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResult())
-                        .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
-                override, indexName, document.getId());
+        logMetricsInfo(override, SearchDbMsgs.DELETE_DOCUMENT_TIME, opResult, indexName, document.getId());
 
         shutdownConnection(conn);
 
@@ -708,12 +652,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     @Override
     public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
             throws DocumentStoreOperationException {
-        DocumentOperationResult opResult = new DocumentOperationResult();
-
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
@@ -721,22 +659,17 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         if (document.getVersion() == null) {
             fullUrl = getFullUrl("/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId(), false);
         } else {
-            fullUrl = getFullUrl(
-                    "/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + "?version=" + document.getVersion(),
-                    false);
+            fullUrl = getFullUrl("/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId() + QUERY_PARAM_VERSION
+                    + document.getVersion(), false);
         }
         HttpURLConnection conn = initializeConnection(fullUrl);
 
         logger.debug("\nSending 'GET' request to: " + conn.getURL());
 
-        handleResponse(conn, opResult);
+        DocumentOperationResult opResult = getOperationResult(conn);
         buildDocumentResult(opResult, indexName);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger.info(SearchDbMsgs.GET_DOCUMENT_TIME,
-                new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                        .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                override, indexName, document.getId());
+        logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, document.getId());
 
         shutdownConnection(conn);
 
@@ -745,11 +678,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
     @Override
     public SearchOperationResult search(String indexName, String queryString) throws DocumentStoreOperationException {
-        SearchOperationResult opResult = new SearchOperationResult();
-
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
 
         String fullUrl = getFullUrl("/" + indexName + "/_search" + "?" + queryString, false);
 
@@ -767,14 +695,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         logger.debug("\nsearch(), Sending 'GET' request to URL : " + conn.getURL());
 
-        handleResponse(conn, opResult);
+        SearchOperationResult opResult = getSearchOperationResult(conn);
         buildSearchResult(opResult, indexName);
 
-
-        metricsLogger.info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
-                new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                        .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                override, indexName, queryString);
+        logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, queryString);
 
         return opResult;
     }
@@ -782,16 +706,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     @Override
     public SearchOperationResult searchWithPayload(String indexName, String query)
             throws DocumentStoreOperationException {
-        SearchOperationResult opResult = new SearchOperationResult();
-
         if (logger.isDebugEnabled()) {
             logger.debug("Querying index: " + indexName + " with query string: " + query);
         }
 
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         String fullUrl = getFullUrl("/" + indexName + "/_search", false);
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -803,7 +721,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("POST");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_POST_FAILED, e);
         }
 
         attachContent(conn, query);
@@ -811,14 +729,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL());
         logger.debug("Request body =  Elasticsearch query = " + query);
 
-        handleResponse(conn, opResult);
+        SearchOperationResult opResult = getSearchOperationResult(conn);
+
         buildSearchResult(opResult, indexName);
 
-        metricsLogger
-                .info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                        override, indexName, query);
+        logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query);
 
         shutdownConnection(conn);
 
@@ -829,17 +744,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     @Override
     public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
             throws DocumentStoreOperationException {
-
-        SearchOperationResult opResult = new SearchOperationResult();
-
         if (logger.isDebugEnabled()) {
             logger.debug("Querying Suggestion index: " + indexName + " with query string: " + query);
         }
 
-        // Initialize operation result with a failure codes / fault string
-        opResult.setResultCode(500);
-        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
-
         String fullUrl = getFullUrl("/" + indexName + "/_suggest", false);
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -851,7 +759,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             conn.setRequestMethod("POST");
         } catch (ProtocolException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e);
+            throw new DocumentStoreOperationException(MSG_HTTP_POST_FAILED, e);
         }
 
         attachContent(conn, query);
@@ -859,14 +767,10 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL());
         logger.debug("Request body =  Elasticsearch query = " + query);
 
-        handleResponse(conn, opResult);
+        SearchOperationResult opResult = getSearchOperationResult(conn);
         buildSuggestResult(opResult, indexName);
 
-        metricsLogger
-                .info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
-                        override, indexName, query);
+        logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query);
 
         shutdownConnection(conn);
 
@@ -907,7 +811,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         try {
             conn = (HttpURLConnection) url.openConnection();
-            conn.setRequestProperty("Content-Type", "application/json");
+            conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
             conn.setDoOutput(true);
         } catch (IOException e) {
             shutdownConnection(conn);
@@ -919,20 +823,20 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
     private void handleResponse(HttpURLConnection conn, OperationResult opResult)
             throws DocumentStoreOperationException {
-        int resultCode = 200;
+        int resultCode;
 
         try {
             resultCode = conn.getResponseCode();
         } catch (IOException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to get the response code from the connection.", e);
+            throw new DocumentStoreOperationException(FAILED_TO_GET_THE_RESPONSE_CODE_FROM_THE_CONNECTION, e);
         }
 
-        logger.debug("Response Code : " + resultCode);
+        logger.debug(MSG_RESPONSE_CODE + resultCode);
 
         InputStream inputStream = null;
 
-        if (!(resultCode >= 200 && resultCode <= 299)) { // 2xx response indicates success
+        if (!isSuccessCode(resultCode)) {
             inputStream = conn.getErrorStream();
         } else {
             try {
@@ -958,8 +862,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             throw new DocumentStoreOperationException("Failed getting the response body payload.", e);
         }
 
-        if (resultCode == HttpStatus.CONFLICT.value()) {
-            opResult.setResultCode(HttpStatus.PRECONDITION_FAILED.value());
+        if (resultCode == Status.CONFLICT.getStatusCode()) {
+            opResult.setResultCode(Status.PRECONDITION_FAILED.getStatusCode());
         } else {
             opResult.setResultCode(resultCode);
         }
@@ -975,8 +879,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         String version = null;
         try {
             JSONObject root = (JSONObject) parser.parse(result);
-            if (root.get("_version") != null) {
-                version = root.get("_version").toString();
+            if (root.get(JSON_ATTR_VERSION) != null) {
+                version = root.get(JSON_ATTR_VERSION).toString();
             }
         } catch (ParseException e) {
             // Not all responses from ElasticSearch include a version, so
@@ -1015,27 +919,23 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     }
 
     private boolean isSuccess(OperationResult result) {
-
         return isSuccessCode(result.getResultCode());
     }
 
-
     private boolean isSuccessCode(int statusCode) {
-        return ((statusCode >= 200) && (statusCode < 300));
+        return Family.familyOf(statusCode).equals(Family.SUCCESSFUL);
     }
 
-
     @Override
     public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
-
         if (logger.isDebugEnabled()) {
-            String dbgString = "ESController: performBulkOperations - Operations: ";
+            StringBuilder dbgString = new StringBuilder("ESController: performBulkOperations - Operations: ");
 
             for (BulkRequest request : requests) {
-                dbgString += "[" + request.toString() + "] ";
+                dbgString.append("[").append(request).append("] ");
             }
 
-            logger.debug(dbgString);
+            logger.debug(dbgString.toString());
         }
 
         // Grab the current time so we can use it to generate a metrics log.
@@ -1068,7 +968,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 conn = (HttpURLConnection) url.openConnection();
                 conn.setRequestMethod("PUT");
                 conn.setDoOutput(true);
-                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+                conn.setRequestProperty(CONTENT_TYPE, APPLICATION_FORM_URLENCODED);
                 conn.setRequestProperty("Connection", "Close");
 
             } catch (IOException e) {
@@ -1149,20 +1049,20 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         // In the success case we don't want the entire result string to be
         // dumped into the metrics log, so concatenate it.
         String resultStringForMetricsLog = result.getResult();
-        if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
+        if (isSuccess(result)) {
             resultStringForMetricsLog =
                     resultStringForMetricsLog.substring(0, Math.max(resultStringForMetricsLog.length(), 85)) + "...";
         }
 
         metricsLogger.info(SearchDbMsgs.BULK_OPERATIONS_TIME,
-                new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
+                new LogFields() //
+                        .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
                         .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, resultStringForMetricsLog),
                 override);
 
         return result;
     }
 
-
     /**
      * This method converts a {@link BulkRequest} object into a json structure which can be understood by ElasticSearch.
      *
@@ -1192,7 +1092,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 // correctly.
                 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), false)) {
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
+                            MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
                             request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
@@ -1202,8 +1102,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 if (!indexExists(ApiUtils.extractIndexFromUri(request.getOperation().getMetaData().getUrl()))) {
 
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Specified resource does not exist: " + request.getOperation().getMetaData().getUrl(),
-                            request.getIndex(), request.getId(), 404, request.getOperation().getMetaData().getUrl()));
+                            MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
+                            request.getId(), 404, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
 
@@ -1244,7 +1144,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 // correctly.
                 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
+                            MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
                             request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
@@ -1254,8 +1154,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 if (!indexExists(request.getIndex())) {
 
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Specified resource does not exist: " + request.getOperation().getMetaData().getUrl(),
-                            request.getIndex(), request.getId(), 404, request.getOperation().getMetaData().getUrl()));
+                            MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
+                            request.getId(), 404, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
 
@@ -1264,8 +1164,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 if (!documentExists(request.getIndex(), request.getId())) {
 
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Specified resource does not exist: " + request.getOperation().getMetaData().getUrl(),
-                            request.getIndex(), request.getId(), 404, request.getOperation().getMetaData().getUrl()));
+                            MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
+                            request.getId(), 404, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
 
@@ -1297,7 +1197,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 // correctly.
                 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
+                            MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
                             request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
@@ -1307,8 +1207,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 if (!indexExists(request.getIndex())) {
 
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Specified resource does not exist: " + request.getOperation().getMetaData().getUrl(),
-                            request.getIndex(), request.getId(), 404, request.getOperation().getMetaData().getUrl()));
+                            MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
+                            request.getId(), 404, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
 
@@ -1317,8 +1217,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                 if (!documentExists(request.getIndex(), request.getId())) {
 
                     fails.add(generateRejectionEntry(request.getOperationType(),
-                            "Specified resource does not exist: " + request.getOperation().getMetaData().getUrl(),
-                            request.getIndex(), request.getId(), 404, request.getOperation().getMetaData().getUrl()));
+                            MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
+                            request.getId(), 404, request.getOperation().getMetaData().getUrl()));
                     return false;
                 }
 
@@ -1342,17 +1242,11 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
     }
 
     private boolean indexExists(String index) throws DocumentStoreOperationException {
-
-        OperationResult indexExistsResult = checkIndexExistence(index);
-
-        return ((indexExistsResult.getResultCode() >= 200) && (indexExistsResult.getResultCode() < 300));
+        return isSuccess(checkIndexExistence(index));
     }
 
     private boolean documentExists(String index, String id) throws DocumentStoreOperationException {
-
-        OperationResult docExistsResult = checkDocumentExistence(index, id);
-
-        return ((docExistsResult.getResultCode() >= 200) && (docExistsResult.getResultCode() < 300));
+        return isSuccess(checkDocumentExistence(index, id));
     }
 
     /**
@@ -1483,11 +1377,6 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
      * @throws DocumentStoreOperationException
      */
     public OperationResult checkIndexExistence(String indexName) throws DocumentStoreOperationException {
-
-        // Initialize operation result with a failure codes / fault string
-        OperationResult opResult = new OperationResult();
-        opResult.setResultCode(500);
-
         // Grab the current time so we can use it to generate a metrics log.
         MdcOverride override = getStartTime(new MdcOverride());
 
@@ -1509,24 +1398,51 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
             resultCode = conn.getResponseCode();
         } catch (IOException e) {
             shutdownConnection(conn);
-            throw new DocumentStoreOperationException("Failed to get the response code from the connection.", e);
+            throw new DocumentStoreOperationException(FAILED_TO_GET_THE_RESPONSE_CODE_FROM_THE_CONNECTION, e);
         }
-        logger.debug("Response Code : " + resultCode);
+        logger.debug(MSG_RESPONSE_CODE + resultCode);
 
+        // Initialize operation result with a failure codes / fault string
+        OperationResult opResult = new OperationResult();
+        setDefaultOperationResultValues(opResult);
         opResult.setResultCode(resultCode);
 
-        // Generate a metrics log so we can track how long the operation took.
-        metricsLogger
-                .info(SearchDbMsgs.CHECK_INDEX_TIME,
-                        new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
-                                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
-                        override, indexName);
+        logMetricsInfo(override, SearchDbMsgs.CHECK_INDEX_TIME, opResult, indexName);
 
         shutdownConnection(conn);
 
         return opResult;
     }
 
+    private DocumentOperationResult getOperationResult(HttpURLConnection conn) throws DocumentStoreOperationException {
+        DocumentOperationResult opResult = createDefaultOperationResult();
+        handleResponse(conn, opResult);
+        return opResult;
+    }
+
+    private SearchOperationResult getSearchOperationResult(HttpURLConnection conn)
+            throws DocumentStoreOperationException {
+        SearchOperationResult opResult = createDefaultSearchOperationResult();
+        handleResponse(conn, opResult);
+        return opResult;
+    }
+
+    private DocumentOperationResult createDefaultOperationResult() {
+        DocumentOperationResult opResult = new DocumentOperationResult();
+        setDefaultOperationResultValues(opResult);
+        return opResult;
+    }
+
+    private SearchOperationResult createDefaultSearchOperationResult() {
+        SearchOperationResult opResult = new SearchOperationResult();
+        setDefaultOperationResultValues(opResult);
+        return opResult;
+    }
+
+    private void setDefaultOperationResultValues(OperationResult opResult) {
+        opResult.setResultCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
+        opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
+    }
 
     private void buildDocumentResult(DocumentOperationResult result, String index)
             throws DocumentStoreOperationException {
@@ -1535,8 +1451,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         JSONObject root;
         try {
             root = (JSONObject) parser.parse(result.getResult());
-
-            if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
+            if (isSuccess(result)) {
                 // Success response object
                 Document doc = new Document();
                 doc.setEtag(result.getResultVersion());
@@ -1547,17 +1462,16 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
             } else {
                 // Error response object
-                JSONObject error = (JSONObject) root.get("error");
+                JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
                 if (error != null) {
-                    result.setError(new ErrorResult(error.get("type").toString(), error.get("reason").toString()));
+                    result.setError(
+                            new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
                 }
 
             }
         } catch (Exception e) {
-            throw new DocumentStoreOperationException("Failed to parse Elastic Search response." + result.getResult());
+            throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
         }
-
-
     }
 
     private String buildDocumentResponseUrl(String index, String id) {
@@ -1571,7 +1485,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
         try {
             root = (JSONObject) parser.parse(result.getResult());
-            if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
+            if (isSuccess(result)) {
                 JSONObject hits = (JSONObject) root.get("hits");
                 JSONArray hitArray = (JSONArray) hits.get("hits");
                 SearchHits searchHits = new SearchHits();
@@ -1583,8 +1497,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                     SearchHit searchHit = new SearchHit();
                     searchHit.setScore((hit.get("_score") != null) ? hit.get("_score").toString() : "");
                     Document doc = new Document();
-                    if (hit.get("_version") != null) {
-                        doc.setEtag((hit.get("_version") != null) ? hit.get("_version").toString() : "");
+                    if (hit.get(JSON_ATTR_VERSION) != null) {
+                        doc.setEtag((hit.get(JSON_ATTR_VERSION) != null) ? hit.get(JSON_ATTR_VERSION).toString() : "");
                     }
 
                     doc.setUrl(
@@ -1606,15 +1520,15 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
                 // success
             } else {
-                JSONObject error = (JSONObject) root.get("error");
+                JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
                 if (error != null) {
-                    result.setError(new ErrorResult(error.get("type").toString(), error.get("reason").toString()));
+                    result.setError(
+                            new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
                 }
             }
         } catch (Exception e) {
-            throw new DocumentStoreOperationException("Failed to parse Elastic Search response." + result.getResult());
+            throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
         }
-
     }
 
     private void buildSuggestResult(SearchOperationResult result, String index) throws DocumentStoreOperationException {
@@ -1622,7 +1536,7 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
         JSONObject root;
         try {
             root = (JSONObject) parser.parse(result.getResult());
-            if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
+            if (isSuccess(result)) {
                 JSONArray hitArray = (JSONArray) root.get("suggest-vnf");
                 JSONObject hitdata = (JSONObject) hitArray.get(0);
                 JSONArray optionsArray = (JSONArray) hitdata.get("options");
@@ -1638,8 +1552,8 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
                     suggestHit.setScore((hit.get("score") != null) ? hit.get("score").toString() : "");
                     suggestHit.setText((hit.get("text") != null) ? hit.get("text").toString() : "");
                     Document doc = new Document();
-                    if (hit.get("_version") != null) {
-                        doc.setEtag((hit.get("_version") != null) ? hit.get("_version").toString() : "");
+                    if (hit.get(JSON_ATTR_VERSION) != null) {
+                        doc.setEtag((hit.get(JSON_ATTR_VERSION) != null) ? hit.get(JSON_ATTR_VERSION).toString() : "");
                     }
                     doc.setUrl(
                             buildDocumentResponseUrl(index, (hit.get("_id") != null) ? hit.get("_id").toString() : ""));
@@ -1661,13 +1575,27 @@ public class ElasticSearchHttpController implements DocumentStoreInterface {
 
                 // success
             } else {
-                JSONObject error = (JSONObject) root.get("error");
+                JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
                 if (error != null) {
-                    result.setError(new ErrorResult(error.get("type").toString(), error.get("reason").toString()));
+                    result.setError(
+                            new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
                 }
             }
         } catch (Exception e) {
-            throw new DocumentStoreOperationException("Failed to parse Elastic Search response." + result.getResult());
+            throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
         }
     }
+
+    /**
+     * Record the timing of the operation in the metrics log.
+     *
+     */
+    private void logMetricsInfo(MdcOverride override, SearchDbMsgs message, OperationResult operationResult,
+            String... args) {
+        metricsLogger.info(message,
+                new LogFields() //
+                        .setField(LogLine.DefinedFields.RESPONSE_CODE, operationResult.getResultCode())
+                        .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, operationResult.getResult()),
+                override, args);
+    }
 }