Search in sources :

Example 1 with DSLSearchResult

use of org.apache.atlas.v1.model.discovery.DSLSearchResult in project atlas by apache.

the class MetadataDiscoveryResource method searchUsingQueryDSL.

/**
 * Search using query DSL format.
 *
 * @param dslQuery search query in DSL format.
 * @param limit number of rows to be returned in the result, used for pagination. maxlimit > limit > 0. -1 maps to atlas.search.defaultlimit property value
 * @param offset offset to the results returned, used for pagination. offset >= 0. -1 maps to offset 0
 * Limit and offset in API are used in conjunction with limit and offset in DSL query
 * Final limit = min(API limit, max(query limit - API offset, 0))
 * Final offset = API offset + query offset
 *
 * @return JSON representing the type and results.
 */
@GET
@Path("search/dsl")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response searchUsingQueryDSL(@QueryParam("query") String dslQuery, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("limit") int limit, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("offset") int offset) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingQueryDSL({}, {}, {})", dslQuery, limit, offset);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingQueryDSL(" + dslQuery + ", " + limit + ", " + offset + ")");
        }
        dslQuery = ParamChecker.notEmpty(dslQuery, "dslQuery cannot be null");
        QueryParams queryParams = validateQueryParams(limit, offset);
        AtlasSearchResult result = atlasDiscoveryService.searchUsingDslQuery(dslQuery, queryParams.limit(), queryParams.offset());
        DSLSearchResult dslResult = new DSLSearchResult();
        dslResult.setQueryType(QUERY_TYPE_DSL);
        dslResult.setRequestId(Servlets.getRequestId());
        dslResult.setDataType(result.getType());
        dslResult.setQuery(result.getQueryText());
        dslResult.setCount(0);
        if (CollectionUtils.isNotEmpty(result.getEntities())) {
            for (AtlasEntityHeader entityHeader : result.getEntities()) {
                Referenceable entity = getEntity(entityHeader.getGuid());
                dslResult.addResult(entity);
            }
            if (dslResult.getResults() != null) {
                dslResult.setCount(dslResult.getResults().size());
            }
        } else if (result.getAttributes() != null && CollectionUtils.isNotEmpty(result.getAttributes().getName())) {
            List<String> attrNames = result.getAttributes().getName();
            for (List<Object> attrValues : result.getAttributes().getValues()) {
                if (attrValues == null) {
                    continue;
                }
                Referenceable entity = new Referenceable();
                for (int i = 0; i < attrNames.size(); i++) {
                    String attrName = attrNames.get(i);
                    Object attrValue = attrValues.size() > i ? attrValues.get(i) : null;
                    entity.set(attrName, attrValue);
                }
                dslResult.addResult(entity);
            }
            if (dslResult.getResults() != null) {
                dslResult.setCount(dslResult.getResults().size());
            }
        }
        String response = AtlasJson.toV1SearchJson(dslResult);
        return Response.ok(response).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingQueryDSL({}, {}, {})", dslQuery, limit, offset);
        }
    }
}
Also used : DSLSearchResult(org.apache.atlas.v1.model.discovery.DSLSearchResult) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) List(java.util.List) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AtlasSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult)1 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)1 QueryParams (org.apache.atlas.query.QueryParams)1 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)1 DSLSearchResult (org.apache.atlas.v1.model.discovery.DSLSearchResult)1 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)1