import org.apache.lucene.queryParser.*; 
import org.apache.lucene.search.*; 
import org.apache.lucene.index.*; 
import org.apache.lucene.analysis.*; 
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*; 
import org.apache.lucene.store.*;

public class LuceneTest { 
    
    RAMDirectory ramdir;
    Analyzer analyzer;
    IndexWriter writer;
    IndexReader reader;
    Searcher searcher;

    public LuceneTest() {
		analyzer = new StandardAnalyzer(); 
		ramdir = new RAMDirectory();
    }


    public static void main(String args[]) throws Exception { 
		LuceneTest ld = new LuceneTest();
		ld.load();
		ld.search();
    }
    
    
    void load() throws Exception {
		writer = new IndexWriter(ramdir, analyzer, true); 
		Document doc;
		doc = new Document(); doc.add(Field.Text("hello", "this is the first test")); writer.addDocument(doc);
		doc = new Document(); doc.add(Field.Text("hello", "this is the second test")); writer.addDocument(doc);
		doc = new Document(); doc.add(Field.Text("hello", "this is the third test")); writer.addDocument(doc);
		doc = new Document(); doc.add(Field.Text("hello", "this is the fourth test")); writer.addDocument(doc);
		writer.close();
    }

    void search() throws Exception {
		reader = IndexReader.open(ramdir);
		searcher = new IndexSearcher(reader);
		Query query = QueryParser.parse(" +hello:\"third\"", "hello", analyzer);
		search (query);
		query = QueryParser.parse(" +hello:\"fourth test\"", "hello", analyzer);
		search(query);
		query = QueryParser.parse(" +hello:\"fourth day\"", "hello", analyzer);
		search(query);
    }


    void search(Query query) throws Exception {
		Hits hits = searcher.search(query);
		System.out.println("num hits:" +hits.length());
		for (int i = 0; i < hits.length(); i++) {
			System.out.println("document: " + hits.doc(i).get("hello") );
		}
    }
}



