Created
September 24, 2012 15:00
-
-
Save madan712/3776391 to your computer and use it in GitHub Desktop.
Java program to create index and search using Lucene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* LuceneExample.java */ | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import org.apache.lucene.analysis.Analyzer; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.index.IndexReader; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.index.IndexWriter.MaxFieldLength; | |
import org.apache.lucene.queryParser.QueryParser; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.TopDocs; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.FSDirectory; | |
import org.apache.lucene.store.SimpleFSDirectory; | |
import org.apache.lucene.util.Version; | |
public class LuceneExample { | |
public static final String files = "C:/TestLucene/files"; | |
public static final String index = "C:/TestLucene/index"; | |
public static void createIndex() { | |
System.out.println("Creating indexes...."); | |
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); | |
try { | |
// Store the index in file | |
Directory directory = new SimpleFSDirectory(new File(index)); | |
IndexWriter iwriter = new IndexWriter(directory, analyzer, true, | |
MaxFieldLength.UNLIMITED); | |
File dir = new File(files); | |
File[] files = dir.listFiles(); | |
for (File file : files) { | |
System.out.println(file.getPath()); | |
Document doc = new Document(); | |
doc.add(new Field("path", file.getPath(), Field.Store.YES, | |
Field.Index.ANALYZED)); | |
Reader reader = new FileReader(file.getCanonicalPath()); | |
doc.add(new Field("contents", reader)); | |
iwriter.addDocument(doc); | |
} | |
iwriter.optimize(); | |
iwriter.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void searchIndex(String searchString) { | |
System.out.println("Searching.... '" + searchString + "'"); | |
try { | |
IndexReader reader = IndexReader.open(FSDirectory.open(new File( | |
index)), true); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// construct our usual analyzer | |
QueryParser qp = new QueryParser(Version.LUCENE_30, "contents", | |
analyzer); | |
Query query = qp.parse(searchString); // parse the query and construct the Query object | |
TopDocs hits = searcher.search(query, 100); // run the query | |
if (hits.totalHits == 0) { | |
System.out.println("No data found."); | |
} else { | |
for (int i = 0; i < hits.totalHits; i++) { | |
Document doc = searcher.doc(hits.scoreDocs[i].doc); // get the next document | |
String url = doc.get("path"); // get its path field | |
System.out.println("Found in :: " + url); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
createIndex(); | |
searchIndex("Object"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only few keywords are searched if i use the above code ... I want every keyword has to be searched in pdf file