apache / apache/lucene

QueryParserBase should not throw ParseException in getPrefixQuery when termStr starts with * [LUCENE-4773]

Open
#5,838 0 comments 0 reactions 0 assignees View on GitHub
affects-version:4.0 affects-version:4.1 legacy-jira-priority:Minor 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 getPrefixQuery of org.apache.lucene.queryparser.classic.QueryParserBase checks for leading \*-wildcards:

```java
protected Query getPrefixQuery(String field, String termStr) throws ParseException
{
if (!allowLeadingWildcard && termStr.startsWith("*"))
throw new ParseException("'*' not allowed as first character in PrefixQuery");
...
}
```

However, the passed termStr is already unescaped in handleBareTokenQuery(...):

```java
q = getPrefixQuery(qfield,
discardEscapeChar(term.image.substring
(0, term.image.length()-1)));
```

Therefore, a search query like this one results in a ParseException, even though the first wildcard is escaped:
```
title:\*a*
```

I don't think there is any sense in checking for leading wildcards in getPrefixQuery, as the passed termStr is already used literally, without paying attention to special characters at all.

---
Migrated from [LUCENE-4773](https://issues.apache.org/jira/browse/LUCENE-4773) by Christoph Kaser

Contributor guide

Open the contributing guide

Research direction

Start in org.apache.lucene.queryparser.classic.QueryParserBase, following handleBareTokenQuery(...) into getPrefixQuery(...), where the already-unescaped termStr is checked for a leading wildcard. Verify the escaped query title:\*a* no longer raises ParseException while genuinely disallowed input retains its expected behavior.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.