Search in sources :

Example 1 with SearchToken

use of io.openk9.search.api.query.SearchToken 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 2 with SearchToken

use of io.openk9.search.api.query.SearchToken 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 3 with SearchToken

use of io.openk9.search.api.query.SearchToken in project openk9 by smclab.

the class EntityQueryParser method _createEntityQueryConsumer.

private Mono<Consumer<BoolQueryBuilder>> _createEntityQueryConsumer(Context context, List<SearchToken> searchTokens) {
    return Mono.fromSupplier(() -> bool -> {
        Map<String, List<SearchToken>> searchTokenGroupingByType = searchTokens.stream().collect(Collectors.groupingBy(searchToken -> searchToken.getEntityType() == null ? "" : searchToken.getEntityType()));
        BoolQueryBuilder outerBoolQueryBuilder = QueryBuilders.boolQuery();
        for (Map.Entry<String, List<SearchToken>> groupSearchTokens : searchTokenGroupingByType.entrySet()) {
            String type = groupSearchTokens.getKey();
            if (!type.isBlank()) {
                outerBoolQueryBuilder.must(QueryBuilders.matchQuery(ENTITIES_ENTITY_TYPE, type));
            }
            List<SearchToken> value = groupSearchTokens.getValue();
            String[] ids = value.stream().map(SearchToken::getValues).flatMap(Arrays::stream).distinct().toArray(String[]::new);
            if (ids.length != 0) {
                outerBoolQueryBuilder.must(_multiMatchValues(ENTITIES_ID, ids, _boost));
            }
            String[] keywordKeys = value.stream().map(SearchToken::getKeywordKey).filter(Objects::nonNull).toArray(String[]::new);
            if (keywordKeys.length != 0) {
                outerBoolQueryBuilder.must(_multiMatchValues(ENTITIES_CONTEXT, keywordKeys, 1.0f));
            }
        }
        context.getQueryCondition().accept(bool, outerBoolQueryBuilder);
    });
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Arrays(java.util.Arrays) Mono(reactor.core.publisher.Mono) QueryParser(io.openk9.search.api.query.QueryParser) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) SearchToken(io.openk9.search.api.query.SearchToken) Collectors(java.util.stream.Collectors) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Designate(org.osgi.service.metatype.annotations.Designate) Component(org.osgi.service.component.annotations.Component) List(java.util.List) ObjectClassDefinition(org.osgi.service.metatype.annotations.ObjectClassDefinition) Map(java.util.Map) Modified(org.osgi.service.component.annotations.Modified) Activate(org.osgi.service.component.annotations.Activate) ReferencePolicyOption(org.osgi.service.component.annotations.ReferencePolicyOption) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Reference(org.osgi.service.component.annotations.Reference) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchToken(io.openk9.search.api.query.SearchToken) List(java.util.List) Arrays(java.util.Arrays) Map(java.util.Map)

Example 4 with SearchToken

use of io.openk9.search.api.query.SearchToken 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)

Example 5 with SearchToken

use of io.openk9.search.api.query.SearchToken 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)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 SearchKeywordDTO (io.openk9.plugin.driver.manager.model.SearchKeywordDTO)4 QueryParser (io.openk9.search.api.query.QueryParser)4 SearchToken (io.openk9.search.api.query.SearchToken)4 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)4 Mono (reactor.core.publisher.Mono)4 DocumentTypeDTO (io.openk9.plugin.driver.manager.model.DocumentTypeDTO)3 PluginDriverDTO (io.openk9.plugin.driver.manager.model.PluginDriverDTO)3 Consumer (java.util.function.Consumer)3 Component (org.osgi.service.component.annotations.Component)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Stream (java.util.stream.Stream)2 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)2 DatasourceClient (io.openk9.datasource.client.api.DatasourceClient)1 HttpResponseWriter (io.openk9.http.util.HttpResponseWriter)1