mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-15 08:32:18 +00:00
Fix exists query for imperative repository implementation.
Original Pull Request #2236 Closes #2162 (cherry picked from commit 373be49f97fe333714d29ce5e78ace38d7b0354f)
This commit is contained in:
parent
e3e666fd2e
commit
be70a398be
@ -128,6 +128,9 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
|
|||||||
|
|
||||||
} else if (tree.isCountProjection()) {
|
} else if (tree.isCountProjection()) {
|
||||||
result = elasticsearchOperations.count(query, clazz, index);
|
result = elasticsearchOperations.count(query, clazz, index);
|
||||||
|
} else if (tree.isExistsProjection()) {
|
||||||
|
long count = elasticsearchOperations.count(query, clazz, index);
|
||||||
|
result = count > 0;
|
||||||
} else {
|
} else {
|
||||||
result = elasticsearchOperations.searchOne(query, clazz, index);
|
result = elasticsearchOperations.searchOne(query, clazz, index);
|
||||||
}
|
}
|
||||||
|
@ -310,6 +310,17 @@ abstract class QueryKeywordsIntegrationTests {
|
|||||||
assertThat(searchHits.getTotalHits()).isEqualTo(5);
|
assertThat(searchHits.getTotalHits()).isEqualTo(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2162
|
||||||
|
@DisplayName("should run exists query")
|
||||||
|
void shouldRunExistsQuery() {
|
||||||
|
|
||||||
|
Boolean existsCaneSugar = repository.existsByText("Cane sugar");
|
||||||
|
Boolean existsSand = repository.existsByText("Sand");
|
||||||
|
|
||||||
|
assertThat(existsCaneSugar).isTrue();
|
||||||
|
assertThat(existsSand).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
||||||
static class Product {
|
static class Product {
|
||||||
@ -452,6 +463,8 @@ abstract class QueryKeywordsIntegrationTests {
|
|||||||
SearchHits<Product> findByNameEmpty();
|
SearchHits<Product> findByNameEmpty();
|
||||||
|
|
||||||
SearchHits<Product> findByNameNotEmpty();
|
SearchHits<Product> findByNameNotEmpty();
|
||||||
|
|
||||||
|
Boolean existsByText(String text);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,11 @@ import static org.assertj.core.api.Assertions.*;
|
|||||||
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import java.lang.Boolean;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Order;
|
import org.junit.jupiter.api.Order;
|
||||||
@ -121,6 +124,21 @@ public abstract class ReactiveQueryKeywordsIntegrationTests {
|
|||||||
}).verifyComplete();
|
}).verifyComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2162
|
||||||
|
@DisplayName("should run exists query")
|
||||||
|
void shouldRunExistsQuery() {
|
||||||
|
|
||||||
|
loadEntities();
|
||||||
|
repository.existsByMessage("message") //
|
||||||
|
.as(StepVerifier::create) //
|
||||||
|
.expectNext(true) //
|
||||||
|
.verifyComplete();
|
||||||
|
repository.existsByMessage("without") //
|
||||||
|
.as(StepVerifier::create) //
|
||||||
|
.expectNext(false) //
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("SpringDataMethodInconsistencyInspection")
|
@SuppressWarnings("SpringDataMethodInconsistencyInspection")
|
||||||
interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity, String> {
|
interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity, String> {
|
||||||
Flux<SearchHit<SampleEntity>> findByMessageExists();
|
Flux<SearchHit<SampleEntity>> findByMessageExists();
|
||||||
@ -132,6 +150,8 @@ public abstract class ReactiveQueryKeywordsIntegrationTests {
|
|||||||
Flux<SearchHit<SampleEntity>> findByMessageIsNotEmpty();
|
Flux<SearchHit<SampleEntity>> findByMessageIsNotEmpty();
|
||||||
|
|
||||||
Flux<SearchHit<SampleEntity>> findByMessageIsEmpty();
|
Flux<SearchHit<SampleEntity>> findByMessageIsEmpty();
|
||||||
|
|
||||||
|
Mono<Boolean> existsByMessage(String message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadEntities() {
|
private void loadEntities() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user