Skip to content

Commit 310f0ed

Browse files
committed
Fix criteria filter in native query.
Original Pull Request #2846 Closes #2840 (cherry picked from commit e9ecebd)
1 parent dab0be0 commit 310f0ed

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,13 @@ private void addFilter(Query query, SearchRequest.Builder builder) {
16821682
} else // noinspection StatementWithEmptyBody
16831683
if (query instanceof StringQuery) {
16841684
// no filter for StringQuery
1685-
} else if (query instanceof NativeQuery) {
1686-
builder.postFilter(((NativeQuery) query).getFilter());
1685+
} else if (query instanceof NativeQuery nativeQuery) {
1686+
1687+
if (nativeQuery.getFilter() != null) {
1688+
builder.postFilter(nativeQuery.getFilter());
1689+
} else if (nativeQuery.getSpringDataQuery() != null) {
1690+
addFilter(nativeQuery.getSpringDataQuery(), builder);
1691+
}
16871692
} else {
16881693
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
16891694
}

src/test/java/org/springframework/data/elasticsearch/core/query/NativeQueryIntegrationTests.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.elasticsearch.annotations.FieldType;
2929
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
3030
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
31+
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3132
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3233
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
3334
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
@@ -50,7 +51,7 @@ public void before() {
5051
@Test
5152
@Order(java.lang.Integer.MAX_VALUE)
5253
void cleanup() {
53-
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
54+
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + '*')).delete();
5455
}
5556

5657
@Test // #2391
@@ -75,6 +76,28 @@ void shouldBeAbleToUseCriteriaQueryInANativeQuery() {
7576
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
7677
}
7778

79+
@Test // #2840
80+
@DisplayName("should be able to use CriteriaQuery with filter arguments in a NativeQuery")
81+
void shouldBeAbleToUseCriteriaQueryWithFilterArgumentsInANativeQuery() {
82+
var entity1 = new SampleEntity();
83+
entity1.setId("60");
84+
var location1 = new GeoPoint(60.0, 60.0);
85+
entity1.setLocation(location1);
86+
var entity2 = new SampleEntity();
87+
entity2.setId("70");
88+
var location70 = new GeoPoint(70.0, 70.0);
89+
entity2.setLocation(location70);
90+
operations.save(entity1, entity2);
91+
92+
var criteriaQuery = new CriteriaQuery(Criteria.where("location").within(location1, "10km"));
93+
var nativeQuery = NativeQuery.builder().withQuery(criteriaQuery).build();
94+
95+
var searchHits = operations.search(nativeQuery, SampleEntity.class);
96+
97+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
98+
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity1.getId());
99+
}
100+
78101
@Test // #2391
79102
@DisplayName("should be able to use StringQuery in a NativeQuery")
80103
void shouldBeAbleToUseStringQueryInANativeQuery() {
@@ -112,6 +135,14 @@ void shouldBeAbleToUseStringQueryInANativeQuery() {
112135
@Document(indexName = "#{@indexNameProvider.indexName()}")
113136
static class SampleEntity {
114137

138+
@Nullable
139+
@Id private String id;
140+
141+
@Nullable
142+
@Field(type = FieldType.Text) private String text;
143+
144+
@Nullable private GeoPoint location;
145+
115146
@Nullable
116147
public String getId() {
117148
return id;
@@ -121,6 +152,7 @@ public void setId(@Nullable String id) {
121152
this.id = id;
122153
}
123154

155+
@Nullable
124156
public String getText() {
125157
return text;
126158
}
@@ -130,8 +162,12 @@ public void setText(String text) {
130162
}
131163

132164
@Nullable
133-
@Id private String id;
165+
public GeoPoint getLocation() {
166+
return location;
167+
}
134168

135-
@Field(type = FieldType.Text) private String text;
169+
public void setLocation(GeoPoint location) {
170+
this.location = location;
171+
}
136172
}
137173
}

0 commit comments

Comments
 (0)