Search in sources :

Example 16 with PluginDriverDTO

use of io.openk9.plugin.driver.manager.model.PluginDriverDTO in project openk9 by smclab.

the class BaseSearchHTTPHandler method _toQuerySearchRequest.

private Mono<SearchResponse> _toQuerySearchRequest(Tenant tenant, List<Datasource> datasources, PluginDriverDTOList pdDTOList, SearchRequest searchRequest, HttpServerRequest httpRequest) {
    return Mono.defer(() -> {
        List<PluginDriverDTO> pluginDriverDTOList = pdDTOList.getPluginDriverDTOList();
        Map<String, List<SearchToken>> tokenTypeGroup = searchRequest.getSearchQuery().stream().collect(Collectors.groupingBy(SearchToken::getTokenType));
        List<SearchToken> datasource = tokenTypeGroup.get("DATASOURCE");
        Stream<PluginDriverDTO> documentTypeStream = pluginDriverDTOList.stream();
        if (datasource != null) {
            List<String> datasourceValues = datasource.stream().map(SearchToken::getValues).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
            documentTypeStream = documentTypeStream.filter(entry -> datasourceValues.contains(entry.getName()));
        }
        List<PluginDriverDTO> documentTypeList = documentTypeStream.collect(Collectors.toList());
        QueryParser queryParser = _queryParsers.stream().reduce(QueryParser.NOTHING, QueryParser::andThen);
        return queryParser.apply(createQueryParserContext(tenant, datasources, httpRequest, tokenTypeGroup, documentTypeList)).flatMap(boolQueryBuilderConsumer -> _search.flatMapSearch(factory -> {
            long tenantId = tenant.getTenantId();
            if (documentTypeList.isEmpty()) {
                return Mono.just(SearchUtil.EMPTY_SEARCH_REQUEST);
            }
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            boolQueryBuilderConsumer.accept(boolQuery);
            org.elasticsearch.action.search.SearchRequest elasticSearchQuery;
            if (datasource != null) {
                String[] indexNames = documentTypeList.stream().map(PluginDriverDTO::getName).distinct().toArray(String[]::new);
                elasticSearchQuery = factory.createSearchRequestData(tenantId, indexNames);
            } else {
                elasticSearchQuery = factory.createSearchRequestData(tenantId, "*");
            }
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.fetchSource(includeFields(), excludeFields());
            searchSourceBuilder.query(boolQuery);
            searchSourceBuilder.trackTotalHits(true);
            return customizeSearchSourceBuilderMono(tenant, datasources, searchRequest, documentTypeList, searchSourceBuilder, elasticSearchQuery);
        }));
    });
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) Arrays(java.util.Arrays) SearchUtil(io.openk9.search.client.api.util.SearchUtil) SearchHits(org.elasticsearch.search.SearchHits) PluginDriverDTOList(io.openk9.plugin.driver.manager.model.PluginDriverDTOList) LoggerFactory(org.slf4j.LoggerFactory) Tuples(reactor.util.function.Tuples) Search(io.openk9.search.client.api.Search) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) QueryParser(io.openk9.search.api.query.QueryParser) HttpResponseWriter(io.openk9.http.util.HttpResponseWriter) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) SearchToken(io.openk9.search.api.query.SearchToken) JsonFactory(io.openk9.json.api.JsonFactory) ArrayList(java.util.ArrayList) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) ReactorNettyUtils(io.openk9.reactor.netty.util.ReactorNettyUtils) Text(org.elasticsearch.common.text.Text) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) PluginDriverManagerClient(io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient) 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) Collection(java.util.Collection) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) DatasourceClient(io.openk9.datasource.client.api.DatasourceClient) Mono(reactor.core.publisher.Mono) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO) Collectors(java.util.stream.Collectors) TotalHits(org.apache.lucene.search.TotalHits) SearchRequest(io.openk9.search.api.query.SearchRequest) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) List(java.util.List) HttpHandler(io.openk9.http.web.HttpHandler) Stream(java.util.stream.Stream) SearchTokenizer(io.openk9.search.api.query.SearchTokenizer) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Response(io.openk9.search.query.internal.response.Response) SearchRequest(io.openk9.search.api.query.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryParser(io.openk9.search.api.query.QueryParser) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchToken(io.openk9.search.api.query.SearchToken) PluginDriverDTOList(io.openk9.plugin.driver.manager.model.PluginDriverDTOList) ArrayList(java.util.ArrayList) List(java.util.List) Arrays(java.util.Arrays) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO)

Example 17 with PluginDriverDTO

use of io.openk9.plugin.driver.manager.model.PluginDriverDTO in project openk9 by smclab.

the class BaseSearchHTTPHandler method customizeSearchSourceBuilder.

protected void customizeSearchSourceBuilder(Tenant tenant, List<Datasource> datasources, SearchRequest searchRequest, List<PluginDriverDTO> documentTypeList, SearchSourceBuilder searchSourceBuilder, org.elasticsearch.action.search.SearchRequest elasticSearchQuery) {
    int[] range = searchRequest.getRange();
    if (range != null) {
        searchSourceBuilder.from(range[0]);
        searchSourceBuilder.size(range[1]);
    }
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    documentTypeList.stream().map(PluginDriverDTO::getDocumentTypes).flatMap(Collection::stream).map(DocumentTypeDTO::getSearchKeywords).flatMap(Collection::stream).filter(SearchKeywordDTO::isText).map(SearchKeywordDTO::getKeyword).distinct().forEach(highlightBuilder::field);
    highlightBuilder.forceSource(true);
    highlightBuilder.tagsSchema("default");
    searchSourceBuilder.highlighter(highlightBuilder);
}
Also used : Collection(java.util.Collection) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO)

