Lucene 4.x – Indexer
//init index writer
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46));
IndexWriter writer = new IndexWriter(FSDirectory.open(new File(“index”)), config);
//select data from database with json format (array of records)
JSONObject job = SqlToJson.select(“select * from citems”);
JSONArray jarr = job.getJSONArray(“results”);
for(int i=0;i<jarr.length();i++){
JSONObject j = jarr.getJSONObject(i);
// create lucene document
Document doc = new Document();
// create fields
StringField fupc = new StringField(“upc”, j.getString(“upc”), Store.YES);
TextField ftitle = new TextField(“title”, j.getString(“title”),Store.YES);
TextField fsynopsis = new TextField(“synopsis”, j.getString(“synopsis”),Store.YES);
TextField fptype = new TextField(“ptype”, j.getString(“ptype”),Store.YES);
// add fields to document
doc.add(fupc);
doc.add(ftitle);
doc.add(fsynopsis);
doc.add(fptype);
System.out.println(doc.get(“title”));
// add document to index
writer.deleteDocuments(new Term(“upc”,doc.get(“upc”)));
writer.addDocument(doc);
}
//commit and close index writer
writer.commit();
writer.close(true);