private SearchRequestBuilder prepareSearch(Query query) { Assert.notNull(query.getIndices(), "No index defined for Query"); Assert.notNull(query.getTypes(), "No type defined for Query"); int startRecord = 0; SearchRequestBuilder searchRequestBuilder = client .prepareSearch(toArray(query.getIndices())) .setSearchType(query.getSearchType()) .setTypes(toArray(query.getTypes())); if (query.getPageable() != null) { startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize(); searchRequestBuilder.setSize(query.getPageable().getPageSize()); } searchRequestBuilder.setFrom(startRecord); if (!query.getFields().isEmpty()) { searchRequestBuilder.addFields(toArray(query.getFields())); } if (query.getSort() != null) { for (Sort.Order order : query.getSort()) { searchRequestBuilder.addSort( order.getProperty(), order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC); } } if (query.getMinScore() > 0) { searchRequestBuilder.setMinScore(query.getMinScore()); } return searchRequestBuilder; }
private <T> MultiGetResponse getMultiResponse(Query searchQuery, Class<T> clazz) { String indexName = isNotEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().get(0) : getPersistentEntityFor(clazz).getIndexName(); String type = isNotEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().get(0) : getPersistentEntityFor(clazz).getIndexType(); Assert.notNull(indexName, "No index defined for Query"); Assert.notNull(type, "No type define for Query"); Assert.notEmpty(searchQuery.getIds(), "No Id define for Query"); MultiGetRequestBuilder builder = client.prepareMultiGet(); for (String id : searchQuery.getIds()) { MultiGetRequest.Item item = new MultiGetRequest.Item(indexName, type, id); if (searchQuery.getRoute() != null) { item = item.routing(searchQuery.getRoute()); } if (searchQuery.getFields() != null && !searchQuery.getFields().isEmpty()) { item = item.fields(toArray(searchQuery.getFields())); } builder.add(item); } return builder.execute().actionGet(); }
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) { if (query.getIndices().isEmpty()) { query.addIndices(retrieveIndexNameFromPersistentEntity(clazz)); } if (query.getTypes().isEmpty()) { query.addTypes(retrieveTypeFromPersistentEntity(clazz)); } return prepareSearch(query); }