Search in sources :

Example 6 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class JsEnrichProcessor method prepareRequestRawContent.

protected ObjectNode prepareRequestRawContent(ObjectNode objectNode, ObjectNode datasourceConfiguration, DatasourceContext context, PluginDriverDTO pluginDriverDTO) {
    JsonNode rawContentNode = objectNode.get(Constants.RAW_CONTENT);
    JsonNode codeNode = datasourceConfiguration.get(Constants.CODE);
    ObjectNode request = _jsonFactory.createObjectNode();
    request.put(Constants.CODE, codeNode);
    request.put(Constants.CONTENT, rawContentNode);
    JsonNode typeNode = objectNode.get(Constants.TYPE);
    ObjectNode datasourcePayload = _jsonFactory.createObjectNode();
    if (typeNode != null && typeNode.isArray()) {
        ArrayNode types = typeNode.toArrayNode();
        for (JsonNode typeJsonNode : types) {
            String type = typeJsonNode.asText();
            datasourcePayload.put(type, objectNode.get(type));
        }
    }
    request.put(Constants.DATASOURCE_PAYLOAD, datasourcePayload);
    request.put(Constants.TENANT_ID, context.getTenant().getTenantId());
    request.put(Constants.DATASOURCE_ID, context.getDatasource().getDatasourceId());
    request.put(Constants.CONTENT_ID, objectNode.get(Constants.CONTENT_ID));
    return request;
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode)

Example 7 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class InsertIndexWriter method _createDocWriterRequest.

private Mono<DocWriteRequest> _createDocWriterRequest(String indexName, ObjectNode enrichProcessorContext) {
    return Mono.defer(() -> {
        ObjectNode objectNode = enrichProcessorContext.get("payload").toObjectNode();
        String contentId = objectNode.get("contentId").asText();
        return _search.search(factory -> {
            SearchRequest searchRequest = new SearchRequest(indexName);
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("contentId", contentId);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(matchQueryBuilder);
            return searchRequest.source(searchSourceBuilder);
        }).onErrorReturn(SearchUtil.EMPTY_SEARCH_RESPONSE).filter(e -> e.getHits().getHits().length > 0).flatMapIterable(SearchResponse::getHits).next().map(e -> new UpdateRequest(indexName, e.getId()).doc(objectNode.toString(), XContentType.JSON)).cast(DocWriteRequest.class).switchIfEmpty(Mono.fromSupplier(() -> new IndexRequest(indexName).source(objectNode.toString(), XContentType.JSON)));
    });
}
Also used : MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) Logger(org.slf4j.Logger) Disposable(reactor.core.Disposable) SearchUtil(io.openk9.search.client.api.util.SearchUtil) XContentType(org.elasticsearch.common.xcontent.XContentType) IndexBus(io.openk9.search.client.api.IndexBus) Deactivate(org.osgi.service.component.annotations.Deactivate) LoggerFactory(org.slf4j.LoggerFactory) Search(io.openk9.search.client.api.Search) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Mono(reactor.core.publisher.Mono) SearchRequest(org.elasticsearch.action.search.SearchRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) JsonFactory(io.openk9.json.api.JsonFactory) IndexRequest(org.elasticsearch.action.index.IndexRequest) Component(org.osgi.service.component.annotations.Component) ReceiverReactor(io.openk9.ingestion.api.ReceiverReactor) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ObjectNode(io.openk9.json.api.ObjectNode) Activate(org.osgi.service.component.annotations.Activate) Reference(org.osgi.service.component.annotations.Reference) Binding(io.openk9.ingestion.api.Binding) SearchRequest(org.elasticsearch.action.search.SearchRequest) ObjectNode(io.openk9.json.api.ObjectNode) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class InsertIndexWriter method activate.

