public SearchResponse doSearch(ActivityQuery query, SearchOptions options) { SearchRequestBuilder requestBuilder = getClient() .prepareSearch(ActivityIndexDefinition.INDEX) .setTypes(ActivityIndexDefinition.TYPE); requestBuilder.setFrom(options.getOffset()); requestBuilder.setSize(options.getLimit()); requestBuilder.addSort(ActivityIndexDefinition.FIELD_CREATED_AT, SortOrder.DESC); AndFilterBuilder filter = FilterBuilders.andFilter(); if (!query.getTypes().isEmpty()) { OrFilterBuilder typeFilter = FilterBuilders.orFilter(); for (String type : query.getTypes()) { typeFilter.add(FilterBuilders.termFilter(ActivityIndexDefinition.FIELD_TYPE, type)); } filter.add(typeFilter); } if (!query.getDataOrFilters().isEmpty()) { for (Map.Entry<String, Object> entry : query.getDataOrFilters().entrySet()) { OrFilterBuilder orFilter = FilterBuilders.orFilter(); orFilter.add( FilterBuilders.nestedFilter( ActivityIndexDefinition.FIELD_DETAILS, FilterBuilders.termFilter( ActivityIndexDefinition.FIELD_DETAILS + "." + entry.getKey(), entry.getValue()))); filter.add(orFilter); } } Date since = query.getSince(); if (since != null) { filter.add( FilterBuilders.rangeFilter(ActivityIndexDefinition.FIELD_CREATED_AT) .gt(since) .cache(false)); } Date to = query.getTo(); if (to != null) { filter.add( FilterBuilders.rangeFilter(ActivityIndexDefinition.FIELD_CREATED_AT).lt(to).cache(false)); } requestBuilder.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter)); return requestBuilder.get(); }
private OrFilterBuilder buildAndFilter(String name, List<String> values) { OrFilterBuilder filterBuilder = FilterBuilders.orFilter(); values.forEach( e -> { TermFilterBuilder termFilterBuilder = FilterBuilders.termFilter(name, e); filterBuilder.add(termFilterBuilder); }); return filterBuilder; }