28
28
import org .springframework .data .elasticsearch .annotations .FieldType ;
29
29
import org .springframework .data .elasticsearch .client .elc .NativeQuery ;
30
30
import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
31
+ import org .springframework .data .elasticsearch .core .geo .GeoPoint ;
31
32
import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
32
33
import org .springframework .data .elasticsearch .junit .jupiter .SpringIntegrationTest ;
33
34
import org .springframework .data .elasticsearch .utils .IndexNameProvider ;
@@ -50,7 +51,7 @@ public void before() {
50
51
@ Test
51
52
@ Order (java .lang .Integer .MAX_VALUE )
52
53
void cleanup () {
53
- operations .indexOps (IndexCoordinates .of (indexNameProvider .getPrefix () + "*" )).delete ();
54
+ operations .indexOps (IndexCoordinates .of (indexNameProvider .getPrefix () + '*' )).delete ();
54
55
}
55
56
56
57
@ Test // #2391
@@ -75,6 +76,28 @@ void shouldBeAbleToUseCriteriaQueryInANativeQuery() {
75
76
assertThat (searchHits .getSearchHit (0 ).getId ()).isEqualTo (entity .getId ());
76
77
}
77
78
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
+
78
101
@ Test // #2391
79
102
@ DisplayName ("should be able to use StringQuery in a NativeQuery" )
80
103
void shouldBeAbleToUseStringQueryInANativeQuery () {
@@ -112,6 +135,14 @@ void shouldBeAbleToUseStringQueryInANativeQuery() {
112
135
@ Document (indexName = "#{@indexNameProvider.indexName()}" )
113
136
static class SampleEntity {
114
137
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
+
115
146
@ Nullable
116
147
public String getId () {
117
148
return id ;
@@ -121,6 +152,7 @@ public void setId(@Nullable String id) {
121
152
this .id = id ;
122
153
}
123
154
155
+ @ Nullable
124
156
public String getText () {
125
157
return text ;
126
158
}
@@ -130,8 +162,12 @@ public void setText(String text) {
130
162
}
131
163
132
164
@ Nullable
133
- @ Id private String id ;
165
+ public GeoPoint getLocation () {
166
+ return location ;
167
+ }
134
168
135
- @ Field (type = FieldType .Text ) private String text ;
169
+ public void setLocation (GeoPoint location ) {
170
+ this .location = location ;
171
+ }
136
172
}
137
173
}
0 commit comments