Search in sources :

Example 1 with SearchKeywordDTO

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

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

use of io.openk9.plugin.driver.manager.model.SearchKeywordDTO 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)

Example 4 with SearchKeywordDTO

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

the class TextQueryParser method _termQueryPrefixValues.

private void _termQueryPrefixValues(SearchToken tokenText, BoolQueryBuilder query, List<PluginDriverDTO> entityMapperList, QueryCondition queryCondition) {
    String[] values = tokenText.getValues();
    if (values.length == 0) {
        return;
    }
    String keywordKey = tokenText.getKeywordKey();
    boolean keywordKeyIsPresent = keywordKey != null && !keywordKey.isBlank();
    Predicate<SearchKeywordDTO> keywordKeyPredicate;
    if (keywordKeyIsPresent) {
        keywordKeyPredicate = searchKeyword -> searchKeyword.getKeyword().equals(keywordKey);
    } else {
        keywordKeyPredicate = ignore -> true;
    }
    if (queryCondition == QueryCondition.DEFAULT) {
        queryCondition = tokenText.getFilter() != null && tokenText.getFilter() ? QueryCondition.FILTER : QueryCondition.SHOULD;
    }
    Map<String, Float> keywordBoostMap = entityMapperList.stream().map(PluginDriverDTO::getDocumentTypes).flatMap(Collection::stream).map(DocumentTypeDTO::getSearchKeywords).flatMap(Collection::stream).filter(SearchKeywordDTO::isText).filter(keywordKeyPredicate).map(SearchKeywordDTO::getFieldBoost).collect(Collectors.toMap(FieldBoostDTO::getKeyword, FieldBoostDTO::getBoost, Math::max, HashMap::new));
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.boost(_boost);
    for (String value : values) {
        MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(value);
        multiMatchQueryBuilder.fields(keywordBoostMap);
        queryCondition.accept(boolQueryBuilder, multiMatchQueryBuilder);
        if (value.split("\\s+").length > 1) {
            multiMatchQueryBuilder = new MultiMatchQueryBuilder(value);
            multiMatchQueryBuilder.fields(keywordBoostMap);
            multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.PHRASE);
            multiMatchQueryBuilder.slop(2);
            multiMatchQueryBuilder.boost(2.0f);
            queryCondition.accept(boolQueryBuilder, multiMatchQueryBuilder);
        }
    }
    queryCondition.accept(query, boolQueryBuilder);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collection(java.util.Collection) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO)

Example 5 with SearchKeywordDTO

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

the class SearchAsYouTypeQueryParser method _termSearchAsYouTypeQueryValues.

private void _termSearchAsYouTypeQueryValues(SearchToken tokenText, BoolQueryBuilder query, List<PluginDriverDTO> entityMapperList) {
    String[] values = tokenText.getValues();
    if (values.length == 0) {
        return;
    }
    String keywordKey = tokenText.getKeywordKey();
    Predicate<SearchKeywordDTO> keywordKeyPredicate = searchKeyword -> keywordKey == null || keywordKey.isEmpty() || searchKeyword.getKeyword().equals(keywordKey);
    Map<String, Float> keywordBoostMap = entityMapperList.stream().map(PluginDriverDTO::getDocumentTypes).flatMap(Collection::stream).map(DocumentTypeDTO::getSearchKeywords).flatMap(Collection::stream).filter(SearchKeywordDTO::isAutocomplete).filter(keywordKeyPredicate).map(SearchKeywordDTO::getFieldBoost).collect(Collectors.toMap(FieldBoostDTO::getKeyword, FieldBoostDTO::getBoost, Math::max, HashMap::new));
    BoolQueryBuilder innerBoolQueryBuilder = QueryBuilders.boolQuery();
    for (String value : values) {
        MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(value);
        multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.BOOL_PREFIX);
        multiMatchQueryBuilder.fields(keywordBoostMap);
        innerBoolQueryBuilder.should(multiMatchQueryBuilder);
    }
    query.must(innerBoolQueryBuilder);
}
Also used : PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) FieldBoostDTO(io.openk9.plugin.driver.manager.model.FieldBoostDTO) Predicate(java.util.function.Predicate) Collection(java.util.Collection) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO) Mono(reactor.core.publisher.Mono) HashMap(java.util.HashMap) 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) Map(java.util.Map) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collection(java.util.Collection) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchKeywordDTO(io.openk9.plugin.driver.manager.model.SearchKeywordDTO)

Aggregations

Collection (java.util.Collection)6 SearchKeywordDTO (io.openk9.plugin.driver.manager.model.SearchKeywordDTO)5 PluginDriverDTO (io.openk9.plugin.driver.manager.model.PluginDriverDTO)4 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 DocumentTypeDTO (io.openk9.plugin.driver.manager.model.DocumentTypeDTO)3 QueryParser (io.openk9.search.api.query.QueryParser)2 SearchToken (io.openk9.search.api.query.SearchToken)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)2 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)2 Component (org.osgi.service.component.annotations.Component)2 Mono (reactor.core.publisher.Mono)2 Datasource (io.openk9.model.Datasource)1 FieldBoostDTO (io.openk9.plugin.driver.manager.model.FieldBoostDTO)1 HashSet (java.util.HashSet)1 Predicate (java.util.function.Predicate)1