Search in sources :

Example 11 with Response

use of io.openk9.entity.manager.model.payload.Response 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 12 with Response

use of io.openk9.entity.manager.model.payload.Response in project openk9 by smclab.

the class ResourcesHttpHandler method _sendResource.

private Mono<Void> _sendResource(Tenant tenant, Datasource datasource, PluginDriverDTO pluginDriverDTO, long datasourceId, String documentId, String resourceId, HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
    return _queryParser.apply(QueryParser.Context.of(tenant, null, null, null, httpRequest, QueryParser.QueryCondition.DEFAULT)).flatMap(consumer -> _search.search(factory -> {
        SearchRequest searchRequest = factory.createSearchRequestData(tenant.getTenantId(), pluginDriverDTO.getName());
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.idsQuery().addIds(documentId));
        boolQueryBuilder.must(QueryBuilders.nestedQuery(_RESOURCES_BINARIES, QueryBuilders.matchQuery(_RESOURCES_BINARIES_ID, resourceId), ScoreMode.Max));
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        consumer.accept(boolQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.fetchSource(new String[] { _RESOURCES_BINARIES_DATA, _RESOURCES_BINARIES_CONTENT_TYPE }, null);
        return searchRequest.source(searchSourceBuilder);
    })).flatMap(response -> {
        SearchHits searchHits = response.getHits();
        SearchHit[] hits = searchHits.getHits();
        if (hits.length == 0) {
            return httpResponse.sendNotFound();
        } else if (hits.length > 1) {
            _log.warn("found more than one resource (datasourceId: " + datasourceId + " resourceId: " + resourceId + " tenantId: " + tenant.getTenantId() + " documentId: " + documentId + ")");
        }
        SearchHit hit = hits[0];
        String source = hit.getSourceAsString();
        Resources resources = _jsonFactory.fromJson(source, Resources.class);
        ResourcesPayload resourcesPayload = resources.getResources();
        BinaryPayload binaryPayload = resourcesPayload.getBinaries().get(0);
        String data = binaryPayload.getData();
        String contentType = binaryPayload.getContentType();
        _manageCache(httpRequest, httpResponse);
        if (contentType != null && !contentType.isBlank()) {
            httpResponse.header(_CONTENT_TYPE, contentType);
        }
        byte[] decode = Base64.getDecoder().decode(data);
        return Mono.from(httpResponse.sendByteArray(Mono.just(decode)));
    });
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) HttpUtil(io.openk9.http.util.HttpUtil) SearchHits(org.elasticsearch.search.SearchHits) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Search(io.openk9.search.client.api.Search) QueryParser(io.openk9.search.api.query.QueryParser) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ResourcesPayload(io.openk9.model.ResourcesPayload) JsonFactory(io.openk9.json.api.JsonFactory) Component(org.osgi.service.component.annotations.Component) Map(java.util.Map) BinaryPayload(io.openk9.model.BinaryPayload) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) PluginDriverManagerClient(io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient) Activate(org.osgi.service.component.annotations.Activate) ZoneOffset(java.time.ZoneOffset) RouterHandler(io.openk9.http.web.RouterHandler) SearchHit(org.elasticsearch.search.SearchHit) Tenant(io.openk9.model.Tenant) Logger(org.slf4j.Logger) Datasource(io.openk9.model.Datasource) Publisher(org.reactivestreams.Publisher) DatasourceClient(io.openk9.datasource.client.api.DatasourceClient) Mono(reactor.core.publisher.Mono) ScoreMode(org.apache.lucene.search.join.ScoreMode) Instant(java.time.Instant) BundleContext(org.osgi.framework.BundleContext) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) Base64(java.util.Base64) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Builder(lombok.Builder) DateTimeFormatter(java.time.format.DateTimeFormatter) Data(lombok.Data) NumberUtils(org.apache.commons.lang3.math.NumberUtils) AllArgsConstructor(lombok.AllArgsConstructor) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Reference(org.osgi.service.component.annotations.Reference) HttpServerRoutes(reactor.netty.http.server.HttpServerRoutes) NoArgsConstructor(lombok.NoArgsConstructor) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) BinaryPayload(io.openk9.model.BinaryPayload) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ResourcesPayload(io.openk9.model.ResourcesPayload) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 13 with Response

use of io.openk9.entity.manager.model.payload.Response in project openk9 by smclab.

the class BaseNerEnrichProcessor method _getEntityOrCreate.

private Mono<JsonNode> _getEntityOrCreate(ObjectNode jsonNode) {
    return _entityManagerClient.getOrAddEntities(_jsonFactory.fromJsonNode(jsonNode, Request.class)).map(httpResponse -> {
        List<Response> responseList = httpResponse.getResponse();
        JsonNode entitiesJsonNode = jsonNode.get(entitiesField());
        ArrayNode entitiesArrayNode = entitiesJsonNode.toArrayNode();
        ArrayNode arrayNode = _jsonFactory.createArrayNode();
        for (JsonNode node : entitiesArrayNode) {
            Optional<Response> responseOptional = responseList.stream().filter(response -> node.get("tmpId").asLong() == response.getTmpId()).findFirst();
            if (responseOptional.isPresent()) {
                Entity entity = responseOptional.get().getEntity();
                ObjectNode result = _jsonFactory.createObjectNode();
                result.put("entityType", entity.getType());
                result.put("id", entity.getId());
                result.put("context", node.get("context"));
                arrayNode.add(result);
            }
        }
        return arrayNode;
    });
}
Also used : Response(io.openk9.entity.manager.model.payload.Response) EnrichItem(io.openk9.model.EnrichItem) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) Response(io.openk9.entity.manager.model.payload.Response) JsonNode(io.openk9.json.api.JsonNode) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) ArrayNode(io.openk9.json.api.ArrayNode) JsonFactory(io.openk9.json.api.JsonFactory) DatasourceContext(io.openk9.model.DatasourceContext) List(java.util.List) EntityManagerClient(io.openk9.entity.manager.client.api.EntityManagerClient) Map(java.util.Map) Optional(java.util.Optional) Request(io.openk9.entity.manager.model.payload.Request) ObjectNode(io.openk9.json.api.ObjectNode) HttpClient(io.openk9.http.client.HttpClient) Constants(io.openk9.core.api.constant.Constants) Entity(io.openk9.entity.manager.model.Entity) ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode)

Aggregations

ArrayList (java.util.ArrayList)7 Mono (reactor.core.publisher.Mono)7 JsonFactory (io.openk9.json.api.JsonFactory)6 List (java.util.List)6 Map (java.util.Map)6 Activate (org.osgi.service.component.annotations.Activate)6 Component (org.osgi.service.component.annotations.Component)6 Reference (org.osgi.service.component.annotations.Reference)6 HttpServerResponse (reactor.netty.http.server.HttpServerResponse)6 SearchHit (org.elasticsearch.search.SearchHit)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Entity (io.openk9.entity.manager.model.Entity)4 Request (io.openk9.entity.manager.model.payload.Request)4 Response (io.openk9.entity.manager.model.payload.Response)4 PluginDriverDTO (io.openk9.plugin.driver.manager.model.PluginDriverDTO)4 Collectors (java.util.stream.Collectors)4 EntityRequest (io.openk9.entity.manager.model.payload.EntityRequest)3 RelationRequest (io.openk9.entity.manager.model.payload.RelationRequest)3 RouterHandler (io.openk9.http.web.RouterHandler)3