Skip to content

Commit 6a9fcc9

Browse files
ahornaceVladimir Kotal
authored andcommitted
Upper case char in prefix for case insensitive field fix
1 parent d650e82 commit 6a9fcc9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/org/opensolaris/opengrok/search/CustomQueryParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public CustomQueryParser(String field) {
7070
* @return {@code true} if the field is case sensitive, {@code false}
7171
* otherwise
7272
*/
73-
private static boolean isCaseSensitive(String field) {
73+
protected static boolean isCaseSensitive(String field) {
7474
// Only definition search and reference search are case sensitive
7575
return QueryBuilder.DEFS.equals(field)
7676
|| QueryBuilder.REFS.equals(field);

src/org/opensolaris/opengrok/web/suggester/query/SuggesterQueryParser.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Set;
5151
import java.util.logging.Level;
5252
import java.util.logging.Logger;
53+
import java.util.regex.Pattern;
5354

5455
/**
5556
* 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) {
102103

103104
private Term replaceIdentifier(final Term term, final String identifier) {
104105
Term newTerm = new Term(term.field(), term.text().replace(identifier, ""));
105-
this.identifier = term.text();
106-
106+
replaceIdentifier(term.field(), term.text());
107107
return newTerm;
108108
}
109109

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+
110119
@Override
111120
protected Query getBooleanQuery(final List<BooleanClause> clauses) throws ParseException {
112121

@@ -187,14 +196,14 @@ protected Query newWildcardQuery(final Term t) {
187196
if (t.text().contains(identifier)) {
188197
String term = t.text().replace(identifier, "");
189198
if (term.endsWith("*")) {
190-
identifier = t.text();
199+
replaceIdentifier(t.field(), t.text());
191200
term = term.substring(0, term.length() - 1);
192201
SuggesterPrefixQuery q = new SuggesterPrefixQuery(new Term(t.field(), term));
193202
this.suggesterQuery = q;
194203
return q;
195204
} else {
196205
SuggesterWildcardQuery q = new SuggesterWildcardQuery(replaceIdentifier(t, identifier));
197-
identifier = t.text();
206+
replaceIdentifier(t.field(), t.text());
198207
this.suggesterQuery = q;
199208
return q;
200209
}
@@ -209,9 +218,9 @@ protected Query newFuzzyQuery(final Term term, final float minimumSimilarity, fi
209218
Term newTerm = replaceIdentifier(term, identifier);
210219

211220
if (minimumSimilarity < 1) {
212-
identifier = term.text() + "~" + minimumSimilarity;
221+
replaceIdentifier(term.field(), term.text() + "~" + minimumSimilarity);
213222
} else { // similarity greater than 1 must be an integer
214-
identifier = term.text() + "~" + ((int) minimumSimilarity);
223+
replaceIdentifier(term.field(), term.text() + "~" + ((int) minimumSimilarity));
215224
}
216225

217226
int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity,
@@ -230,7 +239,7 @@ protected Query newRegexpQuery(final Term regexp) {
230239
if (regexp.text().contains(identifier)) {
231240
Term newTerm = replaceIdentifier(regexp, identifier);
232241

233-
identifier = "/" + regexp.text() + "/";
242+
replaceIdentifier(regexp.field(), "/" + regexp.text() + "/");
234243

235244
SuggesterRegexpQuery q = new SuggesterRegexpQuery(newTerm);
236245
this.suggesterQuery = q;
@@ -253,7 +262,7 @@ protected Query newFieldQuery(
253262

254263
SuggesterPhraseQuery spq = new SuggesterPhraseQuery(field, identifier, tokens, this.getPhraseSlop());
255264
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());
257266
return spq;
258267
}
259268

@@ -316,7 +325,7 @@ protected Query newRangeQuery(
316325
) {
317326
if (lowerTerm.contains(identifier)) {
318327
String bareLowerTerm = lowerTerm.replace(identifier, "");
319-
this.identifier = lowerTerm;
328+
replaceIdentifier(field, lowerTerm);
320329

321330
SuggesterRangeQuery rangeQuery = new SuggesterRangeQuery(field, new BytesRef(bareLowerTerm),
322331
new BytesRef(upperTerm), startInclusive, endInclusive, SuggesterRangeQuery.SuggestPosition.LOWER);
@@ -326,7 +335,7 @@ protected Query newRangeQuery(
326335
return rangeQuery;
327336
} else if (upperTerm.contains(identifier)) {
328337
String bareUpperTerm = upperTerm.replace(identifier, "");
329-
this.identifier = upperTerm;
338+
replaceIdentifier(field, upperTerm);
330339

331340
SuggesterRangeQuery rangeQuery = new SuggesterRangeQuery(field, new BytesRef(lowerTerm),
332341
new BytesRef(bareUpperTerm), startInclusive, endInclusive, SuggesterRangeQuery.SuggestPosition.UPPER);

0 commit comments

Comments
 (0)