Search in sources :

Example 1 with Statement

use of org.neo4j.cypherdsl.core.Statement in project openk9 by smclab.

the class GetOrAddEntitiesConsumer method writeRelations.

public Mono<ResponseList> writeRelations(List<EntityContext> entityContext) {
    return Mono.defer(() -> {
        List<Statement> statementList = new ArrayList<>();
        for (EntityContext context : entityContext) {
            EntityRequest entityRequest = context.getEntityRequest();
            List<RelationRequest> relations = entityRequest.getRelations();
            if (relations == null || relations.isEmpty()) {
                continue;
            }
            Entity currentEntity = context.getEntity();
            List<Tuple2<String, Entity>> entityRelations = entityContext.stream().flatMap(entry -> {
                for (RelationRequest relation : relations) {
                    if (entry.getEntityRequest().getTmpId() == relation.getTo()) {
                        return Stream.of(Tuples.of(relation.getName(), entry.getEntity()));
                    }
                }
                return Stream.empty();
            }).collect(Collectors.toList());
            Node currentEntityNode = Cypher.node(currentEntity.getType()).named("a");
            List<Statement> currentStatementList = entityRelations.stream().map(t2 -> {
                Entity entityRelation = t2.getT2();
                Node entityRelationNode = Cypher.node(entityRelation.getType()).named("b");
                return Cypher.match(currentEntityNode, entityRelationNode).where(Functions.id(currentEntityNode).eq(literalOf(currentEntity.getId())).and(Functions.id(entityRelationNode).eq(literalOf(entityRelation.getId())))).merge(currentEntityNode.relationshipTo(entityRelationNode, t2.getT1())).build();
            }).collect(Collectors.toList());
            statementList.addAll(currentStatementList);
        }
        List<Response> response = entityContext.stream().map(context -> Response.builder().entity(Entity.builder().name(context.getEntity().getName()).id(context.getEntity().getId()).tenantId(context.getEntity().getTenantId()).type(context.getEntity().getType()).build()).tmpId(context.getEntityRequest().getTmpId()).build()).collect(Collectors.toList());
        if (statementList.size() > 1) {
            return _graphClient.write(Cypher.unionAll(statementList.toArray(new Statement[0]))).then(Mono.just(ResponseList.of("", response)));
        } else if (statementList.size() == 1) {
            return _graphClient.write(statementList.get(0)).then(Mono.just(ResponseList.of("", response)));
        } else {
            return Mono.just(ResponseList.of("", response));
        }
    });
}
Also used : Response(io.openk9.entity.manager.model.payload.Response) SenderReactor(io.openk9.ingestion.api.SenderReactor) Disposable(reactor.core.Disposable) JsonNode(io.openk9.json.api.JsonNode) LoggerFactory(org.slf4j.LoggerFactory) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) TimeoutException(java.util.concurrent.TimeoutException) EntityRequest(io.openk9.entity.manager.model.payload.EntityRequest) GraphClient(io.openk9.relationship.graph.api.client.GraphClient) ArrayNode(io.openk9.json.api.ArrayNode) JsonFactory(io.openk9.json.api.JsonFactory) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) Statement(org.neo4j.cypherdsl.core.Statement) Duration(java.time.Duration) OutboundMessageFactory(io.openk9.ingestion.api.OutboundMessageFactory) Schedulers(reactor.core.scheduler.Schedulers) Request(io.openk9.entity.manager.model.payload.Request) Cypher(org.neo4j.cypherdsl.core.Cypher) Activate(org.osgi.service.component.annotations.Activate) Node(org.neo4j.cypherdsl.core.Node) Binding(io.openk9.ingestion.api.Binding) Logger(org.slf4j.Logger) Deactivate(org.osgi.service.component.annotations.Deactivate) Functions(org.neo4j.cypherdsl.core.Functions) RelationRequest(io.openk9.entity.manager.model.payload.RelationRequest) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) ResponseList(io.openk9.entity.manager.model.payload.ResponseList) ReceiverReactor(io.openk9.ingestion.api.ReceiverReactor) Modified(org.osgi.service.component.annotations.Modified) Optional(java.util.Optional) ObjectNode(io.openk9.json.api.ObjectNode) Reference(org.osgi.service.component.annotations.Reference) Cypher.literalOf(org.neo4j.cypherdsl.core.Cypher.literalOf) Entity(io.openk9.entity.manager.model.Entity) Statement(org.neo4j.cypherdsl.core.Statement) RelationRequest(io.openk9.entity.manager.model.payload.RelationRequest) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode) Node(org.neo4j.cypherdsl.core.Node) ObjectNode(io.openk9.json.api.ObjectNode) ArrayList(java.util.ArrayList) Response(io.openk9.entity.manager.model.payload.Response) EntityRequest(io.openk9.entity.manager.model.payload.EntityRequest) Tuple2(reactor.util.function.Tuple2)

Example 2 with Statement

