import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
import org.onap.aai.sa.searchdbabstraction.searchapi.SearchStatement;
+import org.onap.aai.sa.searchdbabstraction.searchapi.SuggestionStatement;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.api.Logger;
private static final String REQUEST_HEADER_RESOURCE_VERSION = "If-Match";
private static final String RESPONSE_HEADER_RESOURCE_VERSION = "ETag";
private static final String REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION = "X-CreateIndex";
-
+
protected SearchServiceApi searchService = null;
private Logger logger = LoggerFactory.getInstance().getLogger(DocumentApi.class.getName());
- private Logger auditLogger = LoggerFactory.getInstance()
- .getAuditLogger(DocumentApi.class.getName());
+ private Logger auditLogger =
+ LoggerFactory.getInstance().getAuditLogger(DocumentApi.class.getName());
public DocumentApi(SearchServiceApi searchService) {
this.searchService = searchService;
}
public Response processPost(String content, HttpServletRequest request, HttpHeaders headers,
- HttpServletResponse httpResponse, String index,
- DocumentStoreInterface documentStore) {
+ HttpServletResponse httpResponse, String index, DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
isValid = searchService.validateRequest(headers, request, ApiUtils.Action.POST,
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "DocumentApi.processPost",
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "DocumentApi.processPost",
e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
document.setContent(content);
- DocumentOperationResult result = documentStore.createDocument(index, document, implicitlyCreateIndex(headers));
+ DocumentOperationResult result =
+ documentStore.createDocument(index, document, implicitlyCreateIndex(headers));
String output = null;
if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getDocument());
}
public Response processPut(String content, HttpServletRequest request, HttpHeaders headers,
- HttpServletResponse httpResponse, String index,
- String id, DocumentStoreInterface documentStore) {
+ HttpServletResponse httpResponse, String index, String id,
+ DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
isValid = searchService.validateRequest(headers, request, ApiUtils.Action.PUT,
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "DocumentApi.processPut",
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "DocumentApi.processPut",
e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
return handleError(request, content, Status.FORBIDDEN);
}
- String resourceVersion = headers.getRequestHeaders()
- .getFirst(REQUEST_HEADER_RESOURCE_VERSION);
+ String resourceVersion =
+ headers.getRequestHeaders().getFirst(REQUEST_HEADER_RESOURCE_VERSION);
DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
document.setId(id);
}
public Response processDelete(String content, HttpServletRequest request, HttpHeaders headers,
- HttpServletResponse httpResponse, String index, String id,
- DocumentStoreInterface documentStore) {
+ HttpServletResponse httpResponse, String index, String id,
+ DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
isValid = searchService.validateRequest(headers, request, ApiUtils.Action.DELETE,
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "DocumentApi.processDelete",
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "DocumentApi.processDelete",
e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
return handleError(request, content, Status.FORBIDDEN);
}
- String resourceVersion = headers.getRequestHeaders()
- .getFirst(REQUEST_HEADER_RESOURCE_VERSION);
+ String resourceVersion =
+ headers.getRequestHeaders().getFirst(REQUEST_HEADER_RESOURCE_VERSION);
if (resourceVersion == null || resourceVersion.isEmpty()) {
return handleError(request, "Request header 'If-Match' missing",
javax.ws.rs.core.Response.Status.BAD_REQUEST);
}
public Response processGet(String content, HttpServletRequest request, HttpHeaders headers,
- HttpServletResponse httpResponse, String index, String id,
- DocumentStoreInterface documentStore) {
+ HttpServletResponse httpResponse, String index, String id,
+ DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
isValid = searchService.validateRequest(headers, request, ApiUtils.Action.GET,
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "DocumentApi.processGet",
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "DocumentApi.processGet",
e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
return handleError(request, content, Status.FORBIDDEN);
}
- String resourceVersion = headers.getRequestHeaders()
- .getFirst(REQUEST_HEADER_RESOURCE_VERSION);
+ String resourceVersion =
+ headers.getRequestHeaders().getFirst(REQUEST_HEADER_RESOURCE_VERSION);
DocumentStoreDataEntityImpl document = new DocumentStoreDataEntityImpl();
document.setId(id);
}
public Response processSearchWithGet(String content, HttpServletRequest request,
- HttpHeaders headers, String index,
- String queryText, DocumentStoreInterface documentStore) {
+ HttpHeaders headers, String index, String queryText, DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
isValid = searchService.validateRequest(headers, request, ApiUtils.Action.GET,
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "processSearchWithGet",
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "processSearchWithGet",
e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
SearchOperationResult result = documentStore.search(index, queryText);
String output = null;
if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
- output = mapper.writerWithDefaultPrettyPrinter()
- .writeValueAsString(result.getSearchResult());
+ output =
+ mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getSearchResult());
} else {
output = result.getError() != null
? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getError())
}
public Response queryWithGetWithPayload(String content, HttpServletRequest request,
- HttpHeaders headers, String index,
- DocumentStoreInterface documentStore) {
+ HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
// Initialize the MDC Context for logging purposes.
ApiUtils.initMdcContext(request, headers);
- logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "GET", (request != null)
- ? request.getRequestURL().toString() : "");
+ logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "GET",
+ (request != null) ? request.getRequestURL().toString() : "");
if (logger.isDebugEnabled()) {
logger.debug("Request Body: " + content);
}
}
public Response processSearchWithPost(String content, HttpServletRequest request,
- HttpHeaders headers, String index,
- DocumentStoreInterface documentStore) {
+ 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() : "");
+ logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "POST",
+ (request != null) ? request.getRequestURL().toString() : "");
if (logger.isDebugEnabled()) {
logger.debug("Request Body: " + content);
}
return processQuery(index, content, request, headers, documentStore);
}
+ public Response 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("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.
+ * 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 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) {
+ HttpHeaders headers, DocumentStoreInterface documentStore) {
try {
ObjectMapper mapper = new ObjectMapper();
ApiUtils.SEARCH_AUTH_POLICY_NAME);
} catch (Exception e) {
- logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "processQuery",
- e.getMessage());
+ logger.info(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "processQuery", e.getMessage());
return handleError(request, content, Status.FORBIDDEN);
}
// Now, submit the search statement, translated into
// ElasticSearch syntax, to the document store DAO.
- SearchOperationResult result = documentStore.searchWithPayload(index,
- searchStatement.toElasticSearch());
+ SearchOperationResult result =
+ documentStore.searchWithPayload(index, searchStatement.toElasticSearch());
String output = null;
if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
output = prepareOutput(mapper, result);
}
}
-
/**
- * 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.
+ * 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.
*
* @param headers - The HTTP headers to examine.
*
- * @return - true if the headers indicate that missing indices should be implicitly created,
- * false otherwise.
+ * @return - true if the headers indicate that missing indices should be implicitly created, false
+ * otherwise.
*/
private boolean implicitlyCreateIndex(HttpHeaders headers) {
-
+
boolean createIndexIfNotPresent = false;
- String implicitIndexCreationHeader =
+ String implicitIndexCreationHeader =
headers.getRequestHeaders().getFirst(REQUEST_HEADER_ALLOW_IMPLICIT_INDEX_CREATION);
-
- if( (implicitIndexCreationHeader != null) && (implicitIndexCreationHeader.equals("true")) ) {
+
+ if ((implicitIndexCreationHeader != null) && (implicitIndexCreationHeader.equals("true"))) {
createIndexIfNotPresent = true;
}
-
+
return createIndexIfNotPresent;
}
-
-
+
private String prepareOutput(ObjectMapper mapper, SearchOperationResult result)
throws JsonProcessingException {
StringBuffer output = new StringBuffer();
output.append("{\r\n\"searchResult\":");
- output.append(mapper.writerWithDefaultPrettyPrinter()
- .writeValueAsString(result.getSearchResult()));
+ output.append(
+ mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getSearchResult()));
+ AggregationResults aggs = result.getAggregationResult();
+ if (aggs != null) {
+ output.append(",\r\n\"aggregationResult\":");
+ output.append(mapper.setSerializationInclusion(Include.NON_NULL)
+ .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
+ }
+ output.append("\r\n}");
+ return output.toString();
+ }
+
+ private String prepareSuggestOutput(ObjectMapper mapper, SearchOperationResult result)
+ throws JsonProcessingException {
+ StringBuffer output = new StringBuffer();
+ output.append("{\r\n\"searchResult\":");
+ output.append(
+ mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result.getSuggestResult()));
AggregationResults aggs = result.getAggregationResult();
if (aggs != null) {
output.append(",\r\n\"aggregationResult\":");
(request != null) ? request.getRemoteHost() : "", Integer.toString(status.getStatusCode()));
auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, status.getStatusCode())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status.getStatusCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
(request != null) ? request.getMethod() : "",
(request != null) ? request.getRequestURL().toString() : "",
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-
public class SearchServiceApi {
/**
- * The Data Access Object that we will use to interact with the
- * document store.
+ * The Data Access Object that we will use to interact with the document store.
*/
protected DocumentStoreInterface documentStore = null;
protected ApiUtils apiUtils = null;
-
/**
* Create a new instance of the end point.
*/
init();
}
-
/**
* Performs all one-time initialization required for the end point.
*/
@PUT
@Path("/indexes/{index}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processCreateIndex(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processCreateIndex(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our index API to create the index.
IndexApi indexApi = new IndexApi(this);
@PUT
@Path("/indexes/dynamic/{index}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processCreateDynamicIndex(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processCreateDynamicIndex(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our index API to create the index.
IndexApi indexApi = new IndexApi(this);
@DELETE
@Path("/indexes/{index}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processDeleteIndex(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processDeleteIndex(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our index API to delete the index.
IndexApi indexApi = new IndexApi(this);
return indexApi.processDelete(index, request, headers, documentStore);
}
-
@GET
@Path("/indexes/{index}/documents/{id}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processGetDocument(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpServletResponse httpResponse,
- @Context HttpHeaders headers,
- @PathParam("index") String index,
- @PathParam("id") String id) {
+ public Response processGetDocument(String requestBody, @Context HttpServletRequest request,
+ @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
+ @PathParam("index") String index, @PathParam("id") String id) {
// Forward the request to our document API to retrieve the document.
DocumentApi documentApi = new DocumentApi(this);
- return documentApi.processGet(requestBody, request, headers, httpResponse,
- index, id, documentStore);
+ return documentApi.processGet(requestBody, request, headers, httpResponse, index, id,
+ documentStore);
}
@POST
@Path("/indexes/{index}/documents")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processCreateDocWithoutId(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpServletResponse httpResponse,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processCreateDocWithoutId(String requestBody, @Context HttpServletRequest request,
+ @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
+ @PathParam("index") String index) {
// Forward the request to our document API to create the document.
DocumentApi documentApi = new DocumentApi(this);
- return documentApi.processPost(requestBody, request, headers, httpResponse,
- index, documentStore);
+ return documentApi.processPost(requestBody, request, headers, httpResponse, index,
+ documentStore);
}
@PUT
@Path("/indexes/{index}/documents/{id}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processUpsertDoc(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpServletResponse httpResponse,
- @Context HttpHeaders headers,
- @PathParam("index") String index,
- @PathParam("id") String id) {
+ public Response processUpsertDoc(String requestBody, @Context HttpServletRequest request,
+ @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
+ @PathParam("index") String index, @PathParam("id") String id) {
// Forward the request to our document API to upsert the document.
DocumentApi documentApi = new DocumentApi(this);
- return documentApi.processPut(requestBody, request, headers, httpResponse,
- index, id, documentStore);
+ return documentApi.processPut(requestBody, request, headers, httpResponse, index, id,
+ documentStore);
}
@DELETE
@Path("/indexes/{index}/documents/{id}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processDeleteDoc(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpServletResponse httpResponse,
- @Context HttpHeaders headers,
- @PathParam("index") String index,
- @PathParam("id") String id) {
+ public Response processDeleteDoc(String requestBody, @Context HttpServletRequest request,
+ @Context HttpServletResponse httpResponse, @Context HttpHeaders headers,
+ @PathParam("index") String index, @PathParam("id") String id) {
// Forward the request to our document API to delete the document.
DocumentApi documentApi = new DocumentApi(this);
- return documentApi.processDelete(requestBody, request, headers, httpResponse,
- index, id, documentStore);
+ return documentApi.processDelete(requestBody, request, headers, httpResponse, index, id,
+ documentStore);
}
-
@GET
@Path("/indexes/{index}/query/{queryText}")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processInlineQuery(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index,
- @PathParam("queryText") String queryText) {
+ public Response processInlineQuery(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index,
+ @PathParam("queryText") String queryText) {
// Forward the request to our document API to delete the document.
DocumentApi documentApi = new DocumentApi(this);
- return documentApi.processSearchWithGet(requestBody, request, headers,
- index, queryText, documentStore);
+ return documentApi.processSearchWithGet(requestBody, request, headers, index, queryText,
+ documentStore);
}
-
@GET
@Path("/indexes/{index}/query")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processQueryWithGet(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processQueryWithGet(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our document API to delete the document.
DocumentApi documentApi = new DocumentApi(this);
@POST
@Path("/indexes/{index}/query")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processQuery(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processQuery(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our document API to delete the document.
DocumentApi documentApi = new DocumentApi(this);
return documentApi.processSearchWithPost(requestBody, request, headers, index, documentStore);
}
+ @POST
+ @Path("/indexes/{index}/suggest")
+ @Consumes({MediaType.APPLICATION_JSON})
+ public Response processSuggestQuery(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
+
+ // Forward the request to our document API to query suggestions in the
+ // document.
+ DocumentApi documentApi = new DocumentApi(this);
+ return documentApi.processSuggestQueryWithPost(requestBody, request, headers, index,
+ documentStore);
+ }
@POST
@Path("/bulk")
@Consumes({MediaType.APPLICATION_JSON})
- public Response processBulkRequest(String requestBody,
- @Context HttpServletRequest request,
- @Context HttpHeaders headers,
- @PathParam("index") String index) {
+ public Response processBulkRequest(String requestBody, @Context HttpServletRequest request,
+ @Context HttpHeaders headers, @PathParam("index") String index) {
// Forward the request to our document API to delete the document.
BulkApi bulkApi = new BulkApi(this);
return bulkApi.processPost(requestBody, request, headers, documentStore, apiUtils);
}
- protected boolean validateRequest(HttpHeaders headers,
- HttpServletRequest req,
- Action action,
- String authPolicyFunctionName) throws Exception {
+ protected boolean validateRequest(HttpHeaders headers, HttpServletRequest req, Action action,
+ String authPolicyFunctionName) throws Exception {
SearchDbServiceAuth serviceAuth = new SearchDbServiceAuth();
*/
package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
-
import org.onap.aai.sa.rest.BulkRequest;
import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
import org.onap.aai.sa.rest.DocumentSchema;
-
public interface DocumentStoreInterface {
public OperationResult createIndex(String index, DocumentSchema documentSchema);
public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException;
- public DocumentOperationResult createDocument(String indexName,
- DocumentStoreDataEntity document,
- boolean allowImplicitIndexCreation) throws DocumentStoreOperationException;
+ public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
+ boolean allowImplicitIndexCreation) throws DocumentStoreOperationException;
- public DocumentOperationResult updateDocument(String indexName,
- DocumentStoreDataEntity document,
- boolean allowImplicitIndexCreation) throws DocumentStoreOperationException;
+ public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
+ boolean allowImplicitIndexCreation) throws DocumentStoreOperationException;
public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
throws DocumentStoreOperationException;
public SearchOperationResult searchWithPayload(String indexName, String query)
throws DocumentStoreOperationException;
+ public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
+ throws DocumentStoreOperationException;
/**
- * Forwards a set of operations to the document store as a single, bulk
- * request.
+ * Forwards a set of operations to the document store as a single, bulk request.
*
- * @param anIndex - The index to apply the operations to.
- * @param operations - A java object containing the set of operations to
- * be performed.
+ * @param anIndex - The index to apply the operations to.
+ * @param operations - A java object containing the set of operations to be performed.
* @return - An operation result.
* @throws DocumentStoreOperationException
*/
import org.onap.aai.sa.searchdbabstraction.entity.SearchHit;
import org.onap.aai.sa.searchdbabstraction.entity.SearchHits;
import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
+import org.onap.aai.sa.searchdbabstraction.entity.SuggestHit;
+import org.onap.aai.sa.searchdbabstraction.entity.SuggestHits;
import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
import org.onap.aai.sa.searchdbabstraction.util.AggregationParsingUtil;
import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.core.Response.Status;
-
/**
- * This class has the Elasticsearch implementation of the
- * DB operations defined in DocumentStoreInterface.
+ * This class has the Elasticsearch implementation of the DB operations defined in
+ * DocumentStoreInterface.
*/
public class ElasticSearchHttpController implements DocumentStoreInterface {
private static final String INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT =
"Internal Error: ElasticSearch operation fault occurred";
- private static final Logger logger = LoggerFactory.getInstance()
- .getLogger(ElasticSearchHttpController.class.getName());
- private static final Logger metricsLogger = LoggerFactory.getInstance()
- .getMetricsLogger(ElasticSearchHttpController.class.getName());
+ private static final Logger logger =
+ LoggerFactory.getInstance().getLogger(ElasticSearchHttpController.class.getName());
+ private static final Logger metricsLogger =
+ LoggerFactory.getInstance().getMetricsLogger(ElasticSearchHttpController.class.getName());
private final ElasticSearchConfig config;
private static final String DEFAULT_TYPE = "default";
properties.load(new FileInputStream(file));
} catch (Exception e) {
logger.error(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL,
- "ElasticSearchHTTPController.getInstance",
- e.getLocalizedMessage());
+ "ElasticSearchHTTPController.getInstance", e.getLocalizedMessage());
}
ElasticSearchConfig config = new ElasticSearchConfig(properties);
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());
+ logger.error(SearchDbMsgs.ELASTIC_SEARCH_CONNECTION_FAILURE, null, e, getFullUrl("", false),
+ e.getMessage());
}
}
-
public AnalysisConfiguration getAnalysisConfig() {
return analysisConfig;
}
// Submit the request to ElasticSearch to create the index using a
// default document type.
- result = createTable(index,
- DEFAULT_TYPE,
- analysisConfig.getEsIndexSettings(),
+ result = createTable(index, DEFAULT_TYPE, analysisConfig.getEsIndexSettings(),
DocumentSchemaUtil.generateDocumentMappings(documentSchema));
// ElasticSearch will return us a 200 code on success when we
result.setResultCode((result.getResultCode() == 200) ? 201 : result.getResultCode());
if (isSuccess(result)) {
result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}");
- //result.setResult("{\"index\": \"" + index + ", \"type\": \"" + DEFAULT_TYPE + "\"}");
+ // result.setResult("{\"index\": \"" + index + ", \"type\": \""
+ // + DEFAULT_TYPE + "\"}");
}
} catch (DocumentStoreOperationException e) {
public OperationResult createDynamicIndex(String index, String dynamicSchema) {
OperationResult result = new OperationResult();
result.setResultCode(500);
-
+
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());
} catch (DocumentStoreOperationException e) {
result.setFailureCause("Document store operation failure. Cause: " + e.getMessage());
}
-
+
return result;
}
@Override
public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
- //Initialize operation result with a failure codes / fault string
+ // Initialize operation result with a failure codes / fault string
OperationResult opResult = new OperationResult();
opResult.setResultCode(500);
opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName);
+ override, indexName);
shutdownConnection(conn);
return opResult;
}
-
private OperationResult checkConnection() throws Exception {
String fullUrl = getFullUrl("/_cluster/health", false);
try {
inputstream = connection.getInputStream();
} catch (IOException e) {
- logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection", e.getLocalizedMessage());
+ logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection",
+ e.getLocalizedMessage());
} finally {
if (inputstream != null) {
try {
try {
outputstream = connection.getOutputStream();
} catch (IOException e) {
- logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection", e.getLocalizedMessage());
+ logger.debug(SearchDbMsgs.EXCEPTION_DURING_METHOD_CALL, "shutdownConnection",
+ e.getLocalizedMessage());
} finally {
if (outputstream != null) {
try {
connection.disconnect();
}
- //@Override
- protected OperationResult createTable(String indexName, String typeName,
- String indexSettings, String indexMappings)
- throws DocumentStoreOperationException {
+ // @Override
+ protected OperationResult createTable(String indexName, String typeName, String indexSettings,
+ String indexMappings) throws DocumentStoreOperationException {
if (indexSettings == null) {
logger.debug("No settings provided.");
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
- override,
- indexName);
+ override, indexName);
return opResult;
}
- /**
- * Will send the passed in JSON payload to Elasticsearch using the
- * provided index name in an attempt to create the index.
+ /**
+ * Will send the passed in JSON payload to Elasticsearch using the provided index name in an
+ * attempt to create the index.
*
* @param indexName - The name of the index to be created
* @param settingsAndMappings - The actual JSON object that will define the index
* @return - The operation result of writing into Elasticsearch
* @throws DocumentStoreOperationException
*/
- protected OperationResult createTable(String indexName, String settingsAndMappings) throws DocumentStoreOperationException {
+ protected OperationResult createTable(String indexName, String settingsAndMappings)
+ throws DocumentStoreOperationException {
OperationResult result = new OperationResult();
result.setResultCode(500);
result.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 + "/", false);
HttpURLConnection conn = initializeConnection(fullUrl);
shutdownConnection(conn);
throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e);
}
-
+
attachContent(conn, settingsAndMappings);
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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()),
- override,
- indexName);
-
+ override, indexName);
+
+ shutdownConnection(conn);
+
return result;
}
@Override
- public DocumentOperationResult createDocument(String indexName,
- DocumentStoreDataEntity document,
- 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.
+ public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
+ 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)) {
-
+
DocumentOperationResult opResult = new DocumentOperationResult();
opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
opResult.setResult("Document Index '" + indexName + "' does not exist.");
return opResult;
}
}
-
+
if (document.getId() == null || document.getId().isEmpty()) {
return createDocumentWithoutId(indexName, document);
} else {
}
private DocumentOperationResult createDocumentWithId(String indexName,
- DocumentStoreDataEntity document)
- throws DocumentStoreOperationException {
+ DocumentStoreDataEntity document) throws DocumentStoreOperationException {
// check if the document already exists
DocumentOperationResult opResult = checkDocumentExistence(indexName, document.getId());
-
if (opResult.getResultCode() != Status.NOT_FOUND.getStatusCode()) {
if (opResult.getResultCode() == Status.OK.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.setFailureCause(
+ "Failed to verify a document with the specified id does not already exist.");
}
opResult.setResultCode(Status.CONFLICT.getStatusCode());
return opResult;
// 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(), false);
+ String fullUrl =
+ getFullUrl("/" + indexName + "/" + DEFAULT_TYPE + "/" + document.getId(), false);
HttpURLConnection conn = initializeConnection(fullUrl);
try {
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName);
+ override, indexName);
shutdownConnection(conn);
}
private DocumentOperationResult createDocumentWithoutId(String indexName,
- DocumentStoreDataEntity document)
- throws DocumentStoreOperationException {
+ DocumentStoreDataEntity document) throws DocumentStoreOperationException {
DocumentOperationResult response = new DocumentOperationResult();
// Initialize operation result with a failure codes / fault string
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getResult()),
- override,
- indexName);
+ override, indexName);
shutdownConnection(conn);
attachContent(conn, doc.getContentInJson());
}
- private DocumentOperationResult checkDocumentExistence(String indexName,
- String docId)
+ private DocumentOperationResult checkDocumentExistence(String indexName, String docId)
throws DocumentStoreOperationException {
DocumentOperationResult opResult = new DocumentOperationResult();
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);
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName,
- docId);
+ override, indexName, docId);
shutdownConnection(conn);
}
@Override
- public DocumentOperationResult updateDocument(String indexName,
- DocumentStoreDataEntity document,
- 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.
+ public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
+ 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)) {
-
+
DocumentOperationResult opResult = new DocumentOperationResult();
opResult.setResultCode(Status.NOT_FOUND.getStatusCode());
opResult.setResult("Document Index '" + indexName + "' does not exist.");
return opResult;
}
}
-
+
DocumentOperationResult opResult = new DocumentOperationResult();
// Initialize operation result with a failure codes / fault string
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName,
- document.getId());
+ override, indexName, document.getId());
shutdownConnection(conn);
handleResponse(conn, opResult);
buildDocumentResult(opResult, indexName);
- //supress the etag and url in response for delete as they are not required
+ // supress the etag and url in response for delete as they are not
+ // required
if (opResult.getDocument() != null) {
opResult.getDocument().setEtag(null);
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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResult())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
- override,
- indexName,
- document.getId());
+ override, indexName, document.getId());
shutdownConnection(conn);
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName,
- document.getId());
+ override, indexName, document.getId());
shutdownConnection(conn);
handleResponse(conn, opResult);
buildSearchResult(opResult, indexName);
-
metricsLogger.info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
- override,
- indexName,
- queryString);
+ override, indexName, queryString);
return opResult;
}
buildSearchResult(opResult, indexName);
metricsLogger.info(SearchDbMsgs.QUERY_DOCUMENT_TIME,
- new LogFields()
- .setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
+ .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResult()),
+ override, indexName, query);
+
+ shutdownConnection(conn);
+
+ return opResult;
+ }
+
+ 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.
+ MdcOverride override = getStartTime(new MdcOverride());
+
+ HttpURLConnection conn = initializeConnection(fullUrl);
+
+ try {
+ conn.setRequestMethod("POST");
+ } catch (ProtocolException e) {
+ shutdownConnection(conn);
+ throw new DocumentStoreOperationException("Failed to set HTTP request method to POST.", e);
+ }
+
+ attachContent(conn, query);
+
+ logger.debug("\nsearch(), Sending 'POST' request to URL : " + conn.getURL());
+ logger.debug("Request body = Elasticsearch query = " + query);
+
+ handleResponse(conn, opResult);
+ 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);
+ override, indexName, query);
shutdownConnection(conn);
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);
InputStream inputStream = null;
- if (!(resultCode >= 200 && resultCode <= 299)) { // 2xx response indicates success
+ if (!(resultCode >= 200 && resultCode <= 299)) { // 2xx response
+ // indicates success
inputStream = conn.getErrorStream();
} else {
try {
}
/**
- * This convenience method gets the current system time and stores
- * it in an attribute in the supplied {@link MdcOverride} object so
- * that it can be used later by the metrics logger.
+ * This convenience method gets the current system time and stores it in an attribute in the
+ * supplied {@link MdcOverride} object so that it can be used later by the metrics logger.
*
* @param override - The {@link MdcOverride} object to update.
* @return - The supplied {@link MdcOverride} object.
// Return the MdcOverride object that we were passed.
// This looks odd, but it allows us to do stuff like:
//
- // MdcOverride ov = getStartTime(new MdcOverride())
+ // MdcOverride ov = getStartTime(new MdcOverride())
//
// which is quite handy, but also allows us to pass in an existing
// MdcOverride object which already has some attributes set.
return isSuccessCode(result.getResultCode());
}
-
private boolean isSuccessCode(int statusCode) {
return ((statusCode >= 200) && (statusCode < 300));
}
-
@Override
public OperationResult performBulkOperations(BulkRequest[] requests)
throws DocumentStoreOperationException {
logger.debug(Throwables.getStackTraceAsString(e));
}
- throw new DocumentStoreOperationException("Failed to open connection to document store. Cause: "
- + e.getMessage(), e);
+ throw new DocumentStoreOperationException(
+ "Failed to open connection to document store. Cause: " + e.getMessage(), e);
}
StringBuilder bulkResult = new StringBuilder(128);
try {
// Create an output stream to write our request to.
- OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
- ;
+ OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());;
if (logger.isDebugEnabled()) {
logger.debug("ESController: Sending 'BULK' request to " + conn.getURL());
- logger.debug("ESController: operations: " + esOperationSet.toString().replaceAll("\n",
- "\\n"));
+ logger.debug(
+ "ESController: operations: " + esOperationSet.toString().replaceAll("\n", "\\n"));
}
- // Write the resulting request string to our output stream. (this sends the request to ES?)
+ // Write the resulting request string to our output stream.
+ // (this sends the request to ES?)
out.write(esOperationSet.toString());
out.close();
- // Open an input stream on our connection in order to read back the results.
+ // Open an input stream on our connection in order to read back
+ // the results.
InputStream is = conn.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(is);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
- // Read the contents of the input stream into our result string...
+ // Read the contents of the input stream into our result
+ // string...
String esResponseString = null;
while ((esResponseString = bufferedreader.readLine()) != null) {
logger.debug(sw.toString());
}
- throw new DocumentStoreOperationException("Failure interacting with document store. Cause: "
- + e.getMessage(), e);
+ throw new DocumentStoreOperationException(
+ "Failure interacting with document store. Cause: " + e.getMessage(), e);
}
if (logger.isDebugEnabled()) {
- logger.debug("ESController: Received result string from ElasticSearch: = "
- + bulkResult.toString());
+ logger.debug(
+ "ESController: Received result string from ElasticSearch: = " + bulkResult.toString());
}
// ...and marshal the resulting string into a Java object.
logger.debug(Throwables.getStackTraceAsString(e));
}
- throw new DocumentStoreOperationException("Failed to marshal response body. Cause: "
- + e.getMessage(), e);
+ throw new DocumentStoreOperationException(
+ "Failed to marshal response body. Cause: " + e.getMessage(), e);
}
}
// dumped into the metrics log, so concatenate it.
String resultStringForMetricsLog = result.getResult();
if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
- resultStringForMetricsLog = resultStringForMetricsLog.substring(0,
- Math.max(resultStringForMetricsLog.length(), 85)) + "...";
+ 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.
+ * This method converts a {@link BulkRequest} object into a json structure which can be understood
+ * by ElasticSearch.
*
* @param request - The request to be performed.
- * @param sb - The string builder to append the json data to
+ * @param sb - The string builder to append the json data to
* @throws DocumentStoreOperationException
*/
private boolean buildEsOperation(BulkRequest request, StringBuilder sb,
- List<ElasticSearchResultItem> fails)
- throws DocumentStoreOperationException {
+ List<ElasticSearchResultItem> fails) throws DocumentStoreOperationException {
boolean retVal = true;
OperationResult indexExistsResult = null;
// Make sure that we were supplied a document payload.
if (request.getOperation().getDocument() == null) {
- fails.add(generateRejectionEntry(request.getOperationType(),
- "Missing document payload",
- request.getIndex(),
- request.getId(),
- 400,
+ fails.add(generateRejectionEntry(request.getOperationType(), "Missing document payload",
+ request.getIndex(), request.getId(), 400,
request.getOperation().getMetaData().getUrl()));
return false;
}
if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), false)) {
fails.add(generateRejectionEntry(request.getOperationType(),
"Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
- request.getIndex(),
- "",
- 400,
- request.getOperation().getMetaData().getUrl()));
+ request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
return false;
}
// Validate that the specified index actually exists before we
// try to perform the create.
- 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()));
+ 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()));
return false;
}
// include it in the bulk operation to Elastic Search
if (request.getId() == null) {
- sb.append(String.format(BULK_CREATE_WITHOUT_INDEX_TEMPLATE,
- request.getIndex(),
- DEFAULT_TYPE));
+ sb.append(
+ String.format(BULK_CREATE_WITHOUT_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE));
// Otherwise, we just leave that parameter off and ElasticSearch
// will generate one for us.
} else {
- sb.append(String.format(BULK_CREATE_WITH_INDEX_TEMPLATE,
- request.getIndex(),
- DEFAULT_TYPE,
+ sb.append(String.format(BULK_CREATE_WITH_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE,
request.getId()));
}
// Make sure that we were supplied a document payload.
if (request.getOperation().getDocument() == null) {
- fails.add(generateRejectionEntry(request.getOperationType(),
- "Missing document payload",
- request.getIndex(),
- request.getId(),
- 400,
+ fails.add(generateRejectionEntry(request.getOperationType(), "Missing document payload",
+ request.getIndex(), request.getId(), 400,
request.getOperation().getMetaData().getUrl()));
return false;
}
if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
fails.add(generateRejectionEntry(request.getOperationType(),
"Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
- request.getIndex(),
- "",
- 400,
- request.getOperation().getMetaData().getUrl()));
+ request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
return false;
}
// try to perform the update.
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()));
+ fails
+ .add(generateRejectionEntry(request.getOperationType(),
+ "Specified resource does not exist: "
+ + request.getOperation().getMetaData().getUrl(),
+ request.getIndex(), request.getId(), 404,
+ request.getOperation().getMetaData().getUrl()));
return false;
}
// exists before we try to perform the update.
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()));
+ fails
+ .add(generateRejectionEntry(request.getOperationType(),
+ "Specified resource does not exist: "
+ + request.getOperation().getMetaData().getUrl(),
+ request.getIndex(), request.getId(), 404,
+ request.getOperation().getMetaData().getUrl()));
return false;
}
- // It is mandatory that a version be supplied for an update operation,
+ // It is mandatory that a version be supplied for an update
+ // operation,
// so validate that now.
if (request.getOperation().getMetaData().getEtag() == null) {
fails.add(generateRejectionEntry(request.getOperationType(),
- "Missing mandatory ETag field",
- request.getIndex(),
- request.getId(),
- 400,
+ "Missing mandatory ETag field", request.getIndex(), request.getId(), 400,
request.getOperation().getMetaData().getUrl()));
return false;
}
// Generate the update request...
- sb.append(String.format(BULK_IMPORT_INDEX_TEMPLATE,
- request.getIndex(),
- DEFAULT_TYPE,
- request.getId(),
- request.getOperation().getMetaData().getEtag()));
+ sb.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, request.getIndex(), DEFAULT_TYPE,
+ request.getId(), request.getOperation().getMetaData().getEtag()));
// ...and append the document that we want to update.
try {
if (!ApiUtils.validateDocumentUri(request.getOperation().getMetaData().getUrl(), true)) {
fails.add(generateRejectionEntry(request.getOperationType(),
"Invalid document URL: " + request.getOperation().getMetaData().getUrl(),
- request.getIndex(),
- "",
- 400,
- request.getOperation().getMetaData().getUrl()));
+ request.getIndex(), "", 400, request.getOperation().getMetaData().getUrl()));
return false;
}
// try to perform the delete.
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()));
+ fails
+ .add(generateRejectionEntry(request.getOperationType(),
+ "Specified resource does not exist: "
+ + request.getOperation().getMetaData().getUrl(),
+ request.getIndex(), request.getId(), 404,
+ request.getOperation().getMetaData().getUrl()));
return false;
}
// exists before we try to perform the delete.
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()));
+ fails
+ .add(generateRejectionEntry(request.getOperationType(),
+ "Specified resource does not exist: "
+ + request.getOperation().getMetaData().getUrl(),
+ request.getIndex(), request.getId(), 404,
+ request.getOperation().getMetaData().getUrl()));
return false;
}
- // It is mandatory that a version be supplied for a delete operation,
+ // It is mandatory that a version be supplied for a delete
+ // operation,
// so validate that now.
if (request.getOperation().getMetaData().getEtag() == null) {
fails.add(generateRejectionEntry(request.getOperationType(),
- "Missing mandatory ETag field",
- request.getIndex(),
- request.getId(),
- 400,
+ "Missing mandatory ETag field", request.getIndex(), request.getId(), 400,
request.getOperation().getMetaData().getUrl()));
return false;
}
// Generate the delete request.
- sb.append(String.format(BULK_DELETE_TEMPLATE,
- request.getIndex(),
- DEFAULT_TYPE,
- request.getId(),
- request.getOperation().getMetaData().getEtag()));
+ sb.append(String.format(BULK_DELETE_TEMPLATE, request.getIndex(), DEFAULT_TYPE,
+ request.getId(), request.getOperation().getMetaData().getEtag()));
break;
default:
}
}
/**
- * This method constructs a status entry for a bulk operation which has
- * been rejected before even sending it to the document store.
+ * This method constructs a status entry for a bulk operation which has been rejected before even
+ * sending it to the document store.
*
* @param rejectReason - A message describing why the operation was rejected.
- * @param anId - The identifier associated with the document being
- * acted on.
- * @param statusCode - An HTTP status code.
+ * @param anId - The identifier associated with the document being acted on.
+ * @param statusCode - An HTTP status code.
* @return - A result set item.
*/
- private ElasticSearchResultItem generateRejectionEntry(OperationType opType,
- String rejectReason,
- String index,
- String anId,
- int statusCode,
- String originalUrl) {
+ private ElasticSearchResultItem generateRejectionEntry(OperationType opType, String rejectReason,
+ String index, String anId, int statusCode, String originalUrl) {
ElasticSearchError err = new ElasticSearchError();
err.setReason(rejectReason);
return rejectionResult;
}
-
/**
- * This method takes the json structure returned from ElasticSearch in
- * response to a bulk operations request and marshals it into a Java
- * object.
+ * This method takes the json structure returned from ElasticSearch in response to a bulk
+ * operations request and marshals it into a Java object.
*
- * @param jsonResult - The bulk operations response returned from
- * ElasticSearch.
+ * @param jsonResult - The bulk operations response returned from ElasticSearch.
* @return - The marshalled response.
* @throws JsonParseException
* @throws JsonMappingException
return null;
}
-
/**
- * This method takes the marshalled ElasticSearch bulk response and
- * converts it into a generic response payload.
+ * This method takes the marshalled ElasticSearch bulk response and converts it into a generic
+ * response payload.
*
* @param esResult - ElasticSearch bulk operations response.
* @return - A generic result set.
*/
private String buildGenericBulkResultSet(ElasticSearchBulkOperationResult esResult,
- List<ElasticSearchResultItem> rejectedOps) {
+ List<ElasticSearchResultItem> rejectedOps) {
int totalOps = 0;
int totalSuccess = 0;
if (logger.isDebugEnabled()) {
logger.debug("ESController: Build generic result set. ES Results: "
- + ((esResult != null) ? esResult.toString() : "[]")
- + " Rejected Ops: " + rejectedOps.toString());
+ + ((esResult != null) ? esResult.toString() : "[]") + " Rejected Ops: "
+ + rejectedOps.toString());
}
// Build a combined list of result items from the results returned
}
// Now, build the result string and return it.
- String responseBody = "{ \"total_operations\": " + totalOps + ", "
- + "\"total_success\": " + totalSuccess + ", "
- + "\"total_fails\": " + totalFails + ", "
- + "\"results\": ["
- + resultsBuilder.toString()
- + "]}";
+ String responseBody = "{ \"total_operations\": " + totalOps + ", " + "\"total_success\": "
+ + totalSuccess + ", " + "\"total_fails\": " + totalFails + ", " + "\"results\": ["
+ + resultsBuilder.toString() + "]}";
return responseBody;
}
-
/**
- * This method queryies ElasticSearch to determine if the supplied
- * index is present in the document store.
+ * This method queryies ElasticSearch to determine if the supplied index is present in the
+ * document store.
*
* @param indexName - The index to look for.
- * @return - An operation result indicating the success or failure of
- * the check.
+ * @return - An operation result indicating the success or failure of the check.
* @throws DocumentStoreOperationException
*/
public OperationResult checkIndexExistence(String indexName)
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);
// 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())
+ new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, opResult.getResultCode())
.setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, opResult.getResultCode()),
- override,
- indexName);
+ override, indexName);
shutdownConnection(conn);
return opResult;
}
-
private void buildDocumentResult(DocumentOperationResult result, String index)
throws DocumentStoreOperationException {
// Error response object
JSONObject error = (JSONObject) root.get("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("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) {
doc.setEtag((hit.get("_version") != null) ? hit.get("_version").toString() : "");
}
- doc.setUrl(buildDocumentResponseUrl(index, (hit.get("_id") != null)
- ? hit.get("_id").toString() : ""));
+ doc.setUrl(buildDocumentResponseUrl(index,
+ (hit.get("_id") != null) ? hit.get("_id").toString() : ""));
doc.setContent((JSONObject) hit.get("_source"));
searchHit.setDocument(doc);
searchHitArray.add(searchHit);
} else {
JSONObject error = (JSONObject) root.get("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("reason").toString()));
+ }
+ }
+ } catch (Exception e) {
+ throw new DocumentStoreOperationException(
+ "Failed to parse Elastic Search response." + result.getResult());
+ }
+
+ }
+
+ private void buildSuggestResult(SearchOperationResult result, String index)
+ throws DocumentStoreOperationException {
+
+ JSONParser parser = new JSONParser();
+ JSONObject root;
+ try {
+ root = (JSONObject) parser.parse(result.getResult());
+ if (result.getResultCode() >= 200 && result.getResultCode() <= 299) {
+ JSONArray hitArray = (JSONArray) root.get("suggest-vnf");
+ JSONObject hitdata = (JSONObject) hitArray.get(0);
+ JSONArray optionsArray = (JSONArray) hitdata.get("options");
+ SuggestHits suggestHits = new SuggestHits();
+ suggestHits.setTotalHits(String.valueOf(optionsArray.size()));
+
+ ArrayList<SuggestHit> suggestHitArray = new ArrayList<SuggestHit>();
+
+ for (int i = 0; i < optionsArray.size(); i++) {
+ JSONObject hit = (JSONObject) optionsArray.get(i);
+
+ SuggestHit suggestHit = new SuggestHit();
+ 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() : "");
+ }
+ doc.setUrl(buildDocumentResponseUrl(index,
+ (hit.get("_id") != null) ? hit.get("_id").toString() : ""));
+
+ doc.setContent((JSONObject) hit.get("payload"));
+ suggestHit.setDocument(doc);
+ suggestHitArray.add(suggestHit);
+ }
+ suggestHits.setHits(suggestHitArray.toArray(new SuggestHit[suggestHitArray.size()]));
+ result.setSuggestResult(suggestHits);
+
+ JSONObject aggregations = (JSONObject) root.get("aggregations");
+ if (aggregations != null) {
+ AggregationResult[] aggResults =
+ AggregationParsingUtil.parseAggregationResults(aggregations);
+ AggregationResults aggs = new AggregationResults();
+ aggs.setAggregations(aggResults);
+ result.setAggregationResult(aggs);
+ }
+
+ // success
+ } else {
+ JSONObject error = (JSONObject) root.get("error");
+ if (error != null) {
+ result.setError(
+ new ErrorResult(error.get("type").toString(), error.get("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 SearchHits searchResult;
private AggregationResults aggregationResult;
+ private SuggestHits suggestResult;
public SearchHits getSearchResult() {
return searchResult;
}
+ public SuggestHits getSuggestResult() {
+ return suggestResult;
+ }
+
public AggregationResults getAggregationResult() {
return aggregationResult;
}
this.searchResult = hits;
}
+ public void setSuggestResult(SuggestHits hits) {
+ this.suggestResult = hits;
+ }
+
@Override
public String toString() {
- return "SearchOperationResult [searchResult=" + searchResult
- + ", aggregationResult=" + aggregationResult;
+ return "SearchOperationResult [searchResult=" + searchResult + ", aggregationResult="
+ + aggregationResult + ", suggestResult=" + suggestResult;
}
}
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.sa.searchdbabstraction.entity;
+
+public class SuggestHit {
+
+ private String score;
+ private String text;
+ Document document;
+
+ public String getScore() {
+ return score;
+ }
+
+ public void setScore(String score) {
+ this.score = score;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public Document getDocument() {
+ return document;
+ }
+
+ public void setDocument(Document document) {
+ this.document = document;
+ }
+
+ @Override
+ public String toString() {
+ return "SearchHit [text=" + text + ",score=" + score + ", document=" + document + "]";
+ }
+}
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.sa.searchdbabstraction.entity;
+
+import java.util.Arrays;
+
+public class SuggestHits {
+
+ private String totalHits;
+ private SuggestHit[] hits;
+
+ public String getTotalHits() {
+ return totalHits;
+ }
+
+ public void setTotalHits(String totalHits) {
+ this.totalHits = totalHits;
+ }
+
+ public SuggestHit[] getHits() {
+ return hits;
+ }
+
+ public void setHits(SuggestHit[] hits) {
+ this.hits = hits;
+ }
+
+ @Override
+ public String toString() {
+ return "SuggestHit [totalHits=" + totalHits + ", hits=" + Arrays.toString(hits) + "]";
+ }
+}
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.sa.searchdbabstraction.searchapi;
+
+import org.radeox.util.logging.Logger;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * This class represents the structure of a search statement.
+ *
+ * <p>
+ * The expected JSON structure to represent a Completion suggest search statement is as follows:
+ *
+ * { "suggest-vnf" : { "text" : "VNFs", "completion" : { "field" : "entity_suggest", "size": 1 } } }
+ */
+public class SuggestionStatement {
+
+ @JsonProperty("results-size")
+ private Integer size;
+
+ @JsonProperty("suggest-field")
+ private String field;
+
+ @JsonProperty("suggest-text")
+ private String text;
+
+ public Integer getSize() {
+ return size;
+ }
+
+ public void setSize(Integer size) {
+ this.size = size;
+ }
+
+ public String getField() {
+ return field;
+ }
+
+ public void setField(String field) {
+ this.field = field;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ /**
+ * This method returns a string which represents this statement in syntax that is understandable
+ * by ElasticSearch and is suitable for inclusion in an ElasticSearch query string.
+ *
+ * @return - ElasticSearch syntax string.
+ */
+ public String toElasticSearch() {
+
+ StringBuilder sb = new StringBuilder();
+
+ sb.append("{");
+ sb.append("\"suggest-vnf\": {");
+ sb.append("\"text\": ").append("\"" + text + "\"").append(", ");
+ sb.append("\"completion\": {");
+ sb.append("\"field\": ").append("\"" + field + "\"").append(", ");
+ sb.append("\"size\": ").append(size);
+ sb.append("}");
+ sb.append("}");
+ sb.append("}");
+
+ Logger.debug("Generated raw ElasticSearch suggest statement: " + sb.toString());
+ return sb.toString();
+ }
+
+}
import java.util.Map;
/**
- * This class implements a stubbed version of the document store DAO so
- * that we can run unit tests without trying to connect to a real
- * document store.
+ * This class implements a stubbed version of the document store DAO so that we can run unit tests
+ * without trying to connect to a real document store.
*/
public class StubEsController implements DocumentStoreInterface {
/**
*
*/
- //private IndexAPIHarness indexAPIHarness;
+ // private IndexAPIHarness indexAPIHarness;
StubEsController() {
analysisConfig = new AnalysisConfiguration();
"src/test/resources/json/analysis-config.json");
}
-
@Override
- public OperationResult createIndex(String index,
- DocumentSchema documentSchema) {
+ public OperationResult createIndex(String index, DocumentSchema documentSchema) {
// Just return an OK result, with the parameters that we were passed
// bundled in the response string. This allows unit tests to validate
public OperationResult createDynamicIndex(String index, String dynamicSchema) {
OperationResult opResult = new OperationResult();
opResult.setResultCode(200);
- // Directly return the json as this flow should not edit the json in any way
+ // Directly return the json as this flow should not edit the json in any
+ // way
opResult.setResult(dynamicSchema);
return opResult;
}
}
@Override
- public DocumentOperationResult createDocument(String indexName,
- DocumentStoreDataEntity document,
- boolean allowImplicitIndexCreation)
- throws DocumentStoreOperationException {
-
+ public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
+ boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
+
DocumentOperationResult opResult = buildSampleDocumentOperationResult();
if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
}
@Override
- public DocumentOperationResult updateDocument(String indexName,
- DocumentStoreDataEntity document,
- boolean allowImplicitIndexCreation)
- throws DocumentStoreOperationException {
-
+ public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
+ boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
+
DocumentOperationResult opResult = buildSampleDocumentOperationResult();
if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
}
@Override
- public DocumentOperationResult deleteDocument(String indexName,
- DocumentStoreDataEntity document) throws DocumentStoreOperationException {
+ public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
+ throws DocumentStoreOperationException {
DocumentOperationResult opResult = buildSampleDocumentOperationResult();
-
if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
opResult.setResultCode(404);
} else {
}
@Override
- public DocumentOperationResult getDocument(String indexName,
- DocumentStoreDataEntity document) throws DocumentStoreOperationException {
+ public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
+ throws DocumentStoreOperationException {
DocumentOperationResult opResult = buildSampleDocumentOperationResult();
if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
}
@Override
- public SearchOperationResult search(String indexName,
- String queryText) throws DocumentStoreOperationException {
+ public SearchOperationResult search(String indexName, String queryText)
+ throws DocumentStoreOperationException {
SearchOperationResult opResult = buildSampleSearchOperationResult();
}
@Override
- public SearchOperationResult searchWithPayload(String indexName,
- String query) throws DocumentStoreOperationException {
+ public SearchOperationResult searchWithPayload(String indexName, String query)
+ throws DocumentStoreOperationException {
SearchOperationResult opResult = buildSampleSearchOperationResult();
if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
}
@Override
- public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
+ public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
+ throws DocumentStoreOperationException {
+ SearchOperationResult opResult = new SearchOperationResult();
+
+ if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
+ opResult.setResultCode(404);
+ } else {
+ opResult.setResultCode(200);
+ opResult.setResult(indexName + "@" + query);
+ }
+
+ return opResult;
+ }
+
+ @Override
+ public OperationResult performBulkOperations(BulkRequest[] requests)
+ throws DocumentStoreOperationException {
OperationResult opResult = new OperationResult();
opResult.setResultCode(200);
}
-}
\ No newline at end of file
+}
import org.json.JSONException;
import org.json.JSONObject;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
private static ElasticSearchHttpController elasticSearch;
private static AAIEntityTestObject testDocument;
- private static final String indexMappings = "{\r\n \"properties\": {\r\n \"entityType\": {\r\n \"type\": \"string\"\r\n },\r\n \"edgeTagQueryEntityFieldName\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"edgeTagQueryEntityFieldValue\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"searchTagIDs\" : {\r\n \"type\" : \"string\"\r\n },\r\n \"searchTags\": {\r\n \"type\": \"string\",\r\n \"analyzer\": \"nGram_analyzer\",\r\n \"search_analyzer\": \"whitespace_analyzer\"\r\n }\r\n }\r\n}";
- private static final String indexSettings = "{\r\n \"analysis\": {\r\n \"filter\": {\r\n \"nGram_filter\": {\r\n \"type\": \"nGram\",\r\n \"min_gram\": 1,\r\n \"max_gram\": 50,\r\n \"token_chars\": [\r\n \"letter\",\r\n \"digit\",\r\n \"punctuation\",\r\n \"symbol\"\r\n ]\r\n }\r\n },\r\n \"analyzer\": {\r\n \"nGram_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\",\r\n \"nGram_filter\"\r\n ]\r\n },\r\n \"whitespace_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\"\r\n ]\r\n }\r\n }\r\n }\r\n}";
+ private static final String indexMappings =
+ "{\r\n \"properties\": {\r\n \"entityType\": {\r\n \"type\": \"string\"\r\n },\r\n \"edgeTagQueryEntityFieldName\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"edgeTagQueryEntityFieldValue\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"searchTagIDs\" : {\r\n \"type\" : \"string\"\r\n },\r\n \"searchTags\": {\r\n \"type\": \"string\",\r\n \"analyzer\": \"nGram_analyzer\",\r\n \"search_analyzer\": \"whitespace_analyzer\"\r\n }\r\n }\r\n}";
+ private static final String indexSettings =
+ "{\r\n \"analysis\": {\r\n \"filter\": {\r\n \"nGram_filter\": {\r\n \"type\": \"nGram\",\r\n \"min_gram\": 1,\r\n \"max_gram\": 50,\r\n \"token_chars\": [\r\n \"letter\",\r\n \"digit\",\r\n \"punctuation\",\r\n \"symbol\"\r\n ]\r\n }\r\n },\r\n \"analyzer\": {\r\n \"nGram_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\",\r\n \"nGram_filter\"\r\n ]\r\n },\r\n \"whitespace_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\"\r\n ]\r\n }\r\n }\r\n }\r\n}";
@Before
public void setUp() throws Exception {
@Test
public void testCreateTable() throws Exception {
- OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
+ OperationResult result =
+ elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
System.out.println(result);
}
System.out.println(result);
}
+ @Test
+ public void testsuggestionQueryWithPayload() throws Exception {
+
+ Assert.assertNotNull(elasticSearch.suggestionQueryWithPayload("autoSuggest", "suggest-index"));
+
+ }
+
@Test
public void testDeleteIndex() throws Exception {
OperationResult result = elasticSearch.deleteIndex("test");
@Override
public String getContentInJson() {
try {
- return new JSONObject()
- .put("entityType", entityType)
+ return new JSONObject().put("entityType", entityType)
.put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
.put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
- .put("searchTagIDs", searchTagIDs)
- .put("searchTags", searchTags).toString();
+ .put("searchTagIDs", searchTagIDs).put("searchTags", searchTags).toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();