Skip to content

Commit 9a442df

Browse files
committed
Fix exists query for imperative repository implementation.
Original Pull Request #2236 Closes #2162 (cherry picked from commit 373be49) (cherry picked from commit be70a39)
1 parent 7b96041 commit 9a442df

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public Object execute(Object[] parameters) {
128128

129129
} else if (tree.isCountProjection()) {
130130
result = elasticsearchOperations.count(query, clazz, index);
131+
} else if (tree.isExistsProjection()) {
132+
long count = elasticsearchOperations.count(query, clazz, index);
133+
result = count > 0;
131134
} else {
132135
result = elasticsearchOperations.searchOne(query, clazz, index);
133136
}

src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ void shouldFindByNonEmptyProperty() {
321321
assertThat(searchHits.getTotalHits()).isEqualTo(5);
322322
}
323323

324+
@Test // #2162
325+
@DisplayName("should run exists query")
326+
void shouldRunExistsQuery() {
327+
328+
Boolean existsCaneSugar = repository.existsByText("Cane sugar");
329+
Boolean existsSand = repository.existsByText("Sand");
330+
331+
assertThat(existsCaneSugar).isTrue();
332+
assertThat(existsSand).isFalse();
333+
}
334+
324335
@SuppressWarnings("unused")
325336
@Document(indexName = "test-index-product-query-keywords")
326337
static class Product {
@@ -459,6 +470,8 @@ interface ProductRepository extends ElasticsearchRepository<Product, String> {
459470
SearchHits<Product> findByNameEmpty();
460471

461472
SearchHits<Product> findByNameNotEmpty();
473+
474+
Boolean existsByText(String text);
462475
}
463476

464477
}

src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/ReactiveQueryKeywordsIntegrationTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
2020

2121
import reactor.core.publisher.Flux;
22+
import reactor.core.publisher.Mono;
2223
import reactor.test.StepVerifier;
2324

25+
import java.lang.Boolean;
26+
2427
import org.junit.jupiter.api.BeforeEach;
2528
import org.junit.jupiter.api.DisplayName;
2629
import org.junit.jupiter.api.Order;
@@ -139,6 +142,21 @@ void shouldFindByNotEmptyProperty() {
139142
}).verifyComplete();
140143
}
141144

145+
@Test // #2162
146+
@DisplayName("should run exists query")
147+
void shouldRunExistsQuery() {
148+
149+
loadEntities();
150+
repository.existsByMessage("message") //
151+
.as(StepVerifier::create) //
152+
.expectNext(true) //
153+
.verifyComplete();
154+
repository.existsByMessage("without") //
155+
.as(StepVerifier::create) //
156+
.expectNext(false) //
157+
.verifyComplete();
158+
}
159+
142160
@SuppressWarnings("SpringDataMethodInconsistencyInspection")
143161
interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity, String> {
144162
Flux<SearchHit<SampleEntity>> findByMessageExists();
@@ -150,6 +168,8 @@ interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity,
150168
Flux<SearchHit<SampleEntity>> findByMessageIsNotEmpty();
151169

152170
Flux<SearchHit<SampleEntity>> findByMessageIsEmpty();
171+
172+
Mono<Boolean> existsByMessage(String message);
153173
}
154174

155175
private void loadEntities() {

0 commit comments

Comments
 (0)