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());
}
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;
}
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;
}
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;
}
}
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\""));
});
}
Aggregations