use of org.neo4j.cypherdsl.core.Statement 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;
}
Also used : Statement(org.neo4j.cypherdsl.core.Statement) Node(org.neo4j.cypherdsl.core.Node) EntityIndex(io.openk9.entity.manager.model.index.EntityIndex) SymbolicName(org.neo4j.cypherdsl.core.SymbolicName) Property(org.neo4j.cypherdsl.core.Property) AliasedExpression(org.neo4j.cypherdsl.core.AliasedExpression)

Example 3 with Statement

use of org.neo4j.cypherdsl.core.Statement 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);
        }
    })));
}
Also used : ReactorStopWatch(io.openk9.common.api.reactor.util.ReactorStopWatch) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) GraphClient(io.openk9.relationship.graph.api.client.GraphClient) JsonFactory(io.openk9.json.api.JsonFactory) DocumentEntityRequest(io.openk9.index.writer.entity.model.DocumentEntityRequest) SymbolicName(org.neo4j.cypherdsl.core.SymbolicName) IndexRequest(org.elasticsearch.action.index.IndexRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) Cypher(org.neo4j.cypherdsl.core.Cypher) IndexWriterEntityClient(io.openk9.index.writer.entity.client.api.IndexWriterEntityClient) ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) EntityNameCleanerProvider(io.openk9.entity.manager.api.EntityNameCleanerProvider) Collectors(java.util.stream.Collectors) List(java.util.List) Builder(lombok.Builder) Cypher.literalOf(org.neo4j.cypherdsl.core.Cypher.literalOf) RestHighLevelClientProvider(io.openk9.search.client.api.RestHighLevelClientProvider) XContentType(org.elasticsearch.common.xcontent.XContentType) MonoSink(reactor.core.publisher.MonoSink) SearchRequest(org.elasticsearch.action.search.SearchRequest) EntityGraphRepository(io.openk9.entity.manager.api.EntityGraphRepository) EntityRequest(io.openk9.entity.manager.model.payload.EntityRequest) Function(java.util.function.Function) WriteRequest(org.elasticsearch.action.support.WriteRequest) Component(org.osgi.service.component.annotations.Component) EntityNameCleaner(io.openk9.entity.manager.api.EntityNameCleaner) Statement(org.neo4j.cypherdsl.core.Statement) DocumentEntityResponse(io.openk9.index.writer.entity.model.DocumentEntityResponse) Cancellable(org.elasticsearch.client.Cancellable) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse) Activate(org.osgi.service.component.annotations.Activate) Node(org.neo4j.cypherdsl.core.Node) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) Functions(org.neo4j.cypherdsl.core.Functions) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Flux(reactor.core.publisher.Flux) AliasedExpression(org.neo4j.cypherdsl.core.AliasedExpression) Data(lombok.Data) Modified(org.osgi.service.component.annotations.Modified) AllArgsConstructor(lombok.AllArgsConstructor) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) NoArgsConstructor(lombok.NoArgsConstructor) Entity(io.openk9.entity.manager.model.Entity) Statement(org.neo4j.cypherdsl.core.Statement) DocumentEntityResponse(io.openk9.index.writer.entity.model.DocumentEntityResponse) Node(org.neo4j.cypherdsl.core.Node) SymbolicName(org.neo4j.cypherdsl.core.SymbolicName) AliasedExpression(org.neo4j.cypherdsl.core.AliasedExpression)

Example 4 with Statement

use of org.neo4j.cypherdsl.core.Statement in project openk9 by smclab.

the class GetOrAddEntitiesHttpHandler method writeRelations.

