Skip to content

Fix exists query for imperative repository implementation. #2236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2022
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 @@ -128,6 +128,9 @@ public Object execute(Object[] parameters) {

} else if (tree.isCountProjection()) {
result = elasticsearchOperations.count(query, clazz, index);
} else if (tree.isExistsProjection()) {
long count = elasticsearchOperations.count(query, clazz, index);
result = count > 0;
} else {
result = elasticsearchOperations.searchOne(query, clazz, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ void shouldFindByNonEmptyProperty() {
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")
@Document(indexName = "#{@indexNameProvider.indexName()}")
static class Product {
Expand Down Expand Up @@ -452,6 +463,8 @@ interface ProductRepository extends ElasticsearchRepository<Product, String> {
SearchHits<Product> findByNameEmpty();

SearchHits<Product> findByNameNotEmpty();

Boolean existsByText(String text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import static org.springframework.data.elasticsearch.annotations.FieldType.*;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.lang.Boolean;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
Expand Down Expand Up @@ -121,6 +124,21 @@ void shouldFindByNotEmptyProperty() {
}).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")
interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity, String> {
Flux<SearchHit<SampleEntity>> findByMessageExists();
Expand All @@ -132,6 +150,8 @@ interface SampleRepository extends ReactiveElasticsearchRepository<SampleEntity,
Flux<SearchHit<SampleEntity>> findByMessageIsNotEmpty();

Flux<SearchHit<SampleEntity>> findByMessageIsEmpty();

Mono<Boolean> existsByMessage(String message);
}

private void loadEntities() {
Expand Down