Search in sources :

Example 1 with FieldBoostDTO

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

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

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