Lucene WildcardQuery Search Example

In Lucene, WildcardQuery can be used to execute wildcard based searches on lucene indexes. Learn to use WildcardQuery with example.

Wild card queries can be slow in runtime, as it needs to iterate over many terms. To prevent it, a wildcard term should not start with the wildcard *.
Table of Contents

Supported Wildcards
WildcardQuery Example
Sourcecode

Supported Wildcards

Supported wildcards are:

WildcardUsage
*matches any character sequence (including the empty one)
?matches any single character
'\'escape character

Read More: Lucene – Index and Search Text Files

Lucene WildcardQuery Example

In this example, I am reusing the indexes created in previous lucene example. If you want to learn more about creating lucene indexes with text files, follow linked article.

package com.howtodoinjava.demo.lucene.misc;

import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.search.uhighlight.UnifiedHighlighter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class WildcardQueryExample 
{
	//This contains the lucene indexed documents
	private static final String INDEX_DIR = "indexedFiles";

	public static void main(String[] args) throws Exception 
	{
		//Get directory reference
		Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
		
		//Index reader - an interface for accessing a point-in-time view of a lucene index
		IndexReader reader = DirectoryReader.open(dir);
		
		//Create lucene searcher. It search over a single IndexReader.
		IndexSearcher searcher = new IndexSearcher(reader);
		
		//analyzer with the default stop words
		Analyzer analyzer = new StandardAnalyzer();
		
		
		/**
		 * Wildcard "*" Example
		 * */
		
		//Create wildcard query
		Query query = new WildcardQuery(new Term("contents", "prefer*"));
		
		//Search the lucene documents
		TopDocs hits = searcher.search(query, 10, Sort.INDEXORDER);
		
		System.out.println("Search terms found in :: " + hits.totalHits + " files");
		
		UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, analyzer);
        String[] fragments = highlighter.highlight("contents", query, hits);
        
        for(String f : fragments)
        {
        	System.out.println(f);
        }
        
        /**
		 * Wildcard "?" Example
		 * */
        
        //Create wildcard query
  		query = new WildcardQuery(new Term("contents", "prefer??d"));
  		
  		//Search the lucene documents
  		hits = searcher.search(query, 10, Sort.INDEXORDER);
  		
  		System.out.println("Search terms found in :: " + hits.totalHits + " files");
  		
  		highlighter = new UnifiedHighlighter(searcher, analyzer);
	    fragments = highlighter.highlight("contents", query, hits);
	      
	    for(String f : fragments)
	    {
	    	System.out.println(f);
	    }
		
        dir.close();
	}
}

Output:

Search terms found in :: 1 files
Questions explained agreeable preferred strangers too him her son. 
Search terms found in :: 1 files
Questions explained agreeable preferred strangers too him her son. 

Sourcecode

Download the sourcecode using below given link.

Drop me your questions related to lucene WildcardQuery class usage in comments section.

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

1 thought on “Lucene WildcardQuery Search Example”

  1. Hi,
    First of all, I want to thank you for these excellent posts that helped me a lot.
    My question is: can we search using multiple words such as “I?want?it?all” in Lucene?
    Thanks

    Reply

Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.