use of io.openk9.search.client.api.Search in project openk9 by smclab.
the class GetOrAddEntities method _disambiguate.
private Mono<Entity> _disambiguate(List<DocumentEntityResponse> candidates, List<DocumentEntityResponse> entityRequestList, long tenantId, EntityRequest currentEntityRequest) {
Flux<Entity> entityFlux = Flux.empty();
String currentEntityRequestType = currentEntityRequest.getType();
if (_log.isDebugEnabled()) {
_log.debug(Arrays.toString(_uniqueEntities));
}
if (!candidates.isEmpty() && !_containsValue(_uniqueEntities, currentEntityRequestType)) {
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++) {
DocumentEntityResponse entityRequest = entityRequestList.get(i);
Node nodeEntity = Cypher.node(entityRequest.getType()).named(ENTITY);
AliasedExpression entityAliased = nodeEntity.as(ENTITY);
SymbolicName path = Cypher.name(PATH);
Statement statement = Cypher.match(nodeEntity).where(Functions.id(nodeEntity).eq(literalOf(entityRequest.getId()))).call(APOC_PATH_EXPAND).withArgs(entityAliased.getDelegate(), literalOf(null), literalOf(_labelFilter), 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();
entityFlux = _entityGraphRepository.getEntities(entityRequestListStatement);
} else if (statements.length > 1) {
Statement entityRequestListStatement = Cypher.call(Cypher.unionAll(statements)).returning(NODE, HOPS).orderBy(Cypher.name(HOPS)).build();
entityFlux = _entityGraphRepository.getEntities(entityRequestListStatement);
}
}
if (candidates.size() == 1 && _containsValue(_uniqueEntities, currentEntityRequestType)) {
if (_log.isDebugEnabled()) {
_log.debug("disambiguating entity with type " + currentEntityRequestType);
}
DocumentEntityResponse candidate = candidates.get(0);
entityFlux = _entityGraphRepository.getEntity(candidate.getId()).flux();
}
return entityFlux.filter(entity -> candidates.stream().anyMatch(entity1 -> entity1.getId() == entity.getId())).next().switchIfEmpty(Mono.defer(() -> _entityGraphRepository.addEntity(tenantId, currentEntityRequest.getName(), currentEntityRequest.getType()).flatMap(entity -> {
if (_containsValue(_notIndexEntities, entity.getType())) {
return Mono.just(entity);
} else {
return _insertEntity(DocumentEntityRequest.builder().tenantId(entity.getTenantId()).name(entity.getName()).type(entity.getType()).id(entity.getId()).build()).thenReturn(entity);
}
})));
}
Aggregations