Skip to content

Commit e9ecebd

Browse files
authored
Fix criteria filter in native query.
Original Pulle Request #2846 Closes #2840
1 parent 9a3f5dc commit e9ecebd

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,7 @@ public MsearchTemplateRequest searchMsearchTemplateRequest(
11691169
}
11701170

11711171
return bb;
1172-
})
1173-
);
1172+
}));
11741173
});
11751174
return mtrb;
11761175
});
@@ -1721,8 +1720,13 @@ private void addFilter(Query query, SearchRequest.Builder builder) {
17211720
} else // noinspection StatementWithEmptyBody
17221721
if (query instanceof StringQuery) {
17231722
// no filter for StringQuery
1724-
} else if (query instanceof NativeQuery) {
1725-
builder.postFilter(((NativeQuery) query).getFilter());
1723+
} else if (query instanceof NativeQuery nativeQuery) {
1724+
1725+
if (nativeQuery.getFilter() != null) {
1726+
builder.postFilter(nativeQuery.getFilter());
1727+
} else if (nativeQuery.getSpringDataQuery() != null) {
1728+
addFilter(nativeQuery.getSpringDataQuery(), builder);
1729+
}
17261730
} else {
17271731
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
17281732
}

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)