17
17
18
18
import java .util .Collection ;
19
19
import java .util .Iterator ;
20
-
21
20
import org .springframework .dao .InvalidDataAccessApiUsageException ;
22
21
import org .springframework .data .domain .Sort ;
23
22
import org .springframework .data .elasticsearch .core .geo .GeoBox ;
@@ -63,8 +62,8 @@ public ElasticsearchQueryCreator(PartTree tree, MappingContext<?, ElasticsearchP
63
62
64
63
@ Override
65
64
protected CriteriaQuery create (Part part , Iterator <Object > iterator ) {
66
- PersistentPropertyPath <ElasticsearchPersistentProperty > path = context
67
- . getPersistentPropertyPath ( part .getProperty ());
65
+ PersistentPropertyPath <ElasticsearchPersistentProperty > path = context . getPersistentPropertyPath (
66
+ part .getProperty ());
68
67
return new CriteriaQuery (from (part ,
69
68
new Criteria (path .toDotPath (ElasticsearchPersistentProperty .QueryPropertyToFieldNameConverter .INSTANCE )),
70
69
iterator ));
@@ -75,8 +74,8 @@ protected CriteriaQuery and(Part part, CriteriaQuery base, Iterator<Object> iter
75
74
if (base == null ) {
76
75
return create (part , iterator );
77
76
}
78
- PersistentPropertyPath <ElasticsearchPersistentProperty > path = context
79
- . getPersistentPropertyPath ( part .getProperty ());
77
+ PersistentPropertyPath <ElasticsearchPersistentProperty > path = context . getPersistentPropertyPath (
78
+ part .getProperty ());
80
79
return base .addCriteria (from (part ,
81
80
new Criteria (path .toDotPath (ElasticsearchPersistentProperty .QueryPropertyToFieldNameConverter .INSTANCE )),
82
81
iterator ));
@@ -155,14 +154,7 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
155
154
secondParameter = parameters .next ();
156
155
}
157
156
158
- if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string )
159
- return criteria .within (geoPoint , string );
160
-
161
- if (firstParameter instanceof Point point && secondParameter instanceof Distance distance )
162
- return criteria .within (point , distance );
163
-
164
- if (firstParameter instanceof String firstString && secondParameter instanceof String secondString )
165
- return criteria .within (firstString , secondString );
157
+ return doWithinIfPossible (criteria , firstParameter , secondParameter );
166
158
}
167
159
case NEAR : {
168
160
Object firstParameter = parameters .next ();
@@ -177,15 +169,7 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
177
169
178
170
Object secondParameter = parameters .next ();
179
171
180
- // "near" query can be the same query as the "within" query
181
- if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string )
182
- return criteria .within (geoPoint , string );
183
-
184
- if (firstParameter instanceof Point point && secondParameter instanceof Distance distance )
185
- return criteria .within (point , distance );
186
-
187
- if (firstParameter instanceof String firstString && secondParameter instanceof String secondString )
188
- return criteria .within (firstString , secondString );
172
+ return doWithinIfPossible (criteria , firstParameter , secondParameter );
189
173
}
190
174
case EXISTS :
191
175
case IS_NOT_NULL :
@@ -201,6 +185,32 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
201
185
}
202
186
}
203
187
188
+ /**
189
+ * Do a within query if possible, otherwise return the criteria unchanged.
190
+ *
191
+ * @param criteria must not be {@literal null}
192
+ * @param firstParameter must not be {@literal null}
193
+ * @param secondParameter must not be {@literal null}
194
+ * @return the criteria with the within query applied if possible.
195
+ * @author Junghoon Ban
196
+ */
197
+ private Criteria doWithinIfPossible (Criteria criteria , Object firstParameter , Object secondParameter ) {
198
+
199
+ if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string ) {
200
+ return criteria .within (geoPoint , string );
201
+ }
202
+
203
+ if (firstParameter instanceof Point point && secondParameter instanceof Distance distance ) {
204
+ return criteria .within (point , distance );
205
+ }
206
+
207
+ if (firstParameter instanceof String firstString && secondParameter instanceof String secondString ) {
208
+ return criteria .within (firstString , secondString );
209
+ }
210
+
211
+ return criteria ;
212
+ }
213
+
204
214
private Object [] asArray (Object o ) {
205
215
if (o instanceof Collection ) {
206
216
return ((Collection <?>) o ).toArray ();
0 commit comments