diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java index 1567b4bef..0f690a601 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java @@ -1169,8 +1169,7 @@ public MsearchTemplateRequest searchMsearchTemplateRequest( } return bb; - }) - ); + })); }); return mtrb; }); @@ -1721,8 +1720,13 @@ private void addFilter(Query query, SearchRequest.Builder builder) { } 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()); } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java b/src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java index 84a4d339f..46fa0958b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java @@ -28,6 +28,7 @@ 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 void before() { @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 @@ void shouldBeAbleToUseCriteriaQueryInANativeQuery() { 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 @@ void shouldBeAbleToUseStringQueryInANativeQuery() { @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 void setId(@Nullable String id) { this.id = id; } + @Nullable public String getText() { return text; } @@ -130,8 +162,12 @@ public void setText(String text) { } @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; + } } }