Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit b36861c

Browse files
committed
fixed search issues with non-tokenized fields
Signed-off-by: Neal Ensor <[email protected]>
1 parent 3af4e1d commit b36861c

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

src/main/java/gov/osti/search/SearchData.java

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class SearchData implements Serializable {
2828
.setSerializationInclusion(Include.NON_NULL);
2929

3030
// set of special characters to be escaped before sending to SOLR
31-
protected static Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
31+
protected static Pattern TEXT_REGEX_CHARACTERS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
32+
// set of special characters applying to TOKENS in SOLR
33+
protected static Pattern TOKEN_REGEX_CHARACTERS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|\"]");
3234

3335
private String allFields = null;
3436
private String softwareTitle = null;
@@ -161,7 +163,19 @@ public void setSort(String sort) {
161163
protected static String escape(String in) {
162164
return (null==in) ?
163165
"" :
164-
SPECIAL_REGEX_CHARS.matcher(in).replaceAll("\\\\$0");
166+
TEXT_REGEX_CHARACTERS.matcher(in).replaceAll("\\\\$0");
167+
}
168+
169+
/**
170+
* Escape SOLR special characters in TOKEN expressions.
171+
*
172+
* @param in the incoming search expression
173+
* @return a String with special characters escaped
174+
*/
175+
protected static String escapeToken(String in) {
176+
return (null==in) ?
177+
"" :
178+
TOKEN_REGEX_CHARACTERS.matcher(in).replaceAll("\\\\$0");
165179
}
166180

167181
/**
@@ -193,7 +207,7 @@ public String toQ() {
193207
StringBuilder values = new StringBuilder();
194208
for ( String license : getLicenses() ) {
195209
if (values.length()>0) values.append(" OR ");
196-
values.append("licenses:\"").append(escape(license)).append("\"");
210+
values.append("licenses:\"").append(escapeToken(license)).append("\"");
197211
}
198212
if (values.length()>0) {
199213
if (q.length()>0) q.append(" ");
@@ -216,28 +230,28 @@ public String toQ() {
216230
if (q.length()>0) q.append(" ");
217231
q.append("_id_numbers:(").append(escape(getIdentifiers())).append(")");
218232
}
219-
if (null!=getResearchOrganization()) {
220-
StringBuilder values = new StringBuilder();
221-
for ( String org : getResearchOrganization() ) {
222-
if (values.length()>0) values.append(" OR ");
223-
values.append("researchOrganizations.organizationName:\"").append(escape(org)).append("\"");
224-
}
225-
if (values.length()>0) {
226-
if (q.length()>0) q.append(" ");
227-
q.append("(").append(values.toString()).append(")");
228-
}
229-
}
230-
if (null!=getSponsoringOrganization()) {
231-
StringBuilder values = new StringBuilder();
232-
for ( String org : getSponsoringOrganization() ) {
233-
if (values.length()>0) values.append(" OR ");
234-
values.append("sponsoringOrganizations.organizationName:\"").append(escape(org)).append("\"");
235-
}
236-
if (values.length()>0) {
237-
if (q.length()>0) q.append(" ");
238-
q.append("(").append(values.toString()).append(")");
239-
}
240-
}
233+
if (null!=getResearchOrganization()) {
234+
StringBuilder values = new StringBuilder();
235+
for ( String org : getResearchOrganization() ) {
236+
if (values.length()>0) values.append(" OR ");
237+
values.append("researchOrganizations.organizationName:\"").append(escape(org)).append("\"");
238+
}
239+
if (values.length()>0) {
240+
if (q.length()>0) q.append(" ");
241+
q.append("(").append(values.toString()).append(")");
242+
}
243+
}
244+
if (null!=getSponsoringOrganization()) {
245+
StringBuilder values = new StringBuilder();
246+
for ( String org : getSponsoringOrganization() ) {
247+
if (values.length()>0) values.append(" OR ");
248+
values.append("sponsoringOrganizations.organizationName:\"").append(escape(org)).append("\"");
249+
}
250+
if (values.length()>0) {
251+
if (q.length()>0) q.append(" ");
252+
q.append("(").append(values.toString()).append(")");
253+
}
254+
}
241255
if (!StringUtils.isEmpty(getSoftwareTitle())) {
242256
if (q.length()>0) q.append(" ");
243257
q.append("softwareTitle:(").append(escape(getSoftwareTitle())).append(")");
@@ -266,7 +280,7 @@ public String toQ() {
266280
.withSecond(59)))
267281
.append("]");
268282
}
269-
283+
270284
return (0==q.length()) ? "*:*" : q.toString();
271285
}
272286

0 commit comments

Comments
 (0)