Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -55,23 +56,32 @@ default <S extends T> S index(S entity) {
<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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get actually rid of these methods entirely as that's an anti-pattern in Spring Data. See also https://jira.spring.io/browse/DATAMONGO-2610.


/**
* Search for similar entities using a morelikethis query
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -252,6 +253,12 @@ public Page<T> search(Query query) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

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;
Expand All @@ -51,6 +52,9 @@
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;
Expand All @@ -71,8 +75,8 @@
* @author Murali Chevuri
*/
@SpringIntegrationTest
@ContextConfiguration(classes = { SimpleElasticsearchRepositoryTests.Config.class })
public class SimpleElasticsearchRepositoryTests {
@ContextConfiguration(classes = { SimpleElasticsearchRepositoryIntegrationTests.Config.class })
public class SimpleElasticsearchRepositoryIntegrationTests {

@Configuration
@Import({ ElasticsearchRestTemplateConfiguration.class })
Expand Down Expand Up @@ -714,6 +718,24 @@ void shouldNotReturnNullValuesInFindAllById() throws IOException {
.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<>();
Expand Down