public Mono<ResponseList> writeRelations(List<EntityContext> entityContext) {
    return Mono.defer(() -> {
        List<Statement> statementList = new ArrayList<>();
        for (EntityContext context : entityContext) {
            EntityRequest entityRequest = context.getEntityRequest();
            List<RelationRequest> relations = entityRequest.getRelations();
            if (relations == null || relations.isEmpty()) {
                continue;
            }
            Entity currentEntity = context.getEntity();
            List<Tuple2<String, Entity>> entityRelations = entityContext.stream().flatMap(entry -> {
                for (RelationRequest relation : relations) {
                    if (entry.getEntityRequest().getTmpId() == relation.getTo()) {
                        return Stream.of(Tuples.of(relation.getName(), entry.getEntity()));
                    }
                }
                return Stream.empty();
            }).collect(Collectors.toList());
            Node currentEntityNode = Cypher.node(currentEntity.getType()).named("a");
            List<Statement> currentStatementList = entityRelations.stream().map(t2 -> {
                Entity entityRelation = t2.getT2();
                Node entityRelationNode = Cypher.node(entityRelation.getType()).named("b");
                return Cypher.match(currentEntityNode, entityRelationNode).where(Functions.id(currentEntityNode).eq(literalOf(currentEntity.getId())).and(Functions.id(entityRelationNode).eq(literalOf(entityRelation.getId())))).merge(currentEntityNode.relationshipTo(entityRelationNode, t2.getT1())).build();
            }).collect(Collectors.toList());
            statementList.addAll(currentStatementList);
        }
        List<Response> response = entityContext.stream().map(context -> Response.builder().entity(Entity.builder().name(context.getEntity().getName()).id(context.getEntity().getId()).tenantId(context.getEntity().getTenantId()).type(context.getEntity().getType()).build()).tmpId(context.getEntityRequest().getTmpId()).build()).collect(Collectors.toList());
        if (statementList.size() > 1) {
            return _graphClient.write(Cypher.unionAll(statementList.toArray(new Statement[0]))).then(Mono.just(ResponseList.of("", response)));
        } else if (statementList.size() == 1) {
            return _graphClient.write(statementList.get(0)).then(Mono.just(ResponseList.of("", response)));
        } else {
            return Mono.just(ResponseList.of("", response));
        }
    });
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) Response(io.openk9.entity.manager.model.payload.Response) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) HttpResponseWriter(io.openk9.http.util.HttpResponseWriter) EntityRequest(io.openk9.entity.manager.model.payload.EntityRequest) GraphClient(io.openk9.relationship.graph.api.client.GraphClient) JsonFactory(io.openk9.json.api.JsonFactory) ArrayList(java.util.ArrayList) ReactorNettyUtils(io.openk9.reactor.netty.util.ReactorNettyUtils) Component(org.osgi.service.component.annotations.Component) Statement(org.neo4j.cypherdsl.core.Statement) Request(io.openk9.entity.manager.model.payload.Request) Cypher(org.neo4j.cypherdsl.core.Cypher) Activate(org.osgi.service.component.annotations.Activate) Node(org.neo4j.cypherdsl.core.Node) RouterHandler(io.openk9.http.web.RouterHandler) Deactivate(org.osgi.service.component.annotations.Deactivate) Publisher(org.reactivestreams.Publisher) Functions(org.neo4j.cypherdsl.core.Functions) RelationRequest(io.openk9.entity.manager.model.payload.RelationRequest) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) List(java.util.List) HttpHandler(io.openk9.http.web.HttpHandler) Stream(java.util.stream.Stream) ResponseList(io.openk9.entity.manager.model.payload.ResponseList) Modified(org.osgi.service.component.annotations.Modified) Reference(org.osgi.service.component.annotations.Reference) Cypher.literalOf(org.neo4j.cypherdsl.core.Cypher.literalOf) HttpServerRoutes(reactor.netty.http.server.HttpServerRoutes) Entity(io.openk9.entity.manager.model.Entity) Statement(org.neo4j.cypherdsl.core.Statement) RelationRequest(io.openk9.entity.manager.model.payload.RelationRequest) Node(org.neo4j.cypherdsl.core.Node) ArrayList(java.util.ArrayList) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) Response(io.openk9.entity.manager.model.payload.Response) EntityRequest(io.openk9.entity.manager.model.payload.EntityRequest) Tuple2(reactor.util.function.Tuple2)

Example 5 with Statement

use of org.neo4j.cypherdsl.core.Statement in project openk9 by smclab.

the class EntityGraphRepositoryImpl method addEntity.

@Override
public Mono<Entity> addEntity(long tenantId, String entityName, String entityType) {
    Node entity = Cypher.node(entityType).named(Constants.ENTITY).withProperties(Constants.ENTITY_NAME_FIELD, literalOf(entityName), Constants.ENTITY_TENANT_ID_FIELD, literalOf(tenantId));
    Statement statement = Cypher.create(entity).returning(entity).build();
    return Mono.from(_graphClient.write(statement)).transform(this::_recordToEntity);
}
Also used : Statement(org.neo4j.cypherdsl.core.Statement) Node(org.neo4j.cypherdsl.core.Node)

Aggregations

Node (org.neo4j.cypherdsl.core.Node)8 Statement (org.neo4j.cypherdsl.core.Statement)8 Entity (io.openk9.entity.manager.model.Entity)4 GraphClient (io.openk9.relationship.graph.api.client.GraphClient)4 List (java.util.List)4 Cypher (org.neo4j.cypherdsl.core.Cypher)4 Cypher.literalOf (org.neo4j.cypherdsl.core.Cypher.literalOf)4 Functions (org.neo4j.cypherdsl.core.Functions)4 Component (org.osgi.service.component.annotations.Component)4 Reference (org.osgi.service.component.annotations.Reference)4 Flux (reactor.core.publisher.Flux)4 EntityRequest (io.openk9.entity.manager.model.payload.EntityRequest)3 JsonFactory (io.openk9.json.api.JsonFactory)3 Collectors (java.util.stream.Collectors)3 Property (org.neo4j.cypherdsl.core.Property)3 Activate (org.osgi.service.component.annotations.Activate)3 Modified (org.osgi.service.component.annotations.Modified)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Mono (reactor.core.publisher.Mono)3