@Activate
void activate() {
    _disposable = _receiverReactor.consumeAutoAck(_binding.getQueue()).flatMap(delivery -> {
        ObjectNode enrichProcessorContext = _jsonFactory.fromJsonToJsonNode(delivery.getBody()).toObjectNode();
        String routingKey = delivery.getEnvelope().getRoutingKey();
        long tenantId = enrichProcessorContext.get("datasourceContext").get("tenant").get("tenantId").asLong();
        String pluginDriverName = enrichProcessorContext.get("pluginDriver").get("name").asText();
        String indexName = String.join("-", Long.toString(tenantId), pluginDriverName, "data");
        if (routingKey.endsWith("entity")) {
            return Mono.fromSupplier(() -> new IndexRequest(indexName).source(enrichProcessorContext.toString(), XContentType.JSON));
        }
        return _createDocWriterRequest(indexName, enrichProcessorContext);
    }).onErrorContinue(this::_manageExceptions).doOnNext(_indexBus::sendRequest).subscribe();
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) IndexRequest(org.elasticsearch.action.index.IndexRequest) Activate(org.osgi.service.component.annotations.Activate)

Example 9 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class EnrichPipelineProcessor method _mapToEnrichProcessorContext.

private ObjectNode _mapToEnrichProcessorContext(Tuple3<DatasourceContext, ObjectNode, PluginDriverDTO> t3) {
    List<String> dependencies = t3.getT1().getEnrichItems().stream().map(EnrichItem::getServiceName).collect(Collectors.toList());
    ObjectNode objectNode = _jsonFactory.createObjectNode();
    objectNode.put("pluginDriver", _jsonFactory.fromObjectToJsonNode(t3.getT3()));
    objectNode.put("dependencies", _jsonFactory.fromObjectToJsonNode(dependencies));
    objectNode.put("datasourceContext", _jsonFactory.fromObjectToJsonNode(t3.getT1()));
    objectNode.put("payload", t3.getT2());
    return objectNode;
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode)

Example 10 with ObjectNode

use of io.openk9.json.api.ObjectNode in project openk9 by smclab.

the class EnrichPipelineProcessor method _adaptIngestionPayload.

private Tuple3<DatasourceContext, ObjectNode, PluginDriverDTO> _adaptIngestionPayload(DatasourceContext datasourceContext, ObjectNode ingestionPayload, PluginDriverDTO pluginDriverDTO) {
    ObjectNode newIngestionPayload = ingestionPayload;
    if (newIngestionPayload.hasNonNull(io.openk9.core.api.constant.Constants.DATASOURCE_PAYLOAD)) {
        JsonNode datasourcePayload = newIngestionPayload.remove(io.openk9.core.api.constant.Constants.DATASOURCE_PAYLOAD);
        if (datasourcePayload.isObject()) {
            ObjectNode jsonNodes = datasourcePayload.toObjectNode();
            for (Map.Entry<String, JsonNode> field : jsonNodes.fields()) {
                newIngestionPayload.set(field.getKey(), field.getValue());
            }
        }
    }
    if (!ingestionPayload.hasNonNull(Constants.DATASOURCE_NAME)) {
        newIngestionPayload = newIngestionPayload.deepCopy().put(Constants.DATASOURCE_NAME, pluginDriverDTO.getName());
    }
    if (ingestionPayload.hasNonNull(io.openk9.core.api.constant.Constants.TYPE) && !ingestionPayload.get(io.openk9.core.api.constant.Constants.TYPE).toArrayNode().isEmpty()) {
        return Tuples.of(datasourceContext, ingestionPayload, pluginDriverDTO);
    }
    DocumentTypeDTO documentType = pluginDriverDTO.getDefaultDocumentType();
    return Tuples.of(datasourceContext, newIngestionPayload.set(io.openk9.core.api.constant.Constants.TYPE, _jsonFactory.createArrayNode().add(documentType.getName())), pluginDriverDTO);
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) Map(java.util.Map) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO)

Aggregations

ObjectNode (io.openk9.json.api.ObjectNode)12 JsonNode (io.openk9.json.api.JsonNode)9 ArrayNode (io.openk9.json.api.ArrayNode)6 JsonFactory (io.openk9.json.api.JsonFactory)5 Map (java.util.Map)5 Mono (reactor.core.publisher.Mono)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Activate (org.osgi.service.component.annotations.Activate)3 Component (org.osgi.service.component.annotations.Component)3 Reference (org.osgi.service.component.annotations.Reference)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Entity (io.openk9.entity.manager.model.Entity)2 Request (io.openk9.entity.manager.model.payload.Request)2 Response (io.openk9.entity.manager.model.payload.Response)2 HttpClient (io.openk9.http.client.HttpClient)2 Binding (io.openk9.ingestion.api.Binding)2 ReceiverReactor (io.openk9.ingestion.api.ReceiverReactor)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2