apache / apache/lucene

NPE in MultiFieldQueryParser [LUCENE-4777]

Open
#5,842 0 comments 0 reactions 0 assignees View on GitHub
affects-version:4.1 legacy-jira-priority:Major module:core/queryparser type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

The method getRegexpQuery() in MultiFieldQueryParser is not overridden.
That leads to RegexpQueries where field=null (see example below).

```Java
QueryParser qp = new MultiFieldQueryParser(Version.LUCENE_41, new String[]{ "content" }, new StandardAnalyzer(Version.LUCENE_41))
Query q = qp.parse("path:foo/bar/foo"); // throws no ParseException
q.toString() // throws NPE
```

Workaround for MultiFieldQueryParser:

```Java
`@Override`
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
if (field == null) {
List clauses = new ArrayList();
for (int i = 0; i < fields.length; i++) {
clauses.add(new BooleanClause(getRegexpQuery(fields[i], termStr), BooleanClause.Occur.SHOULD));
}
return getBooleanQuery(clauses, true);
}
return super.getRegexpQuery(field, termStr);
}
```

---
Migrated from [LUCENE-4777](https://issues.apache.org/jira/browse/LUCENE-4777) by André

Contributor guide

Open the contributing guide

Research direction

Start at MultiFieldQueryParser and its getRegexpQuery entry point, then reproduce the provided path:foo/bar/foo example. The fix is done when parsing a regexp query across the configured fields produces a query whose toString() no longer throws a NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.