Search in sources :

Example 6 with Response

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

the class EntitySearchHTTPHandler method _searchHitToResponse.

private Response _searchHitToResponse(SearchHits searchHits) {
    SearchHit[] hits = searchHits.getHits();
    List<Map<String, Object>> result = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        Map<String, Object> sourceMap = new HashMap<>(hit.getSourceAsMap());
        sourceMap.put("entityId", sourceMap.get("id"));
        result.add(sourceMap);
    }
    TotalHits totalHits = searchHits.getTotalHits();
    return new Response(result, totalHits.value);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(io.openk9.search.query.internal.response.Response) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Response

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

the class BaseSearchHTTPHandler method searchHitToResponse.

protected Object searchHitToResponse(SearchResponse searchResponse) {
    SearchHits hits = searchResponse.getHits();
    List<Map<String, Object>> result = new ArrayList<>();
    for (SearchHit hit : hits.getHits()) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        Map<String, Object> sourceMap = new HashMap<>(sourceAsMap.size() + 1, 1);
        sourceMap.putAll(sourceAsMap);
        sourceMap.put("id", hit.getId());
        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        Map<String, Object> highlightMap = new HashMap<>(highlightFields.size(), 1);
        for (HighlightField value : highlightFields.values()) {
            highlightMap.put(value.getName(), Arrays.stream(value.getFragments()).map(Text::string).toArray(String[]::new));
        }
        Map<String, Object> hitMap = new HashMap<>(2, 1);
        hitMap.put("source", sourceMap);
        hitMap.put("highlight", highlightMap);
        result.add(hitMap);
    }
    TotalHits totalHits = hits.getTotalHits();
    return new Response(result, totalHits.value);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(io.openk9.search.query.internal.response.Response) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Response

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

the class PluginDriverEndPoints method _findPluginDriverByName.

private Publisher<Void> _findPluginDriverByName(HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
    return Mono.defer(() -> {
        String serviceDriverName = httpRequest.param("serviceDriverName");
        PluginDriverDTO response = _pluginDriverDTOService.findPluginDriverDTOByName(serviceDriverName).orElseThrow(() -> new HttpException(404, "No Content. PluginDriver not found for serviceDriverName: " + serviceDriverName));
        return Mono.from(_httpResponseWriter.write(httpResponse, response));
    });
}
Also used : HttpException(io.openk9.http.exception.HttpException) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO)

Example 9 with Response

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

the class EntityManagerConsumer method consume.

@Incoming("entity-manager-request")
@Outgoing("entity-manager-response")
@Blocking
public Message<JsonObject> consume(Object obj) throws InterruptedException {
    JsonObject jsonObject = obj instanceof JsonObject ? (JsonObject) obj : new JsonObject(new String((byte[]) obj));
    Payload payload = jsonObject.mapTo(Payload.class);
    _entityManagerQueue.offer(payload, 45, TimeUnit.SECONDS);
    String replyTo = payload.getReplyTo();
    return Message.of(jsonObject, Metadata.of(new OutgoingRabbitMQMetadata.Builder().withRoutingKey(replyTo).withTimestamp(ZonedDateTime.now()).build()));
}
Also used : OutgoingRabbitMQMetadata(io.smallrye.reactive.messaging.rabbitmq.OutgoingRabbitMQMetadata) JsonObject(io.vertx.core.json.JsonObject) Payload(io.openk9.entity.manager.dto.Payload) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) Blocking(io.smallrye.reactive.messaging.annotations.Blocking) Outgoing(org.eclipse.microprofile.reactive.messaging.Outgoing)

Example 10 with Response

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

the class IndexWriterEndpoins method _deleteDataDocuments.

private Publisher<Void> _deleteDataDocuments(HttpServerRequest httpServerRequest, HttpServerResponse httpServerResponse) {
    RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
    Mono<byte[]> body = ReactorNettyUtils.aggregateBodyAsByteArray(httpServerRequest);
    Mono<String> responseMono = body.map(bytes -> _jsonFactory.fromJson(bytes, DeleteEntitiesRequest.class)).flatMap(deleteEntitiesRequest -> {
        if (!deleteEntitiesRequest.getContentIds().isEmpty()) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            for (String contentId : deleteEntitiesRequest.getContentIds()) {
                boolQuery.mustNot(matchQuery("contentId", contentId));
            }
            Mono<Datasource> datasourceMono = _datasourceClient.findDatasource(deleteEntitiesRequest.getDatasourceId());
            return datasourceMono.flatMap(datasource -> _pluginDriverManagerClient.getPluginDriver(datasource.getDriverServiceName()).map(pluginDriverDTO -> datasource.getTenantId() + "-" + pluginDriverDTO.getName() + "-data").map(DeleteByQueryRequest::new).map(deleteByQueryRequest -> deleteByQueryRequest.setQuery(boolQuery)).flatMap(deleteByQueryRequest -> Mono.<BulkByScrollResponse>create(sink -> restHighLevelClient.deleteByQueryAsync(deleteByQueryRequest, RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).map(Object::toString).doOnNext(_log::info).flatMap(response -> Mono.fromRunnable(() -> _sendCleanOrphanEntitiesRequest(datasource.getTenantId())).thenReturn(response)));
        }
        return Mono.empty();
    });
    return _httpResponseWriter.write(httpServerResponse, responseMono);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) JsonFactory(io.openk9.json.api.JsonFactory) DocumentEntityRequest(io.openk9.index.writer.entity.model.DocumentEntityRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ReactorNettyUtils(io.openk9.reactor.netty.util.ReactorNettyUtils) IndexRequest(org.elasticsearch.action.index.IndexRequest) EventBus(io.vertx.core.eventbus.EventBus) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) PluginDriverManagerClient(io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) CountRequest(org.elasticsearch.client.core.CountRequest) TimeValue(org.elasticsearch.core.TimeValue) SearchHit(org.elasticsearch.search.SearchHit) ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Message(io.vertx.core.eventbus.Message) DatasourceClient(io.openk9.datasource.client.api.DatasourceClient) Scroll(org.elasticsearch.search.Scroll) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) List(java.util.List) QueryBuilders.matchQuery(org.elasticsearch.index.query.QueryBuilders.matchQuery) Builder(lombok.Builder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RestHighLevelClientProvider(io.openk9.search.client.api.RestHighLevelClientProvider) HttpServerRoutes(reactor.netty.http.server.HttpServerRoutes) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) XContentType(org.elasticsearch.common.xcontent.XContentType) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) HttpResponseWriter(io.openk9.http.util.HttpResponseWriter) Function(java.util.function.Function) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) CountResponse(org.elasticsearch.client.core.CountResponse) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WriteRequest(org.elasticsearch.action.support.WriteRequest) Component(org.osgi.service.component.annotations.Component) Operator(org.elasticsearch.index.query.Operator) Cancellable(org.elasticsearch.client.Cancellable) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Activate(org.osgi.service.component.annotations.Activate) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) RouterHandler(io.openk9.http.web.RouterHandler) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) Datasource(io.openk9.model.Datasource) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Data(lombok.Data) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) AllArgsConstructor(lombok.AllArgsConstructor) Reference(org.osgi.service.component.annotations.Reference) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) NoArgsConstructor(lombok.NoArgsConstructor) Datasource(io.openk9.model.Datasource) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

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