apache / apache/lucene

TermQuery.toString() doesn't play nicely with whitespace [LUCENE-4673]

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

Description

A TermQuery where term.text() contains whitespace outputs incorrect string representation: field:foo bar instead of field:"foo bar". A "correct" representation is such that could be parsed again to the correct Query object (using the correct analyzer, yes, but still).

This may not be so critical, but in our system we use Lucene's QP to parse and then pre-process and optimize user queries. To do that we use Query.toString on some clauses to rebuild the query string.

This can be easily resolved by always adding quote marks before and after the term text in TermQuery.toString. Testing to see if they are required or not is too much work and TermQuery is ignorant of quote marks anyway.

Some other scenarios which could benefit from this change is places where escaped characters are used, such as URLs.

---
Migrated from [LUCENE-4673](https://issues.apache.org/jira/browse/LUCENE-4673) by Itamar Syn-Hershko, 1 vote

Contributor guide

Open the contributing guide

Research direction

Start at TermQuery.toString() and trace how its output is consumed by the query parser. Reproduce a term containing whitespace or escaped characters, then verify that the resulting string can be parsed back into the equivalent query; done means the representation round-trips correctly.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.