use of io.openk9.entity.manager.cache.model.Entity in project openk9 by smclab.
the class CreateEntitiesRunnable method _disambiguate.
private Optional<EntityGraph> _disambiguate(EntityGraphService entityGraphService, List<EntityIndex> candidates, List<EntityIndex> entityRequestList, Entity currentEntityRequest, String[] uniqueEntities, int minHops, int maxHops) {
String currentEntityRequestType = currentEntityRequest.getType();
List<EntityGraph> result = List.of();
if (_containsValue(uniqueEntities, currentEntityRequestType)) {
if (candidates.size() == 1) {
if (_log.isDebugEnabled()) {
_log.debug("disambiguating entity with type " + currentEntityRequestType);
}
EntityIndex candidate = candidates.get(0);
EntityGraph entityGraph = EntityGraph.of(candidate.getId(), candidate.getGraphId(), candidate.getTenantId(), candidate.getName(), candidate.getType());
result = List.of(entityGraph);
} else if (candidates.size() > 1) {
result = _getEntityGraphs(entityGraphService, entityRequestList, minHops, maxHops, currentEntityRequestType, result);
}
} else {
if (!candidates.isEmpty()) {
result = _getEntityGraphs(entityGraphService, entityRequestList, minHops, maxHops, currentEntityRequestType, result);
}
}
if (_log.isDebugEnabled()) {
_log.debug("_disambiguate: " + result + " current: " + currentEntityRequest);
}
return result.stream().filter(Objects::nonNull).filter(entityGraph -> candidates.stream().anyMatch(entityIndex -> entityGraph.getId().equals(entityIndex.getId()))).findFirst();
}
use of io.openk9.entity.manager.cache.model.Entity in project openk9 by smclab.
the class CreateEntitiesRunnable method _getEntityGraphs.
private List<EntityGraph> _getEntityGraphs(EntityGraphService entityGraphService, List<EntityIndex> entityRequestList, int minHops, int maxHops, String currentEntityRequestType, List<EntityGraph> result) {
if (_log.isDebugEnabled()) {
_log.debug("disambiguating with search entity with type " + currentEntityRequestType);
}
Statement[] statements = new Statement[entityRequestList.size()];
for (int i = 0; i < entityRequestList.size(); i++) {
EntityIndex entityRequest = entityRequestList.get(i);
Node nodeEntity = Cypher.node(entityRequest.getType()).named("entity");
AliasedExpression entityAliased = nodeEntity.as("entity");
SymbolicName path = Cypher.name("path");
Property idProperty = entityAliased.getDelegate().property("id");
Statement statement = Cypher.match(nodeEntity).where(idProperty.eq(literalOf(entityRequest.getId()))).call("apoc.path.expand").withArgs(entityAliased.getDelegate(), literalOf(null), literalOf("-date"), literalOf(minHops), literalOf(maxHops)).yield(path).returning(Functions.last(Functions.nodes(path)).as("node"), Functions.size(Functions.nodes(path)).subtract(literalOf(1)).as("hops")).build();
statements[i] = statement;
}
if (statements.length == 1) {
Statement entityRequestListStatement = Cypher.call(statements[0]).returning("node", "hops").orderBy(Cypher.name("hops")).build();
result = entityGraphService.search(entityRequestListStatement);
} else if (statements.length > 1) {
Statement entityRequestListStatement = Cypher.call(Cypher.unionAll(statements)).returning("node", "hops").orderBy(Cypher.name("hops")).build();
result = entityGraphService.search(entityRequestListStatement);
}
return result;
}
use of io.openk9.entity.manager.cache.model.Entity in project openk9 by smclab.
the class EntityManagerBus method run.
@SneakyThrows
public void run() {
while (true) {
Payload request = _entityManagerQueue.take();
TransactionContext transactionContext = _hazelcastInstance.newTransactionContext();
transactionContext.beginTransaction();
try {
TransactionalMap<EntityKey, Entity> entityTransactionalMap = transactionContext.getMap("entityMap");
TransactionalMap<EntityRelationKey, EntityRelation> transactionalEntityRelationMap = transactionContext.getMap("entityRelationMap");
TransactionalMultiMap<DocumentKey, String> documentEntityMap = transactionContext.getMultiMap("documentEntityMap");
EntityManagerRequest payload = request.getPayload();
_loggerAggregator.emitLog("process ingestionId", payload.getIngestionId());
long tenantId = payload.getTenantId();
String ingestionId = payload.getIngestionId();
List<EntityRequest> entities = request.getEntities();
Map<EntityKey, Entity> localEntityMap = new HashMap<>(entities.size());
for (EntityRequest entityRequest : entities) {
String name = entityRequest.getName();
String type = entityRequest.getType();
String cacheId = Long.toString(_entityFlakeId.newId());
EntityKey key = EntityKey.of(tenantId, name, type, cacheId, ingestionId);
Entity entity = new Entity(null, cacheId, tenantId, name, type, null, ingestionId, false, true, entityRequest.getContext());
entityTransactionalMap.set(key, entity);
localEntityMap.put(key, entity);
for (EntityRequest entityRequest2 : entities) {
for (RelationRequest relation : entityRequest2.getRelations()) {
if (relation.getTo().equals(entityRequest.getTmpId())) {
relation.setTo(entity.getCacheId());
}
}
}
}
for (EntityRequest entity : entities) {
List<RelationRequest> relations = entity.getRelations();
if (relations == null || relations.isEmpty()) {
continue;
}
Collection<Entity> values = localEntityMap.values();
Entity current = values.stream().filter(e -> e.getName().equals(entity.getName()) && e.getType().equals(entity.getType())).findFirst().orElse(null);
if (current == null) {
continue;
}
for (RelationRequest relation : relations) {
String to = relation.getTo();
String name = relation.getName();
for (Entity value : values) {
if (value.getCacheId().equals(to)) {
long entityRelationId = _entityRelationFlakeId.newId();
EntityRelation entityRelation = new EntityRelation(entityRelationId, current.getCacheId(), ingestionId, name, value.getCacheId());
transactionalEntityRelationMap.set(EntityRelationKey.of(entityRelationId, current.getCacheId(), ingestionId), entityRelation);
}
}
}
}
if (!localEntityMap.isEmpty()) {
DocumentKey key = DocumentKey.of(payload.getDatasourceId(), payload.getContentId(), tenantId);
for (Entity value : localEntityMap.values()) {
documentEntityMap.put(key, value.getCacheId());
}
}
} catch (Exception e) {
_log.error(e.getMessage(), e);
transactionContext.rollbackTransaction();
} finally {
transactionContext.commitTransaction();
}
}
}
use of io.openk9.entity.manager.cache.model.Entity in project openk9 by smclab.
the class CreateDocumentEntitiesRunnable method run_.
@Override
public void run_() {
_log.info("start CreateEntitiesRunnable");
IMap<EntityKey, Entity> entityIMap = MapUtil.getEntityMap(_hazelcastInstance);
MultiMap<DocumentKey, String> documentEntityMapMap = MapUtil.getDocumentEntityMapMap(_hazelcastInstance);
EntityGraphService entityGraphService = CDI.current().select(EntityGraphService.class).get();
for (DocumentKey documentKey : documentEntityMapMap.localKeySet()) {
Collection<String> entityCacheIds = documentEntityMapMap.get(documentKey);
String[] arr = entityCacheIds.toArray(String[]::new);
Collection<Object> project = entityIMap.project(Projections.singleAttribute("indexable"), Predicates.and(Predicates.in("cacheId", arr), Predicates.notEqual("id", null), Predicates.notEqual("graphId", null)));
if (project.size() == entityCacheIds.size()) {
Collection<Entity> entities = entityIMap.values(Predicates.in("cacheId", arr));
DocumentGraph document = entityGraphService.insertDocument(DocumentGraph.of(null, documentKey.getDatasourceId(), documentKey.getTenantId(), documentKey.getContentId()));
for (Entity entity : entities) {
entityGraphService.createDocumentRelationship(entity.getId(), document.getId(), "related_to");
}
documentEntityMapMap.remove(documentKey);
}
}
}
use of io.openk9.entity.manager.cache.model.Entity in project openk9 by smclab.
the class CreateEntitiesRunnable method getEntityCandidates.
private EntityCandidates getEntityCandidates(EntityNameCleanerProvider entityNameCleanerProvider, EntityService entityService, EntityMember ingestionIdEntityMember, Entity ingestionIdEntity) {
EntityNameCleaner entityNameCleaner = entityNameCleanerProvider.get(ingestionIdEntity.getType());
QueryBuilder queryBuilder = entityNameCleaner.cleanEntityName(ingestionIdEntity.getTenantId(), ingestionIdEntity.getName());
List<EntityIndex> candidates = entityService.search(ingestionIdEntity.getTenantId(), queryBuilder, 0, 10);
return EntityCandidates.of(ingestionIdEntityMember, candidates);
}
Aggregations