RegexpQuery

Lucene also offers regular expression support in Query. Lucene's favor of RegExp is fast based on benchmark testing. However, it can be slow if the expression begins with ".*". For more information about Lucene's RegExp syntax, refer to http://lucene.apache.org/core/4_10_2/core/org/apache/lucene/search/RegexpQuery.html.

How to do it…

Here is a code snippet:

RegexpQuery query = new RegexpQuery(new Term("content", ".um.*"));

RegexpQuery accepts term as an argument where term would contain Regexp. In this test case, we try to match anything that contains the letter "um" with one leading character. The expression will return sentence one, two, and four from our setup.

Get Lucene 4 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.