package isical.ir.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Readme: 
 * A demo file created for the IR course at ISI Kolkata.
 * 
 * Instruction:
 * 1. Place this Java file in the appropriate package (folder).
 * 2. Download Lucene 4.10.1 and keep the following jar files in your build path
 * (a) lucene-core, (b) lucene-analyzers-common, (c) lucene-queries and (d) lucene-queryparser
 * 3. Change the local file names as you need  
 * 
 * @author debapriyo
 *
 */
public class LuceneDemo {

	public static void indexer() throws IOException {
		StandardAnalyzer analyzer = new StandardAnalyzer();
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_1,
				analyzer);
		String indexDir = ".//index";
		Directory dir;
		dir = FSDirectory.open(new File(indexDir));
		IndexWriter writer = new IndexWriter(dir, config);

		Document doc1 = new Document();
		doc1.add(new TextField("LastName", "Banerjee", Field.Store.YES));
		doc1.add(new StringField("FirstName", "Arkadeep", Field.Store.YES));
		doc1.add(new IntField("Age", 24, Field.Store.YES));
		doc1.add(new TextField(
				"Description",
				"Arkadeep is a highly motivated student who loves any challenge.",
				Field.Store.NO));

		writer.addDocument(doc1);

		Document doc2 = new Document();
		doc2.add(new TextField("LastName", "Pal", Field.Store.YES));
		doc2.add(new StringField("FirstName", "Arindam", Field.Store.YES));
		doc2.add(new IntField("Age", 24, Field.Store.YES));
		doc2.add(new TextField("Description",
				"Arindam is a philosopher in heart.", Field.Store.NO));

		writer.addDocument(doc2);

		writer.close();

	}

	public static void search(String query) throws IOException, ParseException {
		int k = 10;
		StandardAnalyzer analyzer = new StandardAnalyzer();
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
				".//index")));
		IndexSearcher searcher = new IndexSearcher(reader);
				
		Query q = new QueryParser("Description", analyzer).parse(query);
		
		
		//String[] fields = {"Description","LastName","FirstName"};
		//Query q = new MultiFieldQueryParser(fields,analyzer).parse(query);
		
		TopDocs docs = searcher.search(q, k);
		ScoreDoc[] hits = docs.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((i + 1) + ". " + d.get("FirstName") + " " + d.get("LastName"));
	    }

	    reader.close();
	}

	public static void main(String[] args) {

		try {
			//indexer();
			//System.exit(1);
			search("LastName:banerjee");
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
