Web / Apache Lucene Interview questions
What is the difference between StringField and TextField?
These two Field types look similar but serve opposite purposes, and mixing them up is a frequent source of confusing search behavior.
| StringField | TextField |
| Indexed as a single, un-tokenized term. | Passed through the Analyzer and tokenized into many terms. |
| Good for exact-match values: IDs, status codes, tags. | Good for prose: titles, descriptions, article bodies. |
| A query for "New York" only matches the exact phrase. | A query for "New" or "York" alone can match. |
A common mistake is using TextField for a status field like "PUBLISHED" - the StandardAnalyzer will lowercase it, so a query for the exact literal "PUBLISHED" via TermQuery won't match unless you account for that transformation.
More Related questions...