需求:从数据库加载最新的疾病名称数据写到classes目录下的lucene扩展词典
public HashMap<String, String> updateAllIndex() {
HashMap<String, String> resultHashMap = Maps.newHashMap()
FyDiseaseLuceneDao diseaseLuceneDao = new FyDiseaseLuceneDao()
List<FyDisease> diseaseList = fyDiseaseDao.findListByIstatus1()
if (diseaseList != null && diseaseList.size() > 0) {
//更新疾病扩展词表
//System.err.println(this.getClass().getClassLoader().getResourceAsStream(""))
//try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("disease.dic")))) {
String pathStr = null
if (System.getProperty("os.name").toLowerCase().contains("windows"))
pathStr = this.getClass().getClassLoader().getResource("").getPath().substring(1)
else
pathStr = this.getClass().getClassLoader().getResource("").getPath()
try (BufferedWriter out = new BufferedWriter(new FileWriter(pathStr+"disease.dic"))) {
diseaseList.forEach(d -> {
try {
out.write(d.getIndexName())
out.newLine()
} catch (Exception e) {
e.printStackTrace()
}
})
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
// 存lucene 索引
try {
diseaseLuceneDao.addIndexBatch(diseaseList, b -> {
Document doc = new Document()
doc.add(new StringField("id", b.getId(), Store.YES))
doc.add(new StringField("indexName", b.getIndexName(), Store.YES))
return doc
})
resultHashMap.put("code", "200")
resultHashMap.put("msg", "更新lucene索引成功")
} catch (Exception e) {
resultHashMap.put("code", "500")
resultHashMap.put("msg", "更新lucene索引异常")
e.printStackTrace()
}
}
return resultHashMap
}