Fix criteria filter in native query.

Original Pulle Request #2846
Closes #2840
This commit is contained in:
Peter-Josef Meisch 2024-02-06 20:56:41 +01:00 committed by GitHub
parent 9a3f5dc4f5
commit e9ecebd9ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 7 deletions

View File

@ -1169,8 +1169,7 @@ class RequestConverter {
}
return bb;
})
);
}));
});
return mtrb;
});
@ -1721,8 +1720,13 @@ class RequestConverter {
} else // noinspection StatementWithEmptyBody
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;
}
}
}