concept PrefixQuery in category lucene

appears as: PrefixQuery, PrefixQuery
Lucene in Action, Second Edition

This is an excerpt from Manning's book Lucene in Action, Second Edition.

Lucene comes with a number of concrete Query subclasses. So far in this chapter we’ve mentioned only the most basic Lucene Query: TermQuery. Other Query types are BooleanQuery, PhraseQuery, PrefixQuery, PhrasePrefixQuery, TermRangeQuery, NumericRangeQuery, FilteredQuery, and SpanQuery. All of these are covered in chapters 3 and 5. Query is the common, abstract parent class. It contains several utility methods, the most interesting of which is setBoost(float), which enables you to tell Lucene that certain subqueries should have a stronger contribution to the final relevance score than other subqueries. The setBoost method is described in section 3.5.12. Next we cover TermQuery, which is the building block for most complex queries in Lucene.

3.4.4. Searching on a string: PrefixQuery

PrefixQuery matches documents containing terms beginning with a specified string. It’s deceptively handy. The following code demonstrates how you can query a hierarchical structure recursively with a simple PrefixQuery. The documents contain a category field representing a hierarchical structure, which is perfect for matching with a PrefixQuery, as shown in listing 3.5.

Listing 3.5. PrefixQuery

Our PrefixQueryTest demonstrates the difference between a PrefixQuery and a TermQuery. A methodology category exists below the /technology/computers/programming category. Books in this subcategory are found with a PrefixQuery but not with the TermQuery on the parent category.