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);
}
}
}
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);
}
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);
});
}
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);
}
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);
}));
});
}
Aggregations