Fix criteria filter in native query.

Original Pull Request #2846
Closes #2840

(cherry picked from commit e9ecebd9efbe852249e1b2afea4733bb6ea5daee)
(cherry picked from commit 310f0ed567f40e0721e436cb42af21482cfba9eb)
This commit is contained in:
Peter-Josef Meisch 2024-02-06 20:56:41 +01:00
parent 221317f914
commit c7ea83f58d
No known key found for this signature in database
GPG Key ID: DE108246970C7708
2 changed files with 46 additions and 5 deletions

View File

@ -1593,8 +1593,13 @@ class RequestConverter {
CriteriaFilterProcessor.createQuery(((CriteriaQuery) query).getCriteria()).ifPresent(builder::postFilter);
} else if (query instanceof StringQuery) {
// no filter for StringQuery
} else if (query instanceof NativeQuery) {
builder.postFilter(((NativeQuery) query).getFilter());
} else if (query instanceof NativeQuery nativeQuery) {
if (nativeQuery.getFilter() != null) {
builder.postFilter(nativeQuery.getFilter());
} else if (nativeQuery.getSpringDataQuery() != null) {
addFilter(nativeQuery.getSpringDataQuery(), builder);
}
} else {
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
}

View File

@ -28,6 +28,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
@ -50,7 +51,7 @@ public abstract class NativeQueryIntegrationTests {
@Test
@Order(java.lang.Integer.MAX_VALUE)
void cleanup() {
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + '*')).delete();
}
@Test // #2391
@ -75,6 +76,28 @@ public abstract class NativeQueryIntegrationTests {
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
}
@Test // #2840
@DisplayName("should be able to use CriteriaQuery with filter arguments in a NativeQuery")
void shouldBeAbleToUseCriteriaQueryWithFilterArgumentsInANativeQuery() {
var entity1 = new SampleEntity();
entity1.setId("60");
var location1 = new GeoPoint(60.0, 60.0);
entity1.setLocation(location1);
var entity2 = new SampleEntity();
entity2.setId("70");
var location70 = new GeoPoint(70.0, 70.0);
entity2.setLocation(location70);
operations.save(entity1, entity2);
var criteriaQuery = new CriteriaQuery(Criteria.where("location").within(location1, "10km"));
var nativeQuery = NativeQuery.builder().withQuery(criteriaQuery).build();
var searchHits = operations.search(nativeQuery, SampleEntity.class);
assertThat(searchHits.getTotalHits()).isEqualTo(1);
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity1.getId());
}
@Test // #2391
@DisplayName("should be able to use StringQuery in a NativeQuery")
void shouldBeAbleToUseStringQueryInANativeQuery() {
@ -112,6 +135,14 @@ public abstract class NativeQueryIntegrationTests {
@Document(indexName = "#{@indexNameProvider.indexName()}")
static class SampleEntity {
@Nullable
@Id private String id;
@Nullable
@Field(type = FieldType.Text) private String text;
@Nullable private GeoPoint location;
@Nullable
public String getId() {
return id;
@ -121,6 +152,7 @@ public abstract class NativeQueryIntegrationTests {
this.id = id;
}
@Nullable
public String getText() {
return text;
}
@ -130,8 +162,12 @@ public abstract class NativeQueryIntegrationTests {
}
@Nullable
@Id private String id;
public GeoPoint getLocation() {
return location;
}
@Field(type = FieldType.Text) private String text;
public void setLocation(GeoPoint location) {
this.location = location;
}
}
}