1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.ar.ArabicAnalyzer;
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;
@SuppressWarnings("deprecation")
public class Lucenetry {
public static final String files = "C:/Lucene/data";
public static final String index = "C:/Lucene/indexes";
public static void createIndex() {
System.out.println("Creating indexes....");
try {
// Store the index in file
Directory directory = new SimpleFSDirectory(new File(index));
IndexWriter iwriter = new IndexWriter(directory, new ArabicAnalyzer(Version.LUCENE_36), 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 ArabicAnalyzer(Version.LUCENE_36);
QueryParser qp = new QueryParser(Version.LUCENE_36, "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( "الحفاظ على البيئة");
}
} |
Partager