Created
September 24, 2012 15:01
-
-
Save madan712/3776402 to your computer and use it in GitHub Desktop.
Lucene - Java program to update index and search
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 updateIndex(File newFile) { | |
System.out.println("Updating 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, false, | |
MaxFieldLength.UNLIMITED); | |
System.out.println(newFile.getPath()); | |
Document doc = new Document(); | |
doc.add(new Field("path", newFile.getPath(), Field.Store.YES, | |
Field.Index.ANALYZED)); | |
Reader reader = new FileReader(newFile.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) { | |
updateIndex(new File("C:/TestLucene/files/PHP.txt")); | |
searchIndex("Object"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment