Search in sources :

Example 21 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class EntityDataServiceImpl method getRelationships.

@Override
public void getRelationships(RelationshipsQuery query, StreamObserver<EntityRelationship> responseObserver) {
    logQuery(query);
    Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    org.hypertrace.core.documentstore.Query docStoreQuery = new org.hypertrace.core.documentstore.Query();
    List<Filter> filters = new ArrayList<>();
    filters.add(DocStoreConverter.getTenantIdEqFilter(tenantId.get()));
    if (query.getEntityRelationshipCount() > 0) {
        filters.add(new Filter(Filter.Op.IN, EntityServiceConstants.ENTITY_RELATIONSHIP_TYPE, query.getEntityRelationshipList()));
    }
    if (query.getFromEntityIdCount() > 0) {
        filters.add(new Filter(Filter.Op.IN, EntityServiceConstants.FROM_ENTITY_ID, query.getFromEntityIdList()));
    }
    if (query.getToEntityIdCount() > 0) {
        filters.add(new Filter(Filter.Op.IN, EntityServiceConstants.TO_ENTITY_ID, query.getToEntityIdList()));
    }
    if (!filters.isEmpty()) {
        if (filters.size() == 1) {
            docStoreQuery.setFilter(filters.get(0));
        } else {
            Filter f = new Filter();
            f.setOp(Filter.Op.AND);
            f.setChildFilters(filters.toArray(new Filter[] {}));
            docStoreQuery.setFilter(f);
        }
    }
    searchByQueryAndStreamRelationships(docStoreQuery, responseObserver, tenantId.get());
}
Also used : ServiceException(com.google.protobuf.ServiceException) Query(org.hypertrace.entity.data.service.v1.Query) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList)

Example 22 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class EntityDataServiceImpl method buildExistingEntitiesInQuery.

private org.hypertrace.core.documentstore.Query buildExistingEntitiesInQuery(java.util.Collection<String> docIds) {
    org.hypertrace.core.documentstore.Query query = new org.hypertrace.core.documentstore.Query();
    query.setFilter(new Filter(Filter.Op.IN, EntityServiceConstants.ID, docIds));
    return query;
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) Filter(org.hypertrace.core.documentstore.Filter)

Example 23 with Filter

use of org.hypertrace.core.documentstore.Filter in project entity-service by hypertrace.

the class EntityDataServiceImpl method buildExistingEntityQuery.

private org.hypertrace.core.documentstore.Query buildExistingEntityQuery(String tenantId, String entityType, String entityId) {
    org.hypertrace.core.documentstore.Query query = new org.hypertrace.core.documentstore.Query();
    String docId = this.entityNormalizer.getEntityDocKey(tenantId, entityType, entityId).toString();
    query.setFilter(new Filter(Filter.Op.EQ, EntityServiceConstants.ID, docId));
    return query;
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) Filter(org.hypertrace.core.documentstore.Filter)

Example 24 with Filter

use of org.hypertrace.core.documentstore.Filter in project document-store by hypertrace.

the class MongoQueryParser method parseFilter.

