Lab6.LuceneTest.java Source code

Java tutorial

Introduction

Here is the source code for Lab6.LuceneTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Lab6;

/**
 *
 * @author yunfan
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.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;

public class LuceneTest {
    public static void main(String[] args) {

        try {
            //   Specify the analyzer for tokenizing text.
            //   The same analyzer should be used for indexing and searching
            StandardAnalyzer analyzer = new StandardAnalyzer();

            //   Code to create the index
            Directory index = new RAMDirectory();

            IndexWriterConfig config = new IndexWriterConfig(analyzer);

            IndexWriter w = new IndexWriter(index, config);
            addDoc(w, " Software Engineering 2", "133");
            addDoc(w, " Software Engineering 1", "CMPE 131:");
            addDoc(w, " Object Oriented Design", "CS 151:");
            addDoc(w, " Advance Data Structures with Java ", "CS 146:");
            addDoc(w, " System Security with Java", "CS 166:");
            addDoc(w, "Lucene demo by teja", "23k43413");
            w.close();

            //   Text to search
            String querystr = args.length > 0 ? args[0] : "Object";

            //   The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
            Query q = new QueryParser("Classes", analyzer).parse(querystr);

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

            //   Code to display the results of search
            System.out.println("Found " + hits.length + " Classes Matching your Requirement");
            for (int i = 0; i < hits.length; ++i) {
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);
                System.out.println((i + 1) + ". " + d.get("Number") + d.get("Classes"));
            }

            // reader can only be closed when there is no need to access the documents any more
            reader.close();
        }

        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static void addDoc(IndexWriter w, String Class, String number) throws IOException {
        Document doc = new Document();
        // A text field will be tokenized
        doc.add(new TextField("Classes", Class, Field.Store.YES));
        // We use a string field for isbn because we don\'t want it tokenized
        doc.add(new StringField("Number", number, Field.Store.YES));
        w.addDocument(doc);
    }
}