private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) { if (searchQuery.getFilter() != null) { searchRequest.setPostFilter(searchQuery.getFilter()); } if (CollectionUtils.isNotEmpty(searchQuery.getElasticsearchSorts())) { for (SortBuilder sort : searchQuery.getElasticsearchSorts()) { searchRequest.addSort(sort); } } if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) { for (FacetRequest facetRequest : searchQuery.getFacets()) { FacetBuilder facet = facetRequest.getFacet(); if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) { facet.facetFilter(searchQuery.getFilter()); } searchRequest.addFacet(facet); } } if (searchQuery.getHighlightFields() != null) { for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) { searchRequest.addHighlightedField(highlightField); } } if (CollectionUtils.isNotEmpty(searchQuery.getAggregations())) { for (AbstractAggregationBuilder aggregationBuilder : searchQuery.getAggregations()) { searchRequest.addAggregation(aggregationBuilder); } } return searchRequest.setQuery(searchQuery.getQuery()).execute().actionGet(); }
@Override public String scan(SearchQuery searchQuery, long scrollTimeInMillis, boolean noFields) { Assert.notNull(searchQuery.getIndices(), "No index defined for Query"); Assert.notNull(searchQuery.getTypes(), "No type define for Query"); Assert.notNull(searchQuery.getPageable(), "Query.pageable is required for scan & scroll"); SearchRequestBuilder requestBuilder = client .prepareSearch(toArray(searchQuery.getIndices())) .setSearchType(SCAN) .setQuery(searchQuery.getQuery()) .setTypes(toArray(searchQuery.getTypes())) .setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)) .setFrom(0) .setSize(searchQuery.getPageable().getPageSize()); if (searchQuery.getFilter() != null) { requestBuilder.setPostFilter(searchQuery.getFilter()); } if (isNotEmpty(searchQuery.getFields())) { requestBuilder.addFields(toArray(searchQuery.getFields())); } if (noFields) { requestBuilder.setNoFields(); } return requestBuilder.execute().actionGet().getScrollId(); }
@Override public <T> List<String> queryForIds(SearchQuery query) { SearchRequestBuilder request = prepareSearch(query).setQuery(query.getQuery()).setNoFields(); if (query.getFilter() != null) { request.setPostFilter(query.getFilter()); } SearchResponse response = request.execute().actionGet(); return extractIds(response); }
@Override public <T> Page<T> queryForPage(CriteriaQuery criteriaQuery, Class<T> clazz) { QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria()); FilterBuilder elasticsearchFilter = new CriteriaFilterProcessor().createFilterFromCriteria(criteriaQuery.getCriteria()); SearchRequestBuilder searchRequestBuilder = prepareSearch(criteriaQuery, clazz); if (elasticsearchQuery != null) { searchRequestBuilder.setQuery(elasticsearchQuery); } else { searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); } if (criteriaQuery.getMinScore() > 0) { searchRequestBuilder.setMinScore(criteriaQuery.getMinScore()); } if (elasticsearchFilter != null) searchRequestBuilder.setPostFilter(elasticsearchFilter); SearchResponse response = searchRequestBuilder.execute().actionGet(); return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable()); }