static Map<String, Object> parseFilter(Filter filter) {
    if (filter.isComposite()) {
        Filter.Op op = filter.getOp();
        switch(op) {
            case OR:
            case AND:
                {
                    List<Map<String, Object>> childMapList = Arrays.stream(filter.getChildFilters()).map(MongoQueryParser::parseFilter).filter(map -> !map.isEmpty()).collect(Collectors.toList());
                    if (!childMapList.isEmpty()) {
                        return Map.of(PREFIX + op.name().toLowerCase(), childMapList);
                    } else {
                        return Collections.emptyMap();
                    }
                }
            default:
                throw new UnsupportedOperationException(String.format("Boolean operation:%s not supported", op));
        }
    } else {
        Filter.Op op = filter.getOp();
        Object value = filter.getValue();
        Map<String, Object> map = new HashMap<>();
        switch(op) {
            case EQ:
                map.put(filter.getFieldName(), value);
                break;
            case LIKE:
                // Case insensitive regex search
                map.put(filter.getFieldName(), new BasicDBObject("$regex", value).append("$options", "i"));
                break;
            case NOT_IN:
                map.put(filter.getFieldName(), new BasicDBObject("$nin", value));
                break;
            case IN:
                map.put(filter.getFieldName(), new BasicDBObject("$in", value));
                break;
            case CONTAINS:
                map.put(filter.getFieldName(), new BasicDBObject("$elemMatch", filter.getValue()));
                break;
            case GT:
                map.put(filter.getFieldName(), new BasicDBObject("$gt", value));
                break;
            case LT:
                map.put(filter.getFieldName(), new BasicDBObject("$lt", value));
                break;
            case GTE:
                map.put(filter.getFieldName(), new BasicDBObject("$gte", value));
                break;
            case LTE:
                map.put(filter.getFieldName(), new BasicDBObject("$lte", value));
                break;
            case EXISTS:
                map.put(filter.getFieldName(), new BasicDBObject("$exists", true));
                break;
            case NOT_EXISTS:
                map.put(filter.getFieldName(), new BasicDBObject("$exists", false));
                break;
            case NEQ:
                // $ne operator in Mongo also returns the results, where the key does not exist in the
                // document. This is as per semantics of EQ vs NEQ. So, if you need documents where
                // key exists, consumer needs to add additional filter.
                // https://github.com/hypertrace/document-store/pull/20#discussion_r547101520
                map.put(filter.getFieldName(), new BasicDBObject("$ne", value));
                break;
            case AND:
            case OR:
            default:
                throw new UnsupportedOperationException(UNSUPPORTED_QUERY_OPERATION);
        }
        return map;
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Filter(org.hypertrace.core.documentstore.Filter) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) List(java.util.List) BasicDBObject(com.mongodb.BasicDBObject)

Example 25 with Filter

use of org.hypertrace.core.documentstore.Filter in project document-store by hypertrace.

the class MongoDocStoreTest method testContains.

@Test
public void testContains() throws IOException {
    datastore.createCollection(COLLECTION_NAME, null);
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    // https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
    collection.upsert(new SingleValueKey("default", "testKey1"), Utils.createDocument(ImmutablePair.of("id", "testKey1"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 10), Map.of("product", "xyz", "score", 5)))));
    collection.upsert(new SingleValueKey("default", "testKey2"), Utils.createDocument(ImmutablePair.of("id", "testKey2"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 8), Map.of("product", "xyz", "score", 7)))));
    collection.upsert(new SingleValueKey("default", "testKey3"), Utils.createDocument(ImmutablePair.of("id", "testKey3"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 7), Map.of("product", "xyz", "score", 8)))));
    collection.upsert(new SingleValueKey("default", "testKey4"), Utils.createDocument(ImmutablePair.of("id", "testKey4"), ImmutablePair.of("products", List.of(Map.of("product", "abc", "score", 7), Map.of("product", "def", "score", 8)))));
    // try with contains filter
    Query query = new Query();
    Filter filter = new Filter(Op.CONTAINS, "products", Map.of("product", "xyz"));
    query.setFilter(filter);
    Iterator<Document> results = collection.search(query);
    List<Document> documents = new ArrayList<>();
    while (results.hasNext()) {
        documents.add(results.next());
    }
    Assertions.assertEquals(3, documents.size());
    documents.forEach(document -> {
        String jsonStr = document.toJson();
        assertTrue(jsonStr.contains("\"id\":\"testKey1\"") || document.toJson().contains("\"id\":\"testKey2\"") || document.toJson().contains("\"id\":\"testKey3\""));
    });
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Aggregations

Filter (org.hypertrace.core.documentstore.Filter)38 AttributeFilter (org.hypertrace.entity.data.service.v1.AttributeFilter)20 Test (org.junit.jupiter.api.Test)19 Query (org.hypertrace.entity.data.service.v1.Query)18 ArrayList (java.util.ArrayList)12 Map (java.util.Map)9 Query (org.hypertrace.core.documentstore.Query)9 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)7 Document (org.hypertrace.core.documentstore.Document)6 Collection (org.hypertrace.core.documentstore.Collection)5 MongoCollection (com.mongodb.client.MongoCollection)4 IOException (java.io.IOException)4 List (java.util.List)4 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)3 BasicDBObject (com.mongodb.BasicDBObject)3 Collections (java.util.Collections)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3