+ * Common handler for query requests. This is called by both the GET with payload and POST with
+ * payload variants of the query endpoint.
+ *
+ * @param index - The index to be queried against.
+ * @param content - The payload containing the query structure.
+ * @param request - The HTTP request.
+ * @param headers - The HTTP headers.
+ * @return - A standard HTTP response.
+ */
+ private Response processSuggestQuery(String index, String content, HttpServletRequest request,
+ HttpHeaders headers, DocumentStoreInterface documentStore) {
+
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setSerializationInclusion(Include.NON_EMPTY);
+
+ // Make sure that we were supplied a payload before proceeding.
+ if (content == null) {
+ return handleError(request, content, Status.BAD_REQUEST);
+ }
+
+ // Validate that the request has the appropriate authorization.
+ boolean isValid;
+ try {
+ isValid = searchService.validateRequest(headers, request, ApiUtils.Action.POST,
+ ApiUtils.SEARCH_AUTH_POLICY_NAME);
+
+ } catch (Exception e) {
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "processQuery", e.getMessage());
+ return handleError(request, content, Status.FORBIDDEN);
+ }
+
+ if (!isValid) {
+ return handleError(request, content, Status.FORBIDDEN);
+ }
+
+ SuggestionStatement suggestionStatement;
+
+ try {
+ // Marshall the supplied request payload into a search statement
+ // object.
+ suggestionStatement = mapper.readValue(content, SuggestionStatement.class);
+
+ } catch (Exception e) {
+ return handleError(request, e.getMessage(), Status.BAD_REQUEST);
+ }
+
+ // Now, submit the search statement, translated into
+ // ElasticSearch syntax, to the document store DAO.
+ SearchOperationResult result =
+ documentStore.suggestionQueryWithPayload(index, suggestionStatement.toElasticSearch());
+ String output = null;
+ if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
+ output = prepareSuggestOutput(mapper, result);
+ } else {
+ output = result.getError() != null
+ ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
+ : result.getFailureCause();
+ }
+ Response response = Response.status(result.getResultCode()).entity(output).build();
+
+ // Clear the MDC context so that no other transaction inadvertently
+ // uses our transaction id.
+ ApiUtils.clearMdcContext();
+
+ return response;
+
+ } catch (Exception e) {
+ return handleError(request, e.getMessage(), Status.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Checks the supplied HTTP headers to see if we should allow the underlying document store to
+ * implicitly create the index referenced in a document PUT or POST if it does not already exist
+ * in the data store.