50
50
import java .util .Set ;
51
51
import java .util .logging .Level ;
52
52
import java .util .logging .Logger ;
53
+ import java .util .regex .Pattern ;
53
54
54
55
/**
55
56
* Used for parsing the text of a query for which suggestions should be retrieved. Decouples the query into 2 parts:
@@ -102,11 +103,19 @@ protected Query newTermQuery(final Term term) {
102
103
103
104
private Term replaceIdentifier (final Term term , final String identifier ) {
104
105
Term newTerm = new Term (term .field (), term .text ().replace (identifier , "" ));
105
- this .identifier = term .text ();
106
-
106
+ replaceIdentifier (term .field (), term .text ());
107
107
return newTerm ;
108
108
}
109
109
110
+ private void replaceIdentifier (final String field , final String text ) {
111
+ this .identifier = text ;
112
+ if (!isCaseSensitive (field )) {
113
+ // fixes problem when prefix contains upper case chars for case insensitive field
114
+ queryTextWithPlaceholder = queryTextWithPlaceholder .replaceAll (
115
+ "(?i)" + Pattern .quote (identifier ), identifier );
116
+ }
117
+ }
118
+
110
119
@ Override
111
120
protected Query getBooleanQuery (final List <BooleanClause > clauses ) throws ParseException {
112
121
@@ -187,14 +196,14 @@ protected Query newWildcardQuery(final Term t) {
187
196
if (t .text ().contains (identifier )) {
188
197
String term = t .text ().replace (identifier , "" );
189
198
if (term .endsWith ("*" )) {
190
- identifier = t .text ();
199
+ replaceIdentifier ( t . field (), t .text () );
191
200
term = term .substring (0 , term .length () - 1 );
192
201
SuggesterPrefixQuery q = new SuggesterPrefixQuery (new Term (t .field (), term ));
193
202
this .suggesterQuery = q ;
194
203
return q ;
195
204
} else {
196
205
SuggesterWildcardQuery q = new SuggesterWildcardQuery (replaceIdentifier (t , identifier ));
197
- identifier = t .text ();
206
+ replaceIdentifier ( t . field (), t .text () );
198
207
this .suggesterQuery = q ;
199
208
return q ;
200
209
}
@@ -209,9 +218,9 @@ protected Query newFuzzyQuery(final Term term, final float minimumSimilarity, fi
209
218
Term newTerm = replaceIdentifier (term , identifier );
210
219
211
220
if (minimumSimilarity < 1 ) {
212
- identifier = term .text () + "~" + minimumSimilarity ;
221
+ replaceIdentifier ( term . field (), term .text () + "~" + minimumSimilarity ) ;
213
222
} else { // similarity greater than 1 must be an integer
214
- identifier = term .text () + "~" + ((int ) minimumSimilarity );
223
+ replaceIdentifier ( term . field (), term .text () + "~" + ((int ) minimumSimilarity ) );
215
224
}
216
225
217
226
int numEdits = FuzzyQuery .floatToEdits (minimumSimilarity ,
@@ -230,7 +239,7 @@ protected Query newRegexpQuery(final Term regexp) {
230
239
if (regexp .text ().contains (identifier )) {
231
240
Term newTerm = replaceIdentifier (regexp , identifier );
232
241
233
- identifier = "/" + regexp .text () + "/" ;
242
+ replaceIdentifier ( regexp . field (), "/" + regexp .text () + "/" ) ;
234
243
235
244
SuggesterRegexpQuery q = new SuggesterRegexpQuery (newTerm );
236
245
this .suggesterQuery = q ;
@@ -253,7 +262,7 @@ protected Query newFieldQuery(
253
262
254
263
SuggesterPhraseQuery spq = new SuggesterPhraseQuery (field , identifier , tokens , this .getPhraseSlop ());
255
264
this .suggesterQuery = spq .getSuggesterQuery ();
256
- this . identifier = tokens .stream ().filter (t -> t .contains (identifier )).findAny ().get ();
265
+ replaceIdentifier ( field , tokens .stream ().filter (t -> t .contains (identifier )).findAny ().get () );
257
266
return spq ;
258
267
}
259
268
@@ -316,7 +325,7 @@ protected Query newRangeQuery(
316
325
) {
317
326
if (lowerTerm .contains (identifier )) {
318
327
String bareLowerTerm = lowerTerm .replace (identifier , "" );
319
- this . identifier = lowerTerm ;
328
+ replaceIdentifier ( field , lowerTerm ) ;
320
329
321
330
SuggesterRangeQuery rangeQuery = new SuggesterRangeQuery (field , new BytesRef (bareLowerTerm ),
322
331
new BytesRef (upperTerm ), startInclusive , endInclusive , SuggesterRangeQuery .SuggestPosition .LOWER );
@@ -326,7 +335,7 @@ protected Query newRangeQuery(
326
335
return rangeQuery ;
327
336
} else if (upperTerm .contains (identifier )) {
328
337
String bareUpperTerm = upperTerm .replace (identifier , "" );
329
- this . identifier = upperTerm ;
338
+ replaceIdentifier ( field , upperTerm ) ;
330
339
331
340
SuggesterRangeQuery rangeQuery = new SuggesterRangeQuery (field , new BytesRef (lowerTerm ),
332
341
new BytesRef (bareUpperTerm ), startInclusive , endInclusive , SuggesterRangeQuery .SuggestPosition .UPPER );
0 commit comments