DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Bosco has posted 1 posts at DZone. View Full User Profile

Java Lucene Version 3.0.0 In-Memory Or Memory Text Search Example

11.26.2009
| 7852 views |
  • submit to reddit
        //imports
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.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.util.Version;


//function
public void simpleLucene() throws CorruptIndexException, LockObtainFailedException, IOException, ParseException {
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

        // Store the index in memory:
        Directory directory = new RAMDirectory();

        // To store an index on disk, use this instead (note that the
        // parameter true will overwrite the index in that directory
        // if one exists):
        // Directory directory = FSDirectory.open(new File("/tmp/testindex"));

        IndexWriter iwriter = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));

        Document doc = new Document();
        String text = "This is the st. text to be indexed1.";
        doc.add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));
        iwriter.addDocument(doc);
        doc = new Document();
        text = "This is the st text to be indexed2.";
        doc.add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));
        iwriter.addDocument(doc);
        iwriter.close();

        // Now search the index:
        IndexSearcher isearcher = new IndexSearcher(directory);

        // Parse a simple query that searches for "text":
        QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
        Query query = parser.parse("st.");

        TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
        isearcher.search(query, collector);

        System.out.println("collector.getTotalHits()=" + collector.getTotalHits());
//        assertEquals(2, collector.getTotalHits());

        // Iterate through the results:
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
            System.out.println("hitDoc.get(\"fieldname\")=" + hitDoc.get("fieldname"));
        }

        isearcher.close();
        directory.close();
    }