Lucene Tutorial - Lucene HelloWorld








We can use Lucene to add full-text search capability to your application.

Index

We're going to create an in-memory index from some strings.

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LATEST);
Directory index = new RAMDirectory();
/*from   w w  w .  j  a v a  2 s . c o m*/
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action", "1");
addDoc(w, "Lucene for Dummies", "2");
addDoc(w, "Java", "3");
addDoc(w, "Oracle", "4");
w.close();

private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
  Document doc = new Document();
  doc.add(new TextField("title", title, Field.Store.YES));
  doc.add(new StringField("isbn", isbn, Field.Store.YES));
  w.addDocument(doc);
}
 

TextField tokenizes the content while StringField does not tokenize its content.





Query

The following code shows how to build a query from a Java string.

String querystr = "lucene";

Query q = new QueryParser(Version.LATEST, "title", analyzer).parse(querystr);

When doing the search we first open the index, which is a in-memory index. TopScoreDocCollector is used to collect the top 10 scoring hits.

int hitsPerPage = 10;
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;




Display

The following is the logic to show the result.

    System.out.println("Found " + hits.length + " hits.");
    for(int i=0;i<hits.length;++i) {
      int docId = hits[i].doc;
      Document d = searcher.doc(docId);
      System.out.println(d.get("isbn") );
      System.out.println(d.get("title") );
    }

Complete code

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import java.io.IOException;

public class Main {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException, ParseException {
  
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LATEST);
    Directory index = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);

    IndexWriter w = new IndexWriter(index, config);
    addDoc(w, "Lucene in Action", "1");
    addDoc(w, "Lucene for Dummies", "2");
    addDoc(w, "Java", "3");
    addDoc(w, "Oracle", "4");
    w.close();

    String querystr = "lucene";

    Query q = new QueryParser(Version.LATEST, "title", analyzer).parse(querystr);

    int hitsPerPage = 10;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    searcher.search(q, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    
    System.out.println("Found " + hits.length + " hits.");
    for(int i=0;i<hits.length;++i) {
      int docId = hits[i].doc;
      Document d = searcher.doc(docId);
      System.out.println(d.get("isbn") );
      System.out.println(d.get("title") );
    }
    reader.close();
  }

  private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
    Document doc = new Document();
    doc.add(new TextField("title", title, Field.Store.YES));
    doc.add(new StringField("isbn", isbn, Field.Store.YES));
    w.addDocument(doc);
  }
}

Download

Download the source code and library.



Download Lucene_HelloWorld.zip