2828import org .springframework .data .elasticsearch .annotations .FieldType ;
2929import org .springframework .data .elasticsearch .client .elc .NativeQuery ;
3030import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
31+ import org .springframework .data .elasticsearch .core .geo .GeoPoint ;
3132import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
3233import org .springframework .data .elasticsearch .junit .jupiter .SpringIntegrationTest ;
3334import 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