Search in sources :

Example 11 with AtlasSearchResult

use of org.apache.atlas.model.discovery.AtlasSearchResult in project incubator-atlas by apache.

the class EntityDiscoveryJerseyResourceIT method testSearchByDSL.

@Test
public void testSearchByDSL() throws Exception {
    String dslQuery = "from " + DATABASE_TYPE_BUILTIN + " " + QUALIFIED_NAME + "=\"" + dbName + "\"";
    AtlasSearchResult searchResult = atlasClientV2.dslSearch(dslQuery);
    assertNotNull(searchResult);
    assertEquals(searchResult.getQueryText(), dslQuery);
    assertEquals(searchResult.getQueryType(), AtlasQueryType.DSL);
    List<AtlasEntityHeader> entities = searchResult.getEntities();
    assertNotNull(entities);
    assertEquals(entities.size(), 1);
    AtlasEntityHeader dbEntity = entities.get(0);
    assertEquals(dbEntity.getTypeName(), DATABASE_TYPE_BUILTIN);
    assertEquals(dbEntity.getDisplayText(), dbName);
    assertEquals(dbEntity.getStatus(), Status.ACTIVE);
    assertNotNull(dbEntity.getGuid());
    assertNull(searchResult.getAttributes());
    assertNull(searchResult.getFullTextResult());
}
Also used : AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Test(org.testng.annotations.Test)

Example 12 with AtlasSearchResult

use of org.apache.atlas.model.discovery.AtlasSearchResult in project ranger by apache.

the class AtlasRESTTagSource method getAtlasActiveEntities.

private List<RangerAtlasEntityWithTags> getAtlasActiveEntities() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getAtlasActiveEntities()");
    }
    List<RangerAtlasEntityWithTags> ret = new ArrayList<>();
    AtlasClientV2 atlasClient = null;
    try {
        atlasClient = getAtlasClient();
    } catch (IOException exception) {
        LOG.error("Failed to get Atlas client.", exception);
    }
    if (atlasClient != null) {
        SearchParameters searchParams = new SearchParameters();
        searchParams.setExcludeDeletedEntities(true);
        searchParams.setClassification("*");
        // searchParams.setIncludeSubClassifications(true);
        // searchParams.setIncludeSubTypes(true);
        searchParams.setIncludeClassificationAttributes(true);
        searchParams.setLimit(REQUESTED_ENTITIES_LIMIT_MAX);
        boolean isMoreData;
        int nextStartIndex = 0;
        do {
            AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
            AtlasTypeRegistry.AtlasTransientTypeRegistry tty = null;
            AtlasSearchResult searchResult = null;
            boolean commitUpdates = false;
            searchParams.setOffset(nextStartIndex);
            isMoreData = false;
            try {
                searchResult = atlasClient.facetedSearch(searchParams);
                AtlasTypesDef typesDef = atlasClient.getAllTypeDefs(new SearchFilter());
                tty = typeRegistry.lockTypeRegistryForUpdate();
                tty.addTypes(typesDef);
                commitUpdates = true;
            } catch (AtlasServiceException | AtlasBaseException excp) {
                LOG.error("failed to download tags from Atlas", excp);
                ret = null;
            } catch (Exception unexpectedException) {
                LOG.error("Failed to download tags from Atlas due to unexpected exception", unexpectedException);
                ret = null;
            } finally {
                if (tty != null) {
                    typeRegistry.releaseTypeRegistryForUpdate(tty, commitUpdates);
                }
            }
            if (commitUpdates && searchResult != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(AtlasType.toJson(searchResult));
                }
                List<AtlasEntityHeader> entityHeaders = searchResult.getEntities();
                if (CollectionUtils.isNotEmpty(entityHeaders)) {
                    nextStartIndex += entityHeaders.size();
                    isMoreData = true;
                    for (AtlasEntityHeader header : entityHeaders) {
                        if (!header.getStatus().equals(AtlasEntity.Status.ACTIVE)) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Skipping entity because it is not ACTIVE, header:[" + header + "]");
                            }
                            continue;
                        }
                        String typeName = header.getTypeName();
                        if (!AtlasResourceMapperUtil.isEntityTypeHandled(typeName)) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Not fetching Atlas entities of type:[" + typeName + "]");
                            }
                            continue;
                        }
                        List<EntityNotificationWrapper.RangerAtlasClassification> allTagsForEntity = new ArrayList<>();
                        for (AtlasClassification classification : header.getClassifications()) {
                            List<EntityNotificationWrapper.RangerAtlasClassification> tags = resolveTag(typeRegistry, classification);
                            if (tags != null) {
                                allTagsForEntity.addAll(tags);
                            }
                        }
                        if (CollectionUtils.isNotEmpty(allTagsForEntity)) {
                            RangerAtlasEntity entity = new RangerAtlasEntity(typeName, header.getGuid(), header.getAttributes());
                            RangerAtlasEntityWithTags entityWithTags = new RangerAtlasEntityWithTags(entity, allTagsForEntity, typeRegistry);
                            ret.add(entityWithTags);
                        }
                    }
                }
            }
        } while (isMoreData);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getAtlasActiveEntities()");
    }
    return ret;
}
Also used : AtlasClientV2(org.apache.atlas.AtlasClientV2) AtlasTypeRegistry(org.apache.atlas.type.AtlasTypeRegistry) SearchFilter(org.apache.atlas.model.SearchFilter) IOException(java.io.IOException) AtlasServiceException(org.apache.atlas.AtlasServiceException) IOException(java.io.IOException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) SearchParameters(org.apache.atlas.model.discovery.SearchParameters) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasServiceException(org.apache.atlas.AtlasServiceException) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef)

Example 13 with AtlasSearchResult

use of org.apache.atlas.model.discovery.AtlasSearchResult in project atlas by apache.

the class MetadataDiscoveryResource method searchUsingFullText.

/**
 * Search using full text search.
 *
 * @param query search query.
 * @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
 * @return JSON representing the type and results.
 */
@GET
@Path("search/fulltext")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response searchUsingFullText(@QueryParam("query") String query, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("limit") int limit, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("offset") int offset) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingFullText({}, {}, {})", query, limit, offset);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingFullText(" + query + ", " + limit + ", " + offset + ")");
        }
        query = ParamChecker.notEmpty(query, "query cannot be null or empty");
        QueryParams queryParams = validateQueryParams(limit, offset);
        AtlasSearchResult result = atlasDiscoveryService.searchUsingFullTextQuery(query, false, queryParams.limit(), queryParams.offset());
        FullTextSearchResult fullTextResult = new FullTextSearchResult();
        fullTextResult.setQueryType(QUERY_TYPE_FULLTEXT);
        fullTextResult.setRequestId(Servlets.getRequestId());
        fullTextResult.setDataType(result.getType());
        fullTextResult.setQuery(result.getQueryText());
        fullTextResult.setCount(0);
        if (CollectionUtils.isNotEmpty(result.getFullTextResult())) {
            for (AtlasSearchResult.AtlasFullTextResult entity : result.getFullTextResult()) {
                fullTextResult.addResult(entity);
            }
            fullTextResult.setCount(fullTextResult.getResults().size());
        }
        String response = AtlasJson.toV1SearchJson(fullTextResult);
        return Response.ok(response).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingFullText({}, {}, {})", query, limit, offset);
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) FullTextSearchResult(org.apache.atlas.v1.model.discovery.FullTextSearchResult) QueryParams(org.apache.atlas.query.QueryParams) 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)

Example 14 with AtlasSearchResult

use of org.apache.atlas.model.discovery.AtlasSearchResult 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)

Example 15 with AtlasSearchResult

use of org.apache.atlas.model.discovery.AtlasSearchResult in project atlas by apache.

the class BasicSearchIT method testDiscoveryWithSearchParameters.

@Test(dataProvider = "basicSearchJSONNames")
public void testDiscoveryWithSearchParameters(String jsonFile) {
    try {
        BasicSearchParametersWithExpectation[] testExpectations = TestResourceFileUtils.readObjectFromJson(jsonFile, BasicSearchParametersWithExpectation[].class);
        assertNotNull(testExpectations);
        for (BasicSearchParametersWithExpectation testExpectation : testExpectations) {
            LOG.info("TestDescription  :{}", testExpectation.testDescription);
            LOG.info("SearchParameters :{}", testExpectation.searchParameters);
            AtlasSearchResult searchResult = atlasClientV2.facetedSearch(testExpectation.searchParameters);
            if (testExpectation.expectedCount > 0) {
                assertNotNull(searchResult.getEntities());
                assertEquals(searchResult.getEntities().size(), testExpectation.expectedCount);
            }
        }
    } catch (IOException | AtlasServiceException e) {
        fail(e.getMessage());
    }
}
Also used : AtlasServiceException(org.apache.atlas.AtlasServiceException) IOException(java.io.IOException) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Test(org.testng.annotations.Test)

Aggregations

AtlasSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult)30 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)13 Test (org.testng.annotations.Test)13 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)9 QueryParams (org.apache.atlas.query.QueryParams)8 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)7 AtlasFullTextResult (org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult)6 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)5 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)5 AttributeSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult.AttributeSearchResult)4 ScriptEngine (javax.script.ScriptEngine)3 ScriptException (javax.script.ScriptException)3 AtlasServiceException (org.apache.atlas.AtlasServiceException)3 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)3 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)2 IOException (java.io.IOException)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2