mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-30 16:52:11 +00:00
DATAES-934 - Add a Query taking method to ElasticsearchRepository.
Original PR: #535
This commit is contained in:
parent
83658121f3
commit
a5d9e929d9
@ -19,6 +19,7 @@ import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchPage;
|
||||
import org.springframework.data.elasticsearch.core.query.Query;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
@ -55,23 +56,32 @@ public interface ElasticsearchRepository<T, ID> extends PagingAndSortingReposito
|
||||
<S extends T> S indexWithoutRefresh(S entity);
|
||||
|
||||
/**
|
||||
* @deprecated since 4.0, use standard repository method naming or @{@link Query} annotated methods, or
|
||||
* {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
* @deprecated since 4.0, use {@link #searchQuery(Query)}, standard repository method naming or @{@link Query}
|
||||
* annotated methods, or {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
*/
|
||||
@Deprecated
|
||||
Iterable<T> search(QueryBuilder query);
|
||||
|
||||
/**
|
||||
* @deprecated since 4.0, use standard repository method naming or @{@link Query} annotated methods, or
|
||||
* {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
* @deprecated since 4.0, use {@link #searchQuery(Query)}, standard repository method naming or @{@link Query}
|
||||
* annotated methods, or {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
*/
|
||||
@Deprecated
|
||||
Page<T> search(QueryBuilder query, Pageable pageable);
|
||||
|
||||
/**
|
||||
* @deprecated since 4.0, use standard repository method naming or @{@link Query} annotated methods, or
|
||||
* {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
* @deprecated since 4.0, use {@link #searchQuery(Query)}, standard repository method naming or @{@link Query}
|
||||
* annotated methods, or {@link org.springframework.data.elasticsearch.core.ElasticsearchOperations}.
|
||||
*/
|
||||
Page<T> search(Query searchQuery);
|
||||
|
||||
/**
|
||||
* execute the given query and return the result in a SearchPage.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
SearchPage<T> searchQuery(Query query);
|
||||
|
||||
/**
|
||||
* Search for similar entities using a morelikethis query
|
||||
*
|
||||
|
@ -37,6 +37,7 @@ import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchHit;
|
||||
import org.springframework.data.elasticsearch.core.SearchHitSupport;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.SearchPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
@ -252,6 +253,12 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
return (Page<T>) SearchHitSupport.unwrapSearchHits(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchPage<T> searchQuery(Query query) {
|
||||
SearchHits<T> searchHits = execute(operations -> operations.search(query, entityClass, getIndexCoordinates()));
|
||||
return SearchHitSupport.searchPageFor(searchHits, query.getPageable());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Page<T> searchSimilar(T entity, @Nullable String[] fields, Pageable pageable) {
|
||||
|
@ -36,6 +36,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -51,6 +52,9 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchPage;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
|
||||
@ -71,8 +75,8 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
* @author Murali Chevuri
|
||||
*/
|
||||
@SpringIntegrationTest
|
||||
@ContextConfiguration(classes = { SimpleElasticsearchRepositoryTests.Config.class })
|
||||
public class SimpleElasticsearchRepositoryTests {
|
||||
@ContextConfiguration(classes = { SimpleElasticsearchRepositoryIntegrationTests.Config.class })
|
||||
public class SimpleElasticsearchRepositoryIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@Import({ ElasticsearchRestTemplateConfiguration.class })
|
||||
@ -714,6 +718,24 @@ public class SimpleElasticsearchRepositoryTests {
|
||||
.containsExactlyInAnyOrder("id-one", "id-two", "id-three");
|
||||
}
|
||||
|
||||
@Test // DATAES-934
|
||||
@DisplayName("should use query and return SearchPage")
|
||||
void shouldUseQueryAndReturnSearchPage() {
|
||||
|
||||
List<SampleEntity> entities = createSampleEntitiesWithMessage("test", 20);
|
||||
repository.saveAll(entities);
|
||||
|
||||
Criteria criteria = new Criteria("message").is("test");
|
||||
CriteriaQuery query = new CriteriaQuery(new Criteria("message").is("test"));
|
||||
query.setPageable(PageRequest.of(0, 8));
|
||||
|
||||
SearchPage<SampleEntity> searchPage = repository.searchQuery(query);
|
||||
|
||||
assertThat(searchPage.getTotalElements()).isEqualTo(20l);
|
||||
assertThat(searchPage.stream().count()).isEqualTo(8l);
|
||||
assertThat(searchPage.nextPageable().getOffset()).isEqualTo(8l);
|
||||
}
|
||||
|
||||
private static List<SampleEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
|
||||
|
||||
List<SampleEntity> sampleEntities = new ArrayList<>();
|
Loading…
x
Reference in New Issue
Block a user