Web / Apache Lucene Interview questions
What is the difference between a TermQuery and a PhraseQuery?
Both operate on exact Terms rather than doing fuzzy or analyzed matching at query time, but they check for very different conditions.
| TermQuery | PhraseQuery |
| Matches documents containing a single exact Term. | Matches documents containing a sequence of Terms in order. |
| Ignores term position entirely. | Relies on stored term positions to check adjacency. |
| Example: find docs containing "search". | Example: find docs containing "full text search" as a phrase. |
PhraseQuery also supports a configurable "slop" value, allowing a limited number of words to appear between the phrase terms - useful for near-phrase matching without requiring perfectly adjacent terms.
More Related questions...