2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
24 import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
25 import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
26 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
28 import com.fasterxml.jackson.annotation.JsonInclude.Include;
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.JsonMappingException;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.google.common.base.Throwables;
34 import java.io.BufferedReader;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.OutputStream;
41 import java.io.OutputStreamWriter;
42 import java.io.PrintWriter;
43 import java.io.StringWriter;
44 import java.net.HttpURLConnection;
45 import java.net.MalformedURLException;
46 import java.net.ProtocolException;
48 import java.text.SimpleDateFormat;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.List;
52 import java.util.Optional;
53 import java.util.Properties;
54 import java.util.concurrent.atomic.AtomicBoolean;
55 import javax.ws.rs.HttpMethod;
56 import javax.ws.rs.core.Response.Status;
57 import javax.ws.rs.core.UriBuilder;
58 import org.eclipse.jetty.http.HttpStatus;
59 import org.json.simple.JSONArray;
60 import org.json.simple.JSONObject;
61 import org.json.simple.parser.JSONParser;
62 import org.json.simple.parser.ParseException;
63 import org.onap.aai.cl.api.LogFields;
64 import org.onap.aai.cl.api.LogLine;
65 import org.onap.aai.cl.api.Logger;
66 import org.onap.aai.cl.eelf.LoggerFactory;
67 import org.onap.aai.cl.mdc.MdcContext;
68 import org.onap.aai.cl.mdc.MdcOverride;
69 import org.onap.aai.sa.rest.AnalysisConfiguration;
70 import org.onap.aai.sa.rest.ApiUtils;
71 import org.onap.aai.sa.rest.BulkRequest;
72 import org.onap.aai.sa.rest.BulkRequest.OperationType;
73 import org.onap.aai.sa.rest.DocumentSchema;
74 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
75 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
76 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException.ErrorMessage;
77 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult;
78 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResults;
79 import org.onap.aai.sa.searchdbabstraction.entity.Document;
80 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
81 import org.onap.aai.sa.searchdbabstraction.entity.ErrorResult;
82 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
83 import org.onap.aai.sa.searchdbabstraction.entity.OperationResultBuilder;
84 import org.onap.aai.sa.searchdbabstraction.entity.OperationResultBuilder.Type;
85 import org.onap.aai.sa.searchdbabstraction.entity.SearchHit;
86 import org.onap.aai.sa.searchdbabstraction.entity.SearchHits;
87 import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
88 import org.onap.aai.sa.searchdbabstraction.entity.SuggestHit;
89 import org.onap.aai.sa.searchdbabstraction.entity.SuggestHits;
90 import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
91 import org.onap.aai.sa.searchdbabstraction.util.AggregationParsingUtil;
92 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
93 import org.onap.aai.sa.searchdbabstraction.util.ElasticSearchPayloadTranslator;
94 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
97 * This class has the Elasticsearch implementation of the DB operations defined in DocumentStoreInterface.
99 public class ElasticSearchHttpController implements DocumentStoreInterface {
101 private static ElasticSearchHttpController instance = null;
103 private static final Logger logger =
104 LoggerFactory.getInstance().getLogger(ElasticSearchHttpController.class.getName());
105 private static final Logger metricsLogger =
106 LoggerFactory.getInstance().getMetricsLogger(ElasticSearchHttpController.class.getName());
108 private static final String URL_QUERY_VERSION = "version=";
110 private static final String JSON_ATTR_VERSION = "_version";
111 private static final String JSON_ATTR_ERROR = "error";
112 private static final String JSON_ATTR_REASON = "reason";
114 private static final String DEFAULT_TYPE = "default";
116 private static final String MSG_RESOURCE_MISSING = "Specified resource does not exist: ";
117 private static final String MSG_RESPONSE_CODE = "Response Code : ";
118 private static final String MSG_INVALID_DOCUMENT_URL = "Invalid document URL: ";
120 private static final String FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE = "Failed to parse Elastic Search response.";
122 private static final String BULK_CREATE_WITHOUT_INDEX_TEMPLATE =
123 "{\"create\":{\"_index\" : \"%s\", \"_type\" : \"%s\"} }\n";
124 private static final String BULK_CREATE_WITH_INDEX_TEMPLATE =
125 "{\"create\":{\"_index\" : \"%s\", \"_type\" : \"%s\", \"_id\" : \"%s\" } }\n";
126 private static final String BULK_IMPORT_INDEX_TEMPLATE =
127 "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
128 private static final String BULK_DELETE_TEMPLATE =
129 "{ \"delete\": { \"_index\": \"%s\", \"_type\": \"%s\", \"_id\": \"%s\", \"_version\":\"%s\"}}\n";
131 private final ElasticSearchConfig config;
133 protected AnalysisConfiguration analysisConfig;
136 public ElasticSearchHttpController(ElasticSearchConfig config) {
137 this.config = config;
138 analysisConfig = new AnalysisConfiguration();
140 String rootUrl = null;
142 rootUrl = buildUrl(createUriBuilder("")).toString();
143 logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_ATTEMPT, rootUrl);
145 logger.info(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_SUCCESS, rootUrl);
146 } catch (Exception e) {
147 logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, rootUrl, e.getMessage());
151 public static ElasticSearchHttpController getInstance() {
152 synchronized (ElasticSearchHttpController.class) {
153 if (instance == null) {
154 Properties properties = new Properties();
155 File file = new File(SearchDbConstants.ES_CONFIG_FILE);
157 properties.load(new FileInputStream(file));
158 } catch (Exception e) {
159 logger.error(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "ElasticSearchHTTPController.getInstance",
160 e.getLocalizedMessage());
163 ElasticSearchConfig config = new ElasticSearchConfig(properties);
164 instance = new ElasticSearchHttpController(config);
171 public AnalysisConfiguration getAnalysisConfig() {
172 return analysisConfig;
176 public OperationResult createIndex(String index, DocumentSchema documentSchema) {
178 // Submit the request to ElasticSearch to create the index using a default document type.
179 OperationResult result = createTable(index, DEFAULT_TYPE, analysisConfig.getEsIndexSettings(),
180 DocumentSchemaUtil.generateDocumentMappings(documentSchema));
182 // ElasticSearch will return us a 200 code on success when we
183 // want to report a 201, so translate the result here.
184 if (result.getResultCode() == Status.OK.getStatusCode()) {
185 result.setResultCode(Status.CREATED.getStatusCode());
188 if (isSuccess(result)) {
189 result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}");
192 } catch (DocumentStoreOperationException | IOException e) {
193 return new OperationResultBuilder().useDefaults()
194 .failureCause("Document store operation failure. Cause: " + e.getMessage()).build();
199 public OperationResult createDynamicIndex(String index, String dynamicSchema) {
201 OperationResult result = createTable(index, dynamicSchema);
203 // ElasticSearch will return us a 200 code on success when we
204 // want to report a 201, so translate the result here.
205 if (result.getResultCode() == Status.OK.getStatusCode()) {
206 result.setResultCode(Status.CREATED.getStatusCode());
208 if (isSuccess(result)) {
209 result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}");
212 } catch (DocumentStoreOperationException e) {
213 return new OperationResultBuilder().useDefaults()
214 .failureCause("Document store operation failure. Cause: " + e.getMessage()).build();
219 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
220 MdcOverride override = getStartTime(new MdcOverride());
222 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder(indexName)), HttpMethod.DELETE);
223 OperationResult opResult = handleResponse(conn);
224 logMetricsInfo(override, SearchDbMsgs.DELETE_INDEX_TIME, opResult, indexName);
225 shutdownConnection(conn);
231 protected OperationResult createTable(String indexName, String typeName, String indexSettings, String indexMappings)
232 throws DocumentStoreOperationException {
233 if (indexSettings == null) {
234 logger.debug("No settings provided.");
237 if (indexMappings == null) {
238 logger.debug("No mappings provided.");
241 MdcOverride override = getStartTime(new MdcOverride());
243 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder(indexName)), HttpMethod.PUT);
245 StringBuilder sb = new StringBuilder(128);
246 sb.append("{ \"settings\" : ");
247 sb.append(indexSettings);
250 sb.append("\"mappings\" : {");
251 sb.append("\"" + typeName + "\" :");
252 sb.append(indexMappings);
256 attachContent(conn, ElasticSearchPayloadTranslator.translateESPayload(sb.toString()));
257 } catch (IOException e) {
258 logger.error(SearchDbMsgs.INDEX_CREATE_FAILURE, e);
259 throw new DocumentStoreOperationException(e.getMessage(), e);
262 logger.debug("Request content: " + sb);
264 OperationResult opResult = handleResponse(conn);
265 shutdownConnection(conn);
266 logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, opResult, indexName);
272 * Will send the passed in JSON payload to Elasticsearch using the provided index name in an attempt to create the
275 * @param indexName - The name of the index to be created
276 * @param settingsAndMappings - The actual JSON object that will define the index
277 * @return - The operation result of writing into Elasticsearch
278 * @throws DocumentStoreOperationException
280 protected OperationResult createTable(String indexName, String settingsAndMappings)
281 throws DocumentStoreOperationException {
282 MdcOverride override = getStartTime(new MdcOverride());
284 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder(indexName)), HttpMethod.PUT);
286 attachContent(conn, ElasticSearchPayloadTranslator.translateESPayload(settingsAndMappings));
287 } catch (IOException e) {
288 logger.error(SearchDbMsgs.INDEX_CREATE_FAILURE, e);
289 throw new DocumentStoreOperationException(e.getMessage());
292 OperationResult result = handleResponse(conn);
293 logMetricsInfo(override, SearchDbMsgs.CREATE_INDEX_TIME, result, indexName);
299 public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
300 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
302 if (!allowImplicitIndexCreation) {
303 // Before we do anything, make sure that the specified index actually exists in the
304 // document store - we don't want to rely on ElasticSearch to fail the document
305 // create because it could be configured to implicitly create a non-existent index,
306 // which can lead to hard-to-debug behaviour with queries down the road.
307 OperationResult indexExistsResult = checkIndexExistence(indexName);
308 if (!isSuccess(indexExistsResult)) {
309 String resultMsg = "Document Index '" + indexName + "' does not exist.";
310 return (DocumentOperationResult) new OperationResultBuilder(Type.DOCUMENT).status(Status.NOT_FOUND)
311 .result(resultMsg).failureCause(resultMsg).build();
315 if (document.getId() == null || document.getId().isEmpty()) {
316 return createDocumentWithoutId(indexName, document);
318 return createDocumentWithId(indexName, document);
323 public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
324 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
325 if (!allowImplicitIndexCreation) {
326 // Before we do anything, make sure that the specified index actually exists in the
327 // document store - we don't want to rely on ElasticSearch to fail the document
328 // create because it could be configured to implicitly create a non-existent index,
329 // which can lead to hard-to-debug behaviour with queries down the road.
330 OperationResult indexExistsResult = checkIndexExistence(indexName);
331 if (!isSuccess(indexExistsResult)) {
332 DocumentOperationResult opResult = new DocumentOperationResult();
333 opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
334 String resultMsg = "Document Index '" + indexName + "' does not exist.";
335 opResult.setResult(resultMsg);
336 opResult.setFailureCause(resultMsg);
341 MdcOverride override = getStartTime(new MdcOverride());
343 final URL url = buildUrl(createUriBuilder(indexName, DEFAULT_TYPE, document.getId())
344 .replaceQuery(URL_QUERY_VERSION + document.getVersion()));
346 HttpURLConnection conn = createConnection(url, HttpMethod.PUT);
347 attachDocument(conn, document);
349 DocumentOperationResult opResult = getOperationResult(conn);
350 buildDocumentResult(opResult, indexName);
352 logMetricsInfo(override, SearchDbMsgs.UPDATE_DOCUMENT_TIME, opResult, indexName, document.getId());
354 shutdownConnection(conn);
360 public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
361 throws DocumentStoreOperationException {
362 final URL url = buildUrl(createUriBuilder(indexName, DEFAULT_TYPE, document.getId())
363 .replaceQuery(URL_QUERY_VERSION + document.getVersion()));
365 MdcOverride override = getStartTime(new MdcOverride());
367 HttpURLConnection conn = createConnection(url, HttpMethod.DELETE);
369 DocumentOperationResult opResult = getOperationResult(conn);
370 buildDocumentResult(opResult, indexName);
371 // supress the etag and url in response for delete as they are not required
372 if (opResult.getDocument() != null) {
373 opResult.getDocument().setEtag(null);
374 opResult.getDocument().setUrl(null);
377 logMetricsInfo(override, SearchDbMsgs.DELETE_DOCUMENT_TIME, opResult, indexName, document.getId());
379 shutdownConnection(conn);
385 public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
386 throws DocumentStoreOperationException {
387 final UriBuilder uriBuilder = createUriBuilder(indexName, DEFAULT_TYPE, document.getId());
388 if (document.getVersion() != null) {
389 uriBuilder.replaceQuery(URL_QUERY_VERSION + document.getVersion());
392 MdcOverride override = getStartTime(new MdcOverride());
393 HttpURLConnection conn = createConnection(buildUrl(uriBuilder), "GET");
395 DocumentOperationResult opResult = getOperationResult(conn);
396 buildDocumentResult(opResult, indexName);
398 logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, document.getId());
400 shutdownConnection(conn);
406 public SearchOperationResult search(String indexName, String queryString) throws DocumentStoreOperationException {
407 final URL url = buildUrl(createUriBuilder(indexName, "_search").replaceQuery(queryString));
409 MdcOverride override = getStartTime(new MdcOverride());
411 HttpURLConnection conn = createConnection(url, "GET");
412 SearchOperationResult opResult = getSearchOperationResult(conn);
413 buildSearchResult(opResult, indexName);
415 logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, queryString);
421 public SearchOperationResult searchWithPayload(String indexName, String query)
422 throws DocumentStoreOperationException {
423 if (logger.isDebugEnabled()) {
424 logger.debug("Querying index: " + indexName + " with query string: " + query);
426 final URL url = buildUrl(createUriBuilder(indexName, "_search"));
428 MdcOverride override = getStartTime(new MdcOverride());
430 HttpURLConnection conn = createConnection(url, HttpMethod.POST);
431 attachContent(conn, query);
432 logger.debug("Request body = Elasticsearch query = " + query);
434 SearchOperationResult opResult = getSearchOperationResult(conn);
435 buildSearchResult(opResult, indexName);
437 logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query);
439 shutdownConnection(conn);
445 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
446 throws DocumentStoreOperationException {
447 if (logger.isDebugEnabled()) {
448 logger.debug("Querying Suggestion index: " + indexName + " with query string: " + query);
451 MdcOverride override = getStartTime(new MdcOverride());
452 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder(indexName, "_suggest")), HttpMethod.POST);
453 attachContent(conn, query);
455 logger.debug("Request body = Elasticsearch query = " + query);
457 SearchOperationResult opResult = getSearchOperationResult(conn);
458 buildSuggestResult(opResult, indexName);
460 logMetricsInfo(override, SearchDbMsgs.QUERY_DOCUMENT_TIME, opResult, indexName, query);
462 shutdownConnection(conn);
468 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
469 if (logger.isDebugEnabled()) {
470 StringBuilder dbgString = new StringBuilder("ESController: performBulkOperations - Operations: ");
472 for (BulkRequest request : requests) {
473 dbgString.append("[").append(request).append("] ");
476 logger.debug(dbgString.toString());
479 MdcOverride override = getStartTime(new MdcOverride());
481 // Parse the supplied set of operations.
482 // Iterate over the list of operations which we were provided and
483 // translate them into a format that ElasticSearh understands.
485 StringBuilder esOperationSet = new StringBuilder(128);
486 List<ElasticSearchResultItem> rejected = new ArrayList<>();
487 for (BulkRequest request : requests) {
489 // Convert the request to the syntax ElasticSearch likes.
490 if (buildEsOperation(request, esOperationSet, rejected)) {
495 ElasticSearchBulkOperationResult opResult = null;
497 HttpURLConnection conn;
499 conn = (HttpURLConnection) buildUrl(createUriBuilder("_bulk")).openConnection();
500 conn.setRequestMethod(HttpMethod.PUT);
501 conn.setDoOutput(true);
502 conn.setRequestProperty(CONTENT_TYPE, APPLICATION_FORM_URLENCODED);
503 conn.setRequestProperty("Connection", "Close");
505 } catch (IOException e) {
507 logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, e.getMessage());
508 if (logger.isDebugEnabled()) {
509 logger.debug(Throwables.getStackTraceAsString(e));
512 throw new DocumentStoreOperationException(
513 "Failed to open connection to document store. Cause: " + e.getMessage(), e);
516 StringBuilder bulkResult = new StringBuilder(128);
518 // Create an output stream to write our request to.
519 OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
521 if (logger.isDebugEnabled()) {
522 logger.debug("ESController: Sending 'BULK' request to " + conn.getURL());
523 logger.debug("ESController: operations: " + esOperationSet.toString().replaceAll("\n", "\\n"));
526 // Write the resulting request string to our output stream. (this sends the request to ES?)
527 out.write(esOperationSet.toString());
530 // Open an input stream on our connection in order to read back the results.
531 InputStream is = conn.getInputStream();
532 InputStreamReader inputstreamreader = new InputStreamReader(is);
533 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
535 // Read the contents of the input stream into our result string...
536 String esResponseString = null;
538 while ((esResponseString = bufferedreader.readLine()) != null) {
539 bulkResult.append(esResponseString).append("\n");
542 } catch (IOException e) {
544 logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, e.getMessage());
545 if (logger.isDebugEnabled()) {
546 StringWriter sw = new StringWriter();
547 e.printStackTrace(new PrintWriter(sw));
548 logger.debug(sw.toString());
551 throw new DocumentStoreOperationException(
552 "Failure interacting with document store. Cause: " + e.getMessage(), e);
555 if (logger.isDebugEnabled()) {
556 logger.debug("ESController: Received result string from ElasticSearch: = " + bulkResult.toString());
559 // ...and marshal the resulting string into a Java object.
561 opResult = marshallEsBulkResult(bulkResult.toString());
563 } catch (IOException e) {
564 logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, e.getMessage());
565 if (logger.isDebugEnabled()) {
566 logger.debug(Throwables.getStackTraceAsString(e));
569 throw new DocumentStoreOperationException("Failed to marshal response body. Cause: " + e.getMessage(),
574 OperationResult result = new OperationResultBuilder() //
575 .resultCode(HttpStatus.MULTI_STATUS_207) //
576 .result(buildGenericBulkResultSet(opResult, rejected)) //
579 // In the success case we don't want the entire result string to be dumped into the metrics log, so concatenate
581 String resultStringForMetricsLog = result.getResult();
582 if (isSuccess(result)) {
583 resultStringForMetricsLog =
584 resultStringForMetricsLog.substring(0, Math.max(resultStringForMetricsLog.length(), 85)) + "...";
587 metricsLogger.info(SearchDbMsgs.BULK_OPERATIONS_TIME,
589 .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
590 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, resultStringForMetricsLog),
598 * This method queryies ElasticSearch to determine if the supplied index is present in the document store.
600 * @param indexName - The index to look for.
601 * @return - An operation result indicating the success or failure of the check.
602 * @throws DocumentStoreOperationException
604 private OperationResult checkIndexExistence(String indexName) throws DocumentStoreOperationException {
605 MdcOverride override = getStartTime(new MdcOverride());
607 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder(indexName)), HttpMethod.HEAD);
610 resultCode = conn.getResponseCode();
611 } catch (IOException ex) {
612 shutdownConnection(conn);
613 throw new DocumentStoreOperationException(ErrorMessage.NO_RESPONSE_CODE, ex);
615 logger.debug(MSG_RESPONSE_CODE + resultCode);
617 OperationResult opResult = new OperationResultBuilder().useDefaults().resultCode(resultCode).build();
618 logMetricsInfo(override, SearchDbMsgs.CHECK_INDEX_TIME, opResult, indexName);
619 shutdownConnection(conn);
624 private DocumentOperationResult createDocumentWithId(String indexName, DocumentStoreDataEntity document)
625 throws DocumentStoreOperationException {
626 // check if the document already exists
627 DocumentOperationResult opResult = checkDocumentExistence(indexName, document.getId());
629 if (opResult.getResultCode() != Status.NOT_FOUND.getStatusCode()) {
630 if (opResult.getResultCode() == Status.CONFLICT.getStatusCode()) {
631 opResult.setFailureCause("A document with the same id already exists.");
633 opResult.setFailureCause("Failed to verify a document with the specified id does not already exist.");
635 opResult.setResultCode(Status.CONFLICT.getStatusCode());
639 final URL url = buildUrl(createUriBuilder(indexName, DEFAULT_TYPE, document.getId()));
640 MdcOverride override = getStartTime(new MdcOverride());
642 HttpURLConnection conn = createConnection(url, HttpMethod.PUT);
643 attachDocument(conn, document);
645 opResult = getOperationResult(conn);
646 buildDocumentResult(opResult, indexName);
648 logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, opResult, indexName);
650 shutdownConnection(conn);
655 private DocumentOperationResult createDocumentWithoutId(String indexName, DocumentStoreDataEntity document)
656 throws DocumentStoreOperationException {
657 final URL url = buildUrl(createUriBuilder(indexName, DEFAULT_TYPE));
659 MdcOverride override = getStartTime(new MdcOverride());
660 HttpURLConnection conn = createConnection(url, HttpMethod.POST);
661 attachDocument(conn, document);
663 DocumentOperationResult response = getOperationResult(conn);
664 buildDocumentResult(response, indexName);
666 logMetricsInfo(override, SearchDbMsgs.CREATE_DOCUMENT_TIME, response, indexName);
668 shutdownConnection(conn);
673 private void attachDocument(HttpURLConnection conn, DocumentStoreDataEntity doc)
674 throws DocumentStoreOperationException {
675 conn.setRequestProperty("Connection", "Close");
676 attachContent(conn, doc.getContentInJson());
679 private DocumentOperationResult checkDocumentExistence(String indexName, String docId)
680 throws DocumentStoreOperationException {
681 MdcOverride override = getStartTime(new MdcOverride());
682 HttpURLConnection conn =
683 createConnection(buildUrl(createUriBuilder(indexName, DEFAULT_TYPE, docId)), HttpMethod.HEAD);
686 resultCode = conn.getResponseCode();
687 } catch (IOException ex) {
688 shutdownConnection(conn);
689 throw new DocumentStoreOperationException(ErrorMessage.NO_RESPONSE_CODE, ex);
692 logger.debug(MSG_RESPONSE_CODE + resultCode);
694 DocumentOperationResult opResult = (DocumentOperationResult) new OperationResultBuilder(Type.DOCUMENT)
695 .useDefaults().resultCode(resultCode).build();
697 logMetricsInfo(override, SearchDbMsgs.GET_DOCUMENT_TIME, opResult, indexName, docId);
698 shutdownConnection(conn);
703 private void attachContent(HttpURLConnection conn, String content) throws DocumentStoreOperationException {
704 OutputStream outputStream = null;
705 OutputStreamWriter out = null;
708 outputStream = conn.getOutputStream();
709 } catch (IOException e) {
710 shutdownConnection(conn);
711 throw new DocumentStoreOperationException("Failed to get connection output stream.", e);
714 out = new OutputStreamWriter(outputStream);
719 } catch (IOException e) {
720 shutdownConnection(conn);
721 throw new DocumentStoreOperationException("Failed to write to the output stream.", e);
725 private HttpURLConnection initializeConnection(URL url) throws DocumentStoreOperationException {
726 HttpURLConnection conn = null;
728 conn = (HttpURLConnection) url.openConnection();
729 conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
730 conn.setDoOutput(true);
731 } catch (IOException e) {
732 shutdownConnection(conn);
733 throw new DocumentStoreOperationException("Failed to open connection to URL " + url, e);
739 private OperationResult handleResponse(HttpURLConnection conn) throws DocumentStoreOperationException {
740 return handleResponse(conn, new OperationResultBuilder().useDefaults());
743 private OperationResult handleResponse(HttpURLConnection conn, OperationResultBuilder rb)
744 throws DocumentStoreOperationException {
748 resultCode = conn.getResponseCode();
749 } catch (IOException ex) {
750 shutdownConnection(conn);
751 throw new DocumentStoreOperationException(ErrorMessage.NO_RESPONSE_CODE, ex);
754 logger.debug(MSG_RESPONSE_CODE + resultCode);
756 InputStream inputStream = null;
758 if (!ApiUtils.isSuccessStatusCode(resultCode)) {
759 inputStream = conn.getErrorStream();
762 inputStream = conn.getInputStream();
763 } catch (IOException e) {
764 shutdownConnection(conn);
765 throw new DocumentStoreOperationException("Failed to get the response input stream.", e);
769 InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
770 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
772 StringBuilder result = new StringBuilder(128);
773 String string = null;
776 while ((string = bufferedreader.readLine()) != null) {
777 result.append(string).append("\n");
779 } catch (IOException e) {
780 shutdownConnection(conn);
781 throw new DocumentStoreOperationException("Failed getting the response body payload.", e);
784 if (resultCode == Status.CONFLICT.getStatusCode()) {
785 rb.resultCode(Status.PRECONDITION_FAILED.getStatusCode());
787 rb.resultCode(resultCode);
789 if (logger.isDebugEnabled()) {
790 logger.debug("Raw result string from ElasticSearch = " + result.toString());
792 rb.result(result.toString());
793 rb.resultVersion(extractVersion(result.toString()));
797 private String extractVersion(String result) {
798 JSONParser parser = new JSONParser();
799 String version = null;
801 JSONObject root = (JSONObject) parser.parse(result);
802 if (root.get(JSON_ATTR_VERSION) != null) {
803 version = root.get(JSON_ATTR_VERSION).toString();
805 } catch (ParseException e) {
806 // Not all responses from ElasticSearch include a version, so
807 // if we don't get one back, just return an empty string rather
808 // than trigger a false failure.
815 * This convenience method gets the current system time and stores it in an attribute in the supplied
816 * {@link MdcOverride} object so that it can be used later by the metrics logger.
818 * @param override - The {@link MdcOverride} object to update.
819 * @return - The supplied {@link MdcOverride} object.
821 private MdcOverride getStartTime(MdcOverride override) {
823 // Grab the current time...
824 long startTimeInMs = System.currentTimeMillis();
826 // ...and add it as an attribute to the supplied MDC Override object.
827 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
828 override.addAttribute(MdcContext.MDC_START_TIME, formatter.format(startTimeInMs));
830 // Return the MdcOverride object that we were passed.
831 // This looks odd, but it allows us to do stuff like:
833 // MdcOverride ov = getStartTime(new MdcOverride())
835 // which is quite handy, but also allows us to pass in an existing
836 // MdcOverride object which already has some attributes set.
840 private boolean isSuccess(OperationResult result) {
841 return ApiUtils.isSuccessStatusCode(result.getResultCode());
844 private UriBuilder createUriBuilder(String path, String... paths) {
845 UriBuilder builder = UriBuilder.fromPath(path);
846 for (String other : paths) {
849 builder.host(config.getIpAddress());
850 String port = Optional.ofNullable(config.getHttpPort()).orElse("0");
851 builder.port(Integer.valueOf(port));
852 builder.scheme("http");
856 private URL buildUrl(UriBuilder builder) throws DocumentStoreOperationException {
858 return builder.build().toURL();
859 } catch (MalformedURLException e) {
860 logger.error(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "buildUrl", e.getLocalizedMessage());
861 throw new DocumentStoreOperationException("Error building a URL with " + builder.toString(), e);
865 private HttpURLConnection createConnection(final URL url, final String method)
866 throws DocumentStoreOperationException {
867 HttpURLConnection conn = initializeConnection(url);
869 logger.debug("\nSending '" + method + "' request to URL : " + conn.getURL());
870 conn.setRequestMethod(method);
871 } catch (ProtocolException e) {
872 shutdownConnection(conn);
873 throw new DocumentStoreOperationException(ErrorMessage.SET_REQUEST_METHOD_FAILED, e, method);
878 private OperationResult checkConnection() throws IOException, DocumentStoreOperationException {
879 HttpURLConnection conn = createConnection(buildUrl(createUriBuilder("_cluster/health")), HttpMethod.GET);
880 int resultCode = conn.getResponseCode();
881 logger.debug("getClusterHealth() response Code : " + resultCode);
882 shutdownConnection(conn);
883 return new OperationResultBuilder().resultCode(resultCode).build();
886 private void shutdownConnection(HttpURLConnection connection) {
887 if (connection == null) {
891 final String methodName = "shutdownConnection";
892 InputStream inputstream = null;
893 OutputStream outputstream = null;
896 inputstream = connection.getInputStream();
897 } catch (IOException e) {
898 logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
900 if (inputstream != null) {
903 } catch (IOException e) {
904 logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
910 outputstream = connection.getOutputStream();
911 } catch (IOException e) {
912 logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
914 if (outputstream != null) {
916 outputstream.close();
917 } catch (IOException e) {
918 logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, methodName, e.getLocalizedMessage());
923 connection.disconnect();
927 * This method converts a {@link BulkRequest} object into a json structure which can be understood by ElasticSearch.
929 * @param request - The request to be performed.
930 * @param sb - The string builder to append the json data to
931 * @throws DocumentStoreOperationException
933 private boolean buildEsOperation(BulkRequest request, StringBuilder sb, List<ElasticSearchResultItem> fails)
934 throws DocumentStoreOperationException {
936 boolean retVal = true;
937 // What kind of operation are we performing?
938 switch (request.getOperationType()) {
940 // Create a new document.
943 // Make sure that we were supplied a document payload.
944 if (request.getOperation().getDocument() == null) {
946 fails.add(generateRejectionEntry(request.getOperationType(), "Missing document payload",
947 request.getIndex(), request.getId(), 400, request.getOperation().getMetaData().getUrl()));
951 // Make sure that the supplied document URL is formatted
953 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), false)) {
954 fails.add(generateRejectionEntry(request.getOperationType(),
955 MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
956 request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
960 // Validate that the specified index actually exists before we
961 // try to perform the create.
962 if (!indexExists(ApiUtils.extractIndexFromUri(request.getOperation().getMetaData().getUrl()))) {
964 fails.add(generateRejectionEntry(request.getOperationType(),
965 MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
966 request.getId(), 404, request.getOperation().getMetaData().getUrl()));
970 // If we were supplied an id for the new document, then
971 // include it in the bulk operation to Elastic Search
972 if (request.getId() == null) {
974 sb.append(String.format(BULK_CREATE_WITHOUT_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE));
976 // Otherwise, we just leave that parameter off and ElasticSearch
977 // will generate one for us.
979 sb.append(String.format(BULK_CREATE_WITH_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE,
984 // Append the document that we want to create.
985 sb.append(request.getOperation().getDocument().toJson()).append("\n");
986 } catch (JsonProcessingException e) {
987 throw new DocumentStoreOperationException("Failure parsing document to json", e);
992 // Update an existing document.
995 // Make sure that we were supplied a document payload.
996 if (request.getOperation().getDocument() == null) {
998 fails.add(generateRejectionEntry(request.getOperationType(), "Missing document payload",
999 request.getIndex(), request.getId(), 400, request.getOperation().getMetaData().getUrl()));
1003 // Make sure that the supplied document URL is formatted
1005 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
1006 fails.add(generateRejectionEntry(request.getOperationType(),
1007 MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
1008 request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
1012 // Validate that the specified index actually exists before we
1013 // try to perform the update.
1014 if (!indexExists(request.getIndex())) {
1016 fails.add(generateRejectionEntry(request.getOperationType(),
1017 MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
1018 request.getId(), 404, request.getOperation().getMetaData().getUrl()));
1022 // Validate that the document we are trying to update actually
1023 // exists before we try to perform the update.
1024 if (!documentExists(request.getIndex(), request.getId())) {
1026 fails.add(generateRejectionEntry(request.getOperationType(),
1027 MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
1028 request.getId(), 404, request.getOperation().getMetaData().getUrl()));
1032 // It is mandatory that a version be supplied for an update operation,
1033 // so validate that now.
1034 if (request.getOperation().getMetaData().getEtag() == null) {
1036 fails.add(generateRejectionEntry(request.getOperationType(), "Missing mandatory ETag field",
1037 request.getIndex(), request.getId(), 400, request.getOperation().getMetaData().getUrl()));
1041 // Generate the update request...
1042 sb.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE, request.getId(),
1043 request.getOperation().getMetaData().getEtag()));
1045 // ...and append the document that we want to update.
1047 sb.append(request.getOperation().getDocument().toJson()).append("\n");
1048 } catch (JsonProcessingException e) {
1049 throw new DocumentStoreOperationException("Failure parsing document to json", e);
1053 // Delete an existing document.
1056 // Make sure that the supplied document URL is formatted
1058 if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
1059 fails.add(generateRejectionEntry(request.getOperationType(),
1060 MSG_INVALID_DOCUMENT_URL + request.getOperation().getMetaData().getUrl(),
1061 request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
1065 // Validate that the specified index actually exists before we
1066 // try to perform the delete.
1067 if (!indexExists(request.getIndex())) {
1069 fails.add(generateRejectionEntry(request.getOperationType(),
1070 MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
1071 request.getId(), 404, request.getOperation().getMetaData().getUrl()));
1075 // Validate that the document we are trying to update actually
1076 // exists before we try to perform the delete.
1077 if (!documentExists(request.getIndex(), request.getId())) {
1079 fails.add(generateRejectionEntry(request.getOperationType(),
1080 MSG_RESOURCE_MISSING + request.getOperation().getMetaData().getUrl(), request.getIndex(),
1081 request.getId(), 404, request.getOperation().getMetaData().getUrl()));
1085 // It is mandatory that a version be supplied for a delete operation,
1086 // so validate that now.
1087 if (request.getOperation().getMetaData().getEtag() == null) {
1089 fails.add(generateRejectionEntry(request.getOperationType(), "Missing mandatory ETag field",
1090 request.getIndex(), request.getId(), 400, request.getOperation().getMetaData().getUrl()));
1094 // Generate the delete request.
1095 sb.append(String.format(BULK_DELETE_TEMPLATE, request.getIndex(), DEFAULT_TYPE, request.getId(),
1096 request.getOperation().getMetaData().getEtag()));
1104 private boolean indexExists(String index) throws DocumentStoreOperationException {
1105 return isSuccess(checkIndexExistence(index));
1108 private boolean documentExists(String index, String id) throws DocumentStoreOperationException {
1109 return isSuccess(checkDocumentExistence(index, id));
1113 * This method constructs a status entry for a bulk operation which has been rejected before even sending it to the
1116 * @param rejectReason - A message describing why the operation was rejected.
1117 * @param anId - The identifier associated with the document being acted on.
1118 * @param statusCode - An HTTP status code.
1119 * @return - A result set item.
1121 private ElasticSearchResultItem generateRejectionEntry(OperationType opType, String rejectReason, String index,
1122 String anId, int statusCode, String originalUrl) {
1124 ElasticSearchError err = new ElasticSearchError();
1125 err.setReason(rejectReason);
1127 ElasticSearchOperationStatus op = new ElasticSearchOperationStatus();
1130 op.setStatus(statusCode);
1132 op.setAdditionalProperties(ElasticSearchResultItem.REQUEST_URL, originalUrl);
1134 ElasticSearchResultItem rejectionResult = new ElasticSearchResultItem();
1138 rejectionResult.setCreate(op);
1141 rejectionResult.setIndex(op);
1144 rejectionResult.setDelete(op);
1149 return rejectionResult;
1153 * This method takes the json structure returned from ElasticSearch in response to a bulk operations request and
1154 * marshals it into a Java object.
1156 * @param jsonResult - The bulk operations response returned from ElasticSearch.
1157 * @return - The marshalled response.
1158 * @throws JsonParseException
1159 * @throws JsonMappingException
1160 * @throws IOException
1162 private ElasticSearchBulkOperationResult marshallEsBulkResult(String jsonResult) throws IOException {
1163 if (jsonResult != null) {
1164 if (logger.isDebugEnabled()) {
1165 logger.debug("ESController: Marshalling ES result set from json: " + jsonResult.replaceAll("\n", ""));
1168 ObjectMapper mapper = new ObjectMapper();
1169 mapper.setSerializationInclusion(Include.NON_EMPTY);
1171 return mapper.readValue(jsonResult, ElasticSearchBulkOperationResult.class);
1178 * This method takes the marshalled ElasticSearch bulk response and converts it into a generic response payload.
1180 * @param esResult - ElasticSearch bulk operations response.
1181 * @return - A generic result set.
1183 private String buildGenericBulkResultSet(ElasticSearchBulkOperationResult esResult,
1184 List<ElasticSearchResultItem> rejectedOps) {
1186 int totalSuccess = 0;
1189 if (logger.isDebugEnabled()) {
1191 logger.debug("ESController: Build generic result set. ES Results: "
1192 + ((esResult != null) ? esResult.toString() : "[]") + " Rejected Ops: " + rejectedOps.toString());
1195 // Build a combined list of result items from the results returned
1196 // from ElasticSearch and the list of operations that we rejected
1197 // without sending to ElasticSearch.
1198 List<ElasticSearchResultItem> combinedResults = new ArrayList<>();
1199 if (esResult != null) {
1200 combinedResults.addAll(Arrays.asList(esResult.getItems()));
1202 combinedResults.addAll(rejectedOps);
1204 // Iterate over the individual results in the resulting result set.
1205 StringBuilder resultsBuilder = new StringBuilder();
1206 AtomicBoolean firstItem = new AtomicBoolean(true);
1207 for (ElasticSearchResultItem item : combinedResults) {
1209 // Increment the operation counts.
1211 if (ApiUtils.isSuccessStatusCode(item.operationStatus().getStatus())) {
1217 // Prepend a comma to our response string unless this it the
1218 // first result in the set.
1219 if (!firstItem.compareAndSet(true, false)) {
1220 resultsBuilder.append(", ");
1223 // Append the current result as a generic json structure.
1224 resultsBuilder.append(item.toJson());
1227 return "{ \"total_operations\": " + totalOps + ", " + "\"total_success\": " + totalSuccess + ", "
1228 + "\"total_fails\": " + totalFails + ", " + "\"results\": [" + resultsBuilder.toString() + "]}";
1231 private DocumentOperationResult getOperationResult(HttpURLConnection conn) throws DocumentStoreOperationException {
1232 return (DocumentOperationResult) handleResponse(conn, new OperationResultBuilder(Type.DOCUMENT).useDefaults());
1235 private SearchOperationResult getSearchOperationResult(HttpURLConnection conn)
1236 throws DocumentStoreOperationException {
1237 return (SearchOperationResult) handleResponse(conn, new OperationResultBuilder(Type.SEARCH).useDefaults());
1240 private void buildDocumentResult(DocumentOperationResult result, String index)
1241 throws DocumentStoreOperationException {
1243 JSONParser parser = new JSONParser();
1246 root = (JSONObject) parser.parse(result.getResult());
1247 if (isSuccess(result)) {
1248 // Success response object
1249 Document doc = new Document();
1250 doc.setEtag(result.getResultVersion());
1251 doc.setUrl(buildDocumentResponseUrl(index, root.get("_id").toString()));
1253 doc.setContent((JSONObject) root.get("_source"));
1254 result.setDocument(doc);
1257 // Error response object
1258 JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
1259 if (error != null) {
1261 new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
1265 } catch (Exception e) {
1266 throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
1270 private String buildDocumentResponseUrl(String index, String id) {
1271 return ApiUtils.buildDocumentUri(index, id);
1274 private void buildSearchResult(SearchOperationResult result, String index) throws DocumentStoreOperationException {
1275 JSONParser parser = new JSONParser();
1279 root = (JSONObject) parser.parse(result.getResult());
1280 if (isSuccess(result)) {
1281 JSONObject hits = (JSONObject) root.get("hits");
1282 JSONArray hitArray = (JSONArray) hits.get("hits");
1283 SearchHits searchHits = new SearchHits();
1284 searchHits.setTotalHits(hits.get("total").toString());
1285 ArrayList<SearchHit> searchHitArray = new ArrayList<>();
1287 for (int i = 0; i < hitArray.size(); i++) {
1288 JSONObject hit = (JSONObject) hitArray.get(i);
1289 SearchHit searchHit = new SearchHit();
1290 searchHit.setScore((hit.get("_score") != null) ? hit.get("_score").toString() : "");
1291 Document doc = new Document();
1292 if (hit.get(JSON_ATTR_VERSION) != null) {
1293 doc.setEtag((hit.get(JSON_ATTR_VERSION) != null) ? hit.get(JSON_ATTR_VERSION).toString() : "");
1297 buildDocumentResponseUrl(index, (hit.get("_id") != null) ? hit.get("_id").toString() : ""));
1298 doc.setContent((JSONObject) hit.get("_source"));
1299 searchHit.setDocument(doc);
1300 searchHitArray.add(searchHit);
1302 searchHits.setHits(searchHitArray.toArray(new SearchHit[searchHitArray.size()]));
1303 result.setSearchResult(searchHits);
1305 JSONObject aggregations = (JSONObject) root.get("aggregations");
1306 if (aggregations != null) {
1307 AggregationResult[] aggResults = AggregationParsingUtil.parseAggregationResults(aggregations);
1308 AggregationResults aggs = new AggregationResults();
1309 aggs.setAggregations(aggResults);
1310 result.setAggregationResult(aggs);
1315 JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
1316 if (error != null) {
1318 new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
1321 } catch (Exception e) {
1322 throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
1326 private void buildSuggestResult(SearchOperationResult result, String index) throws DocumentStoreOperationException {
1327 JSONParser parser = new JSONParser();
1330 root = (JSONObject) parser.parse(result.getResult());
1331 if (isSuccess(result)) {
1332 JSONArray hitArray = (JSONArray) root.get("suggest-vnf");
1333 JSONObject hitdata = (JSONObject) hitArray.get(0);
1334 JSONArray optionsArray = (JSONArray) hitdata.get("options");
1335 SuggestHits suggestHits = new SuggestHits();
1336 suggestHits.setTotalHits(String.valueOf(optionsArray.size()));
1338 ArrayList<SuggestHit> suggestHitArray = new ArrayList<>();
1340 for (int i = 0; i < optionsArray.size(); i++) {
1341 JSONObject hit = (JSONObject) optionsArray.get(i);
1343 SuggestHit suggestHit = new SuggestHit();
1344 suggestHit.setScore((hit.get("score") != null) ? hit.get("score").toString() : "");
1345 suggestHit.setText((hit.get("text") != null) ? hit.get("text").toString() : "");
1346 Document doc = new Document();
1347 if (hit.get(JSON_ATTR_VERSION) != null) {
1348 doc.setEtag((hit.get(JSON_ATTR_VERSION) != null) ? hit.get(JSON_ATTR_VERSION).toString() : "");
1351 buildDocumentResponseUrl(index, (hit.get("_id") != null) ? hit.get("_id").toString() : ""));
1353 doc.setContent((JSONObject) hit.get("payload"));
1354 suggestHit.setDocument(doc);
1355 suggestHitArray.add(suggestHit);
1357 suggestHits.setHits(suggestHitArray.toArray(new SuggestHit[suggestHitArray.size()]));
1358 result.setSuggestResult(suggestHits);
1360 JSONObject aggregations = (JSONObject) root.get("aggregations");
1361 if (aggregations != null) {
1362 AggregationResult[] aggResults = AggregationParsingUtil.parseAggregationResults(aggregations);
1363 AggregationResults aggs = new AggregationResults();
1364 aggs.setAggregations(aggResults);
1365 result.setAggregationResult(aggs);
1370 JSONObject error = (JSONObject) root.get(JSON_ATTR_ERROR);
1371 if (error != null) {
1373 new ErrorResult(error.get("type").toString(), error.get(JSON_ATTR_REASON).toString()));
1376 } catch (Exception e) {
1377 throw new DocumentStoreOperationException(FAILED_TO_PARSE_ELASTIC_SEARCH_RESPONSE + result.getResult());
1382 * Record the timing of the operation in the metrics log.
1385 private void logMetricsInfo(MdcOverride override, SearchDbMsgs message, OperationResult operationResult,
1387 metricsLogger.info(message,
1389 .setField(LogLine.DefinedFields.RESPONSE_CODE, operationResult.getResultCode())
1390 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, operationResult.getResult()),