- return processQuery(index, content, request, headers, documentStore);
- }
-
- /**
- * 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 processQuery(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);
- }
-
- SearchStatement searchStatement;
-
- try {
- // Marshall the supplied request payload into a search statement
- // object.
- searchStatement = mapper.readValue(content, SearchStatement.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.searchWithPayload(index,
- searchStatement.toElasticSearch());
- String output = null;
- if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
- output = prepareOutput(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);
+
+ public ResponseEntity<String> processSuggestQueryWithPost(String content, HttpServletRequest request,
+ HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
+
+ // Initialize the MDC Context for logging purposes.
+ ApiUtils.initMdcContext(request, headers);
+
+ logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "POST",
+ (request != null) ? request.getRequestURL().toString() : "");
+ if (logger.isDebugEnabled()) {
+ logger.debug(MSG_REQUEST_BODY + content);
+ }
+
+ return processSuggestQuery(index, content, request, headers, documentStore);
+ }
+
+ /**
+ * 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 ResponseEntity<String> processQuery(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, HttpStatus.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, HttpStatus.FORBIDDEN);
+ }
+
+ if (!isValid) {
+ return handleError(request, content, HttpStatus.FORBIDDEN);
+ }
+
+ SearchStatement searchStatement;
+
+ try {
+ // Marshall the supplied request payload into a search statement
+ // object.
+ searchStatement = mapper.readValue(content, SearchStatement.class);
+
+ } catch (Exception e) {
+ return handleError(request, e.getMessage(), HttpStatus.BAD_REQUEST);
+ }
+
+ // Now, submit the search statement, translated into
+ // ElasticSearch syntax, to the document store DAO.
+ SearchOperationResult result = documentStore.searchWithPayload(index, searchStatement.toElasticSearch());
+ String output = null;
+ if (ApiUtils.isSuccessStatusCode(result.getResultCode())) {
+ output = prepareOutput(mapper, result);
+ } else {
+ output = result.getError() != null
+ ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
+ : result.getFailureCause();
+ }
+ ResponseEntity<String> response =
+ ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON).body(output);
+
+ // 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(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+
+ /**
+ * 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 ResponseEntity<String> 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, HttpStatus.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, HttpStatus.FORBIDDEN);
+ }
+
+ if (!isValid) {
+ return handleError(request, content, HttpStatus.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(), HttpStatus.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 (ApiUtils.isSuccessStatusCode(result.getResultCode())) {
+ output = prepareSuggestOutput(mapper, result);
+ } else {
+ output = result.getError() != null
+ ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
+ : result.getFailureCause();
+ }
+ ResponseEntity<String> response = ResponseEntity.status(result.getResultCode()).body(output);
+
+ // 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(), HttpStatus.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.
+ *
+ * @param headers - The HTTP headers to examine.
+ *
+ * @return - true if the headers indicate that missing indices should be implicitly created, false otherwise.
+ */
+ private boolean implicitlyCreateIndex(HttpHeaders headers) {
+ String implicitIndexCreationHeader = headers.getFirst(REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION);
+ return implicitIndexCreationHeader != null && "true".equals(implicitIndexCreationHeader);