snoopdave commented on code in PR #225:
URL: https://github.com/apache/roller/pull/225#discussion_r4406638538
##########
app/src/main/java/org/apache/roller/weblogger/business/search/lucene/SearchOperation.java:
##########
@@ -229,4 +231,68 @@ public void setLocale(String locale) {
this.locale = locale;
}
+
+ /**
+ * Query parser that accepts explicit field names only for the fields that
+ * searches are meant to cover. Other index fields hold data that is not
+ * shown on the weblog, so a query naming one is a parse error.
+ */
+ static class SearchFieldsQueryParser extends MultiFieldQueryParser {
+
+ private final Set<String> searchFields;
+
+ SearchFieldsQueryParser(String[] fields, Analyzer analyzer) {
+ super(fields, analyzer);
+ this.searchFields = Set.of(fields);
+ }
+
+ private void checkField(String field) throws ParseException {
+ if (field != null && !searchFields.contains(field)) {
+ throw new ParseException("Cannot search field: " + field);
+ }
+ }
+
+ @Override
+ protected Query getFieldQuery(String field, String queryText, int
slop) throws ParseException {
+ checkField(field);
+ return super.getFieldQuery(field, queryText, slop);
+ }
+
+ @Override
+ protected Query getFieldQuery(String field, String queryText, boolean
quoted) throws ParseException {
+ checkField(field);
+ return super.getFieldQuery(field, queryText, quoted);
+ }
+
+ @Override
+ protected Query getFuzzyQuery(String field, String termStr, float
minSimilarity) throws ParseException {
+ checkField(field);
+ return super.getFuzzyQuery(field, termStr, minSimilarity);
+ }
+
+ @Override
+ protected Query getPrefixQuery(String field, String termStr) throws
ParseException {
+ checkField(field);
+ return super.getPrefixQuery(field, termStr);
+ }
+
+ @Override
+ protected Query getWildcardQuery(String field, String termStr) throws
ParseException {
+ checkField(field);
Review Comment:
🤖Claude: fixed by letting `getWildcardQuery` skip the field check only when
both field and term are `*`, so `*:*` parses to `MatchAllDocsQuery` again. The
search stays limited to the weblog, and match-all reads no field. Any other `*`
field query is still refused. `SearchFieldsQueryParserTest` gains
`matchAllParses`, plus `*:a*` and `email:*` in the refused list. Full `app`
suite on JDK 11: 420 run, 0 failures, 1 skipped. (Local change, in the next
push.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]