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 ;
44
43
* @author Franck Marchand
45
44
* @author Artur Konczak
46
45
* @author Peter-Josef Meisch
46
+ * @author Junghoon Ban
47
47
*/
48
48
public class ElasticsearchQueryCreator extends AbstractQueryCreator <CriteriaQuery , CriteriaQuery > {
49
49
@@ -62,8 +62,8 @@ public ElasticsearchQueryCreator(PartTree tree, MappingContext<?, ElasticsearchP
62
62
63
63
@ Override
64
64
protected CriteriaQuery create (Part part , Iterator <Object > iterator ) {
65
- PersistentPropertyPath <ElasticsearchPersistentProperty > path = context
66
- . getPersistentPropertyPath ( part .getProperty ());
65
+ PersistentPropertyPath <ElasticsearchPersistentProperty > path = context . getPersistentPropertyPath (
66
+ part .getProperty ());
67
67
return new CriteriaQuery (from (part ,
68
68
new Criteria (path .toDotPath (ElasticsearchPersistentProperty .QueryPropertyToFieldNameConverter .INSTANCE )),
69
69
iterator ));
@@ -74,8 +74,8 @@ protected CriteriaQuery and(Part part, CriteriaQuery base, Iterator<Object> iter
74
74
if (base == null ) {
75
75
return create (part , iterator );
76
76
}
77
- PersistentPropertyPath <ElasticsearchPersistentProperty > path = context
78
- . getPersistentPropertyPath ( part .getProperty ());
77
+ PersistentPropertyPath <ElasticsearchPersistentProperty > path = context . getPersistentPropertyPath (
78
+ part .getProperty ());
79
79
return base .addCriteria (from (part ,
80
80
new Criteria (path .toDotPath (ElasticsearchPersistentProperty .QueryPropertyToFieldNameConverter .INSTANCE )),
81
81
iterator ));
@@ -109,31 +109,27 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
109
109
return criteria .is (parameters .next ()).not ();
110
110
case REGEX :
111
111
return criteria .expression (parameters .next ().toString ());
112
- case LIKE :
113
- case STARTING_WITH :
112
+ case LIKE , STARTING_WITH :
114
113
return criteria .startsWith (parameters .next ().toString ());
115
114
case ENDING_WITH :
116
115
return criteria .endsWith (parameters .next ().toString ());
117
116
case CONTAINING :
118
117
return criteria .contains (parameters .next ().toString ());
119
118
case GREATER_THAN :
120
119
return criteria .greaterThan (parameters .next ());
121
- case AFTER :
122
- case GREATER_THAN_EQUAL :
120
+ case AFTER , GREATER_THAN_EQUAL :
123
121
return criteria .greaterThanEqual (parameters .next ());
124
122
case LESS_THAN :
125
123
return criteria .lessThan (parameters .next ());
126
- case BEFORE :
127
- case LESS_THAN_EQUAL :
124
+ case BEFORE , LESS_THAN_EQUAL :
128
125
return criteria .lessThanEqual (parameters .next ());
129
126
case BETWEEN :
130
127
return criteria .between (parameters .next (), parameters .next ());
131
128
case IN :
132
129
return criteria .in (asArray (parameters .next ()));
133
130
case NOT_IN :
134
131
return criteria .notIn (asArray (parameters .next ()));
135
- case SIMPLE_PROPERTY :
136
- case WITHIN : {
132
+ case SIMPLE_PROPERTY , WITHIN : {
137
133
Object firstParameter = parameters .next ();
138
134
Object secondParameter = null ;
139
135
if (type == Part .Type .SIMPLE_PROPERTY ) {
@@ -154,40 +150,24 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
154
150
secondParameter = parameters .next ();
155
151
}
156
152
157
- if (firstParameter instanceof GeoPoint && secondParameter instanceof String )
158
- return criteria .within ((GeoPoint ) firstParameter , (String ) secondParameter );
159
-
160
- if (firstParameter instanceof Point && secondParameter instanceof Distance )
161
- return criteria .within ((Point ) firstParameter , (Distance ) secondParameter );
162
-
163
- if (firstParameter instanceof String && secondParameter instanceof String )
164
- return criteria .within ((String ) firstParameter , (String ) secondParameter );
153
+ return doWithinIfPossible (criteria , firstParameter , secondParameter );
165
154
}
166
155
case NEAR : {
167
156
Object firstParameter = parameters .next ();
168
157
169
- if (firstParameter instanceof GeoBox ) {
170
- return criteria .boundedBy (( GeoBox ) firstParameter );
158
+ if (firstParameter instanceof GeoBox geoBox ) {
159
+ return criteria .boundedBy (geoBox );
171
160
}
172
161
173
- if (firstParameter instanceof Box ) {
174
- return criteria .boundedBy (GeoBox .fromBox (( Box ) firstParameter ));
162
+ if (firstParameter instanceof Box box ) {
163
+ return criteria .boundedBy (GeoBox .fromBox (box ));
175
164
}
176
165
177
166
Object secondParameter = parameters .next ();
178
167
179
- // "near" query can be the same query as the "within" query
180
- if (firstParameter instanceof GeoPoint && secondParameter instanceof String )
181
- return criteria .within ((GeoPoint ) firstParameter , (String ) secondParameter );
182
-
183
- if (firstParameter instanceof Point && secondParameter instanceof Distance )
184
- return criteria .within ((Point ) firstParameter , (Distance ) secondParameter );
185
-
186
- if (firstParameter instanceof String && secondParameter instanceof String )
187
- return criteria .within ((String ) firstParameter , (String ) secondParameter );
168
+ return doWithinIfPossible (criteria , firstParameter , secondParameter );
188
169
}
189
- case EXISTS :
190
- case IS_NOT_NULL :
170
+ case EXISTS , IS_NOT_NULL :
191
171
return criteria .exists ();
192
172
case IS_NULL :
193
173
return criteria .not ().exists ();
@@ -200,6 +180,32 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
200
180
}
201
181
}
202
182
183
+ /**
184
+ * Do a within query if possible, otherwise return the criteria unchanged.
185
+ *
186
+ * @param criteria must not be {@literal null}
187
+ * @param firstParameter must not be {@literal null}
188
+ * @param secondParameter must not be {@literal null}
189
+ * @return the criteria with the within query applied if possible.
190
+ * @author Junghoon Ban
191
+ */
192
+ private Criteria doWithinIfPossible (Criteria criteria , Object firstParameter , Object secondParameter ) {
193
+
194
+ if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string ) {
195
+ return criteria .within (geoPoint , string );
196
+ }
197
+
198
+ if (firstParameter instanceof Point point && secondParameter instanceof Distance distance ) {
199
+ return criteria .within (point , distance );
200
+ }
201
+
202
+ if (firstParameter instanceof String firstString && secondParameter instanceof String secondString ) {
203
+ return criteria .within (firstString , secondString );
204
+ }
205
+
206
+ return criteria ;
207
+ }
208
+
203
209
private Object [] asArray (Object o ) {
204
210
if (o instanceof Collection ) {
205
211
return ((Collection <?>) o ).toArray ();
0 commit comments