Java – Lucerne: Search with partial words

Lucerne: Search with partial words… here is a solution to the problem.

Lucerne: Search with partial words

I’m working on integrating Lucene into our app. Lucene is currently working, for example when I search for “upload” there is some text called “upload” in the document, then it works, but when I search for “Uplo” it doesn’t work. Any ideas?

Code:

  Directory directory = FSDirectory.open(path);
                IndexReader indexReader = DirectoryReader.open(directory);
                IndexSearcher indexSearcher = new IndexSearcher(indexReader);

QueryParser queryParser = new QueryParser("contents", new SimpleAnalyzer());
                Query query = queryParser.parse(text);
                TopDocs topDocs = indexSearcher.search(query, 50);
                for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                    org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
                    objectIds.add(Integer.valueOf(document.get("id")));
                    System.out.println("");
                    System.out.println("id " + document.get("id"));
                    System.out.println("content " + document.get("contents"));
                }
                return objectIds;

Thank you.

Solution

“Upload” may be a token in your Lucene index, where token will be the smallest entity that cannot be further split. If you want to match a partial word like “Uplo”, then it’s best to choose Lucene NGram Indexing.Note that if you use NGram indexes, you will have higher space requirements for inverted indexes.

Related Problems and Solutions