Example 18 with PluginDriverDTO

use of io.openk9.plugin.driver.manager.model.PluginDriverDTO 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 19 with PluginDriverDTO

use of io.openk9.plugin.driver.manager.model.PluginDriverDTO in project openk9 by smclab.

the class PluginDriverDTOServiceImpl method _findDocumentType.

private PluginDriverDTO _findDocumentType(PluginDriver pluginDriver) {
    String name = pluginDriver.getName();
    List<DocumentType> documentTypeList = _documentTypeProvider.getDocumentTypeList(name);
    DocumentType defaultDocumentType = _documentTypeProvider.getDefaultDocumentType(name);
    if (documentTypeList.isEmpty() && defaultDocumentType != null) {
        documentTypeList = List.of(defaultDocumentType);
    } else if (!documentTypeList.isEmpty() && defaultDocumentType == null) {
        defaultDocumentType = documentTypeList.get(0);
    }
    List<DocumentTypeDTO> documentTypeDTOS = documentTypeList.stream().map(documentType -> DocumentTypeDTO.of(documentType.getName(), documentType.getIcon(), _wrapSearchKeywords(documentType))).collect(Collectors.toList());
    return PluginDriverDTO.of(pluginDriver.getDriverServiceName(), pluginDriver.getName(), pluginDriver.schedulerEnabled(), documentTypeDTOS, defaultDocumentType == null ? null : DocumentTypeDTO.of(defaultDocumentType.getName(), defaultDocumentType.getIcon(), _wrapSearchKeywords(defaultDocumentType)));
}
Also used : PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) SearchKeyword(io.openk9.plugin.driver.manager.api.SearchKeyword) FieldBoostDTO(io.openk9.plugin.driver.manager.model.FieldBoostDTO) PluginDriverDTOList(io.openk9.plugin.driver.manager.model.PluginDriverDTOList) Collection(java.util.Collection) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) PluginDriverDTOService(io.openk9.plugin.driver.manager.api.PluginDriverDTOService) PluginDriverRegistry(io.openk9.plugin.driver.manager.api.PluginDriverRegistry) DocumentType(io.openk9.plugin.driver.manager.api.DocumentType) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO) Collectors(java.util.stream.Collectors) Component(org.osgi.service.component.annotations.Component) List(java.util.List) Optional(java.util.Optional) PluginDriver(io.openk9.plugin.driver.manager.api.PluginDriver) Reference(org.osgi.service.component.annotations.Reference) DocumentTypeProvider(io.openk9.plugin.driver.manager.api.DocumentTypeProvider) DocumentType(io.openk9.plugin.driver.manager.api.DocumentType) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO)

Example 20 with PluginDriverDTO

use of io.openk9.plugin.driver.manager.model.PluginDriverDTO in project openk9 by smclab.

the class BaseNerEnrichProcessor method process.

@Override
public Mono<ObjectNode> process(ObjectNode objectNode, DatasourceContext context, EnrichItem enrichItem, PluginDriverDTO pluginDriverName) {
    return Mono.defer(() -> {
        JsonNode datasourceConfiguration = _jsonFactory.fromJsonToJsonNode(enrichItem.getJsonConfig());
        if (!datasourceConfiguration.isObject()) {
            return Mono.error(new RuntimeException("jsonConfig must be an instance of ObjectNode " + datasourceConfiguration.toString()));
        }
        ObjectNode request = prepareRequestRawContent(objectNode, datasourceConfiguration.toObjectNode(), context, pluginDriverName);
        return Mono.from(_httpClient.request(getMethod(), getPath(), request.toString(), getHeaders())).retry(5).map(_jsonFactory::fromJsonToJsonNode).map(JsonNode::toObjectNode).map(jsonNodes -> {
            jsonNodes.put(Constants.TENANT_ID, context.getTenant().getTenantId());
            jsonNodes.put(Constants.DATASOURCE_ID, context.getDatasource().getDatasourceId());
            jsonNodes.put(Constants.CONTENT_ID, objectNode.get(Constants.CONTENT_ID));
            jsonNodes.put(Constants.RAW_CONTENT, objectNode.get(Constants.RAW_CONTENT));
            jsonNodes.put(Constants.INGESTION_ID, objectNode.get(Constants.INGESTION_ID));
            return jsonNodes;
        }).flatMap(this::_getEntityOrCreate).map(entities -> objectNode.set(entitiesField(), entities));
    });
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Aggregations

PluginDriverDTO (io.openk9.plugin.driver.manager.model.PluginDriverDTO)10 Collection (java.util.Collection)9 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)8 SearchKeywordDTO (io.openk9.plugin.driver.manager.model.SearchKeywordDTO)7 List (java.util.List)7 Map (java.util.Map)7 Component (org.osgi.service.component.annotations.Component)7 Mono (reactor.core.publisher.Mono)7 ObjectNode (io.openk9.json.api.ObjectNode)6 Datasource (io.openk9.model.Datasource)6 DocumentTypeDTO (io.openk9.plugin.driver.manager.model.DocumentTypeDTO)6 Collectors (java.util.stream.Collectors)6 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)6 JsonNode (io.openk9.json.api.JsonNode)5 PluginDriverManagerClient (io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient)5 QueryParser (io.openk9.search.api.query.QueryParser)5 HashMap (java.util.HashMap)5 DatasourceClient (io.openk9.datasource.client.api.DatasourceClient)4 RouterHandler (io.openk9.http.web.RouterHandler)4 JsonFactory (io.openk9.json.api.JsonFactory)4