Search in sources :

Example 1 with PluginDriverDTO

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

the class ReindexDatasourceConsumer method activate.

@Activate
void activate() {
    RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
    _disposable = _datasourceEventConsumer.datasourceUpdateEvents().flatMap(datasource -> _pluginDriverManagerClient.getPluginDriver(datasource.getDriverServiceName()).onErrorResume(throwable -> {
        if (_log.isErrorEnabled()) {
            _log.error(throwable.getMessage());
        }
        return Mono.empty();
    }).map(pluginDriverDTO -> Tuples.of(datasource, datasource.getTenantId() + "-" + pluginDriverDTO.getName() + "-data"))).filterWhen(t2 -> Mono.create(sink -> restHighLevelClient.indices().existsAsync(new GetIndexRequest(t2.getT2()), RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).flatMap(t2 -> Mono.<GetSettingsResponse>create(sink -> restHighLevelClient.indices().getSettingsAsync(new GetSettingsRequest().indices(t2.getT2()).names("index.creation_date"), RequestOptions.DEFAULT, new ReactorActionListener<>(sink))).map(response -> Tuples.of(t2.getT1(), t2.getT2(), Instant.ofEpochMilli(Long.parseLong(response.getSetting(t2.getT2(), "index.creation_date")))))).log().filter(t3 -> t3.getT1().getLastIngestionDate().isBefore(t3.getT3())).flatMap(t3 -> Mono.<AcknowledgedResponse>create(sink -> restHighLevelClient.indices().deleteAsync(new DeleteIndexRequest(t3.getT2()), RequestOptions.DEFAULT, new ReactorActionListener<>(sink)))).onErrorContinue((throwable, ignore) -> {
        if (_log.isErrorEnabled()) {
            _log.error(throwable.getMessage());
        }
    }).subscribe();
}
Also used : ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DatasourceEventConsumer(io.openk9.datasource.event.consumer.api.DatasourceEventConsumer) Logger(org.slf4j.Logger) Disposable(reactor.core.Disposable) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) Deactivate(org.osgi.service.component.annotations.Deactivate) LoggerFactory(org.slf4j.LoggerFactory) Tuples(reactor.util.function.Tuples) Mono(reactor.core.publisher.Mono) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Instant(java.time.Instant) Component(org.osgi.service.component.annotations.Component) RequestOptions(org.elasticsearch.client.RequestOptions) PluginDriverManagerClient(io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient) Activate(org.osgi.service.component.annotations.Activate) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) Reference(org.osgi.service.component.annotations.Reference) RestHighLevelClientProvider(io.openk9.search.client.api.RestHighLevelClientProvider) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with PluginDriverDTO

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

the class SupportedDatasourcesEndpoint method _mapToResponse.

private SupportedDatasourcesResponse _mapToResponse(Tuple2<Datasource, PluginDriverDTO> t2) {
    Datasource t1 = t2.getT1();
    PluginDriverDTO pluginDriver = t2.getT2();
    return SupportedDatasourcesResponse.of(t1.getName(), t1.getActive(), pluginDriver.getDocumentTypes().stream().map(documentTypeDTO -> SupportedDatasourcesDocumentType.of(documentTypeDTO.getName(), documentTypeDTO.getIcon())).collect(Collectors.toList()), SupportedDatasourcesDocumentType.of(pluginDriver.getDefaultDocumentType().getName(), pluginDriver.getDefaultDocumentType().getIcon()));
}
Also used : Datasource(io.openk9.model.Datasource) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO)

Example 3 with PluginDriverDTO

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

the class DateOrderQueryParser method apply.

@Override
public Mono<Consumer<BoolQueryBuilder>> apply(Context context) {
    return Mono.fromSupplier(() -> (bool) -> {
        List<PluginDriverDTO> pluginDriverDocumentTypeList = context.getPluginDriverDocumentTypeList();
        Iterator<String> iterator = pluginDriverDocumentTypeList.stream().map(PluginDriverDTO::getDocumentTypes).flatMap(Collection::stream).map(DocumentTypeDTO::getSearchKeywords).flatMap(Collection::stream).filter(SearchKeywordDTO::isDate).distinct().map(SearchKeywordDTO::getKeyword).iterator();
        if (iterator.hasNext()) {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.boost(_boost);
            while (iterator.hasNext()) {
                String keyword = iterator.next();
                FunctionScoreQueryBuilder.FilterFunctionBuilder filterFunctionBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.linearDecayFunction(keyword, null, _scale));
                boolQueryBuilder.should(QueryBuilders.functionScoreQuery(QueryBuilders.existsQuery(keyword), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { filterFunctionBuilder }));
            }
            bool.should(boolQueryBuilder);
        }
    });
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collection(java.util.Collection) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO)

Example 4 with PluginDriverDTO

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

the class DateQueryParser method _rangeQuery.

private void _rangeQuery(List<SearchToken> searchTokens, List<PluginDriverDTO> pluginDriverDocumentTypeList, BoolQueryBuilder bool) {
    if (searchTokens.isEmpty()) {
        return;
    }
    List<SearchKeywordToken> collect = pluginDriverDocumentTypeList.stream().map(PluginDriverDTO::getDocumentTypes).flatMap(Collection::stream).map(DocumentTypeDTO::getSearchKeywords).flatMap(Collection::stream).filter(SearchKeywordDTO::isDate).flatMap(searchKeywordDTO -> {
        for (SearchToken searchToken : searchTokens) {
            if (searchToken.getKeywordKey() != null && searchToken.getKeywordKey().equals(searchKeywordDTO.getKeyword())) {
                return Stream.of(SearchKeywordToken.of(searchKeywordDTO, searchToken));
            }
        }
        return Stream.empty();
    }).collect(Collectors.toList());
    if (!collect.isEmpty()) {
        BoolQueryBuilder internalBool = QueryBuilders.boolQuery();
        boolean flag = false;
        for (SearchKeywordToken searchKeywordToken : collect) {
            SearchToken searchToken = searchKeywordToken.getSearchToken();
            Map<String, Object> extra = searchToken.getExtra();
            if (extra == null) {
                String[] values = searchToken.getValues();
                String gte;
                String lte;
                if (values.length != 0) {
                    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(searchToken.getKeywordKey());
                    if (values.length == 1) {
                        gte = values[0];
                        rangeQueryBuilder.gte(gte);
                    } else if (values.length == 2) {
                        gte = values[0];
                        lte = values[1];
                        if (gte == null || gte.isBlank()) {
                            rangeQueryBuilder.lte(lte);
                        } else {
                            rangeQueryBuilder.gte(gte).lte(lte);
                        }
                    }
                    flag = true;
                    internalBool.should(rangeQueryBuilder);
                }
            } else {
                Object gte = extra.get("gte");
                Object lte = extra.get("lte");
                Object lt = extra.get("lt");
                Object gt = extra.get("gt");
                String format = (String) extra.get("format");
                String timeZone = (String) extra.get("time_zone");
                String relation = (String) extra.get("relation");
                RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(searchToken.getKeywordKey());
                if (gte != null) {
                    rangeQueryBuilder.gte(gte);
                }
                if (lte != null) {
                    rangeQueryBuilder.lte(lte);
                }
                if (gt != null) {
                    rangeQueryBuilder.gt(gt);
                }
                if (lt != null) {
                    rangeQueryBuilder.lt(lt);
                }
                if (format != null) {
                    rangeQueryBuilder.format(format);
                }
                if (timeZone != null) {
                    rangeQueryBuilder.timeZone(timeZone);
                }
                if (relation != null && (relation.equals("INTERSECTS") || relation.equals("CONTAINS") || relation.equals("WITHIN"))) {
                    rangeQueryBuilder.relation(relation);
                }
                flag = true;
                internalBool.should(rangeQueryBuilder);
            }
        }
        if (flag) {
            bool.must(internalBool);
        }
    }
}
Also used : PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Collection(java.util.Collection) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) Mono(reactor.core.publisher.Mono) QueryParser(io.openk9.search.api.query.QueryParser) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO) SearchToken(io.openk9.search.api.query.SearchToken) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Component(org.osgi.service.component.annotations.Component) List(java.util.List) Stream(java.util.stream.Stream) Builder(lombok.Builder) Map(java.util.Map) Data(lombok.Data) AllArgsConstructor(lombok.AllArgsConstructor) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchToken(io.openk9.search.api.query.SearchToken) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO)

Example 5 with PluginDriverDTO

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

the class DocumentTypesEndpoint method _mapToResponse.

private Object _mapToResponse(List<Tuple2<Datasource, PluginDriverDTO>> list) {
    Map<String, Collection<String>> response = new HashMap<>();
    for (Tuple2<Datasource, PluginDriverDTO> t2 : list) {
        PluginDriverDTO pluginDriver = t2.getT2();
        for (DocumentTypeDTO documentType : pluginDriver.getDocumentTypes()) {
            String name = documentType.getName();
            List<SearchKeywordDTO> searchKeywords = documentType.getSearchKeywords();
            List<String> keywords = searchKeywords.stream().map(SearchKeywordDTO::getKeyword).collect(Collectors.toList());
            Collection<String> prevKeywords = response.get(name);
            Set<String> combine = new HashSet<>(keywords);
            if (prevKeywords != null) {
                combine.addAll(prevKeywords);
            }
            response.put(name, combine);
        }
    }
    return response;
}
Also used : Datasource(io.openk9.model.Datasource) HashMap(java.util.HashMap) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO) Collection(java.util.Collection) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) HashSet(java.util.HashSet)

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