Search in sources :

Example 21 with AtlasSearchResult

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

the class DSLQueriesTest method queryAssert.

private void queryAssert(String query, final int expected, final int limit, final int offset) throws AtlasBaseException {
    AtlasSearchResult searchResult = discoveryService.searchUsingDslQuery(query, limit, offset);
    assertSearchResult(searchResult, expected, query);
}
Also used : AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult)

Example 22 with AtlasSearchResult

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

the class EntityDiscoveryService method searchUsingFullTextQuery.

@Override
@GraphTransaction
public AtlasSearchResult searchUsingFullTextQuery(String fullTextQuery, boolean excludeDeletedEntities, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(fullTextQuery, AtlasQueryType.FULL_TEXT);
    QueryParams params = QueryParams.getNormalizedParams(limit, offset);
    AtlasIndexQuery idxQuery = toAtlasIndexQuery(fullTextQuery);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing Full text query: {}", fullTextQuery);
    }
    ret.setFullTextResult(getIndexQueryResults(idxQuery, params, excludeDeletedEntities));
    return ret;
}
Also used : QueryParams(org.apache.atlas.query.QueryParams) AtlasIndexQuery(org.apache.atlas.repository.graphdb.AtlasIndexQuery) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 23 with AtlasSearchResult

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

the class EntityDiscoveryService method searchRelatedEntities.

@Override
@GraphTransaction
public AtlasSearchResult searchRelatedEntities(String guid, String relation, String sortByAttributeName, SortOrder sortOrder, boolean excludeDeletedEntities, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(AtlasQueryType.RELATIONSHIP);
    if (StringUtils.isEmpty(guid) || StringUtils.isEmpty(relation)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "guid: '" + guid + "', relation: '" + relation + "'");
    }
    AtlasVertex entityVertex = entityRetriever.getEntityVertex(guid);
    String entityTypeName = GraphHelper.getTypeName(entityVertex);
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
    if (entityType == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_TYPE, entityTypeName, guid);
    }
    AtlasAttribute attribute = entityType.getAttribute(relation);
    if (attribute != null) {
        if (isRelationshipAttribute(attribute)) {
            relation = EDGE_LABEL_PREFIX + attribute.getQualifiedName();
        } else {
            throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_ATTRIBUTE, relation, attribute.getTypeName());
        }
    }
    if (StringUtils.isEmpty(sortByAttributeName)) {
        sortByAttributeName = DEFAULT_SORT_ATTRIBUTE_NAME;
    }
    AtlasAttribute sortByAttribute = entityType.getAttribute(sortByAttributeName);
    if (sortByAttribute == null) {
        sortByAttributeName = null;
        sortOrder = null;
    } else {
        sortByAttributeName = sortByAttribute.getQualifiedName();
        if (sortOrder == null) {
            sortOrder = ASCENDING;
        }
    }
    QueryParams params = QueryParams.getNormalizedParams(limit, offset);
    ScriptEngine scriptEngine = graph.getGremlinScriptEngine();
    Bindings bindings = scriptEngine.createBindings();
    Set<String> states = getEntityStates();
    String relatedEntitiesQuery = gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH);
    if (excludeDeletedEntities) {
        states.remove(DELETED.toString());
    }
    if (sortOrder == ASCENDING) {
        relatedEntitiesQuery += gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_ASCENDING_SORT);
        bindings.put("sortAttributeName", sortByAttributeName);
    } else if (sortOrder == DESCENDING) {
        relatedEntitiesQuery += gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_DESCENDING_SORT);
        bindings.put("sortAttributeName", sortByAttributeName);
    }
    relatedEntitiesQuery += gremlinQueryProvider.getQuery(TO_RANGE_LIST);
    bindings.put("g", graph);
    bindings.put("guid", guid);
    bindings.put("relation", relation);
    bindings.put("states", Collections.unmodifiableSet(states));
    bindings.put("startIdx", params.offset());
    bindings.put("endIdx", params.offset() + params.limit());
    try {
        Object result = graph.executeGremlinScript(scriptEngine, bindings, relatedEntitiesQuery, false);
        if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
            List<?> queryResult = (List) result;
            Object firstElement = queryResult.get(0);
            if (firstElement instanceof AtlasVertex) {
                List<AtlasVertex> vertices = (List<AtlasVertex>) queryResult;
                List<AtlasEntityHeader> resultList = new ArrayList<>(vertices.size());
                for (AtlasVertex vertex : vertices) {
                    resultList.add(entityRetriever.toAtlasEntityHeader(vertex));
                }
                ret.setEntities(resultList);
            }
        }
        if (ret.getEntities() == null) {
            ret.setEntities(new ArrayList<>());
        }
    } catch (ScriptException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Gremlin script execution failed for relationship search query: {}", relatedEntitiesQuery, e);
        }
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "Relationship search query failed");
    } finally {
        graph.releaseGremlinScriptEngine(scriptEngine);
    }
    return ret;
}
Also used : Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 24 with AtlasSearchResult

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

the class EntityDiscoveryJerseyResourceIT method testSearchDSLLimits.

@Test
public void testSearchDSLLimits() throws Exception {
    String dslQuery = "from " + DATABASE_TYPE_BUILTIN + " " + QUALIFIED_NAME + "=\"" + dbName + "\"";
    AtlasSearchResult searchResult = atlasClientV2.dslSearch(dslQuery);
    assertNotNull(searchResult);
    // higher limit, all results returned
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, 10, 0);
    assertEquals(searchResult.getEntities().size(), 1);
    // default limit and offset -1, all results returned
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, -1, -1);
    assertEquals(searchResult.getEntities().size(), 1);
    // uses the limit parameter passed
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, 1, 0);
    assertEquals(searchResult.getEntities().size(), 1);
    // uses the offset parameter passed
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, 10, 1);
    assertNull(searchResult.getEntities());
    // limit > 0
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, 0, 10);
    assertNull(searchResult.getEntities());
    // limit > maxlimit
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, Integer.MAX_VALUE, 10);
    assertNull(searchResult.getEntities());
    // offset >= 0
    searchResult = atlasClientV2.dslSearchWithParams(dslQuery, 10, -2);
    assertEquals(searchResult.getEntities().size(), 1);
}
Also used : AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Test(org.testng.annotations.Test)

Example 25 with AtlasSearchResult

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

the class EntityDiscoveryJerseyResourceIT method testSearchFullTextOnDSLFailure.

@Test
public void testSearchFullTextOnDSLFailure() throws Exception {
    String query = "*";
    AtlasSearchResult searchResult = atlasClientV2.fullTextSearch(query);
    assertNotNull(searchResult);
    assertEquals(searchResult.getQueryText(), query);
    assertEquals(searchResult.getQueryType(), AtlasQueryType.FULL_TEXT);
}
Also used : 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