一、首先是从数据库查询数据并添加索引
class="java" name="code">
/**
* 写入数据库索引
* @param sql 查询的sql语句
* @param indexPath 保存索引的硬盘绝对路径
* @return
*/
private int createDBIndex(String sql,String indexPath){
log.info("DBIndex is creating,wait a moment!");
long start=new Date().getTime();
IndexWriter writer;
Directory directory = null;
int numberindex = 0;
try {
File indexFile = new File(indexPath);
directory = new SimpleFSDirectory(indexFile);
writer = new IndexWriter(directory, new IKAnalyzer(),true, IndexWriter.MaxFieldLength.LIMITED);
ResultSet rs = getResultSet(sql);
while (rs.next()) {
Document doc = new Document();
String PATIENT_NAME = rs.getString("PATIENT_NAME") == null?"":rs.getString("PATIENT_NAME");
String PATIENT_SEX = rs.getString("PATIENT_SEX") == null ? "-1":rs.getString("PATIENT_SEX");
String PATIENT_AGE = rs.getString("PATIENT_AGE") == null ? "0":rs.getString("PATIENT_AGE");
String SENDED_DEPT = rs.getString("SENDED_DEPT") == null ? "":rs.getString("SENDED_DEPT");
String SENDED_DOCTOR = rs.getString("SENDED_DOCTOR") == null ?"":rs.getString("SENDED_DOCTOR");
String BODY_OF_CASE = rs.getString("BODY_OF_CASE") == null ? "":rs.getString("BODY_OF_CASE");
String SENDED_HOSPITAL = rs.getString("SENDED_HOSPITAL") == null ? "":rs.getString("SENDED_HOSPITAL");
String CHECK_METHOD = rs.getString("CHECK_METHOD")==null ? "":rs.getString("CHECK_METHOD");
String REPORT_DOCNAME = rs.getString("REPORT_DOCNAME")==null ? "":rs.getString("REPORT_DOCNAME");
String AUDIT_DOCNAME = rs.getString("AUDIT_DOCNAME")==null?"":rs.getString("AUDIT_DOCNAME");
String REPORT_DATE = rs.getString("REPORT_DATE")==null?"":rs.getString("REPORT_DATE");
String POSITIVE_FLAG = rs.getString("POSITIVE_FLAG")==null?"":rs.getString("POSITIVE_FLAG");
String REPORT_DESC_TXT = rs.getString("REPORT_DESC_TXT")==null?"":rs.getString("REPORT_DESC_TXT");
String REPORT_DIAG_TXT = rs.getString("REPORT_DIAG_TXT")==null?"":rs.getString("REPORT_DIAG_TXT");
String CLINIC_DESC = rs.getString("CLINIC_DESC")==null?"":rs.getString("CLINIC_DESC");
String CLINIC_DIAG = rs.getString("CLINIC_DIAG")==null?"":rs.getString("CLINIC_DIAG");
String CLINICAL_ADVICE = rs.getString("CLINIC_ADVICE")==null?"":rs.getString("CLINIC_ADVICE");
String HOSPITAL_CARDID = rs.getString("HOSPITAL_CARDID")==null?"":rs.getString("HOSPITAL_CARDID");
String CLINIC_HOSPITALNO = rs.getString("CLINIC_HOSPITALNO")==null?"":rs.getString("CLINIC_HOSPITALNO");
String FTPPATH = rs.getString("FTPPATH")==null?"":rs.getString("FTPPATH");
String SENDED_DATE = rs.getString("SENDED_DATE")==null?"":rs.getString("SENDED_DATE");
doc.add(new Field("PATIENT_NAME", PATIENT_NAME, Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("PATIENT_SEX", PATIENT_SEX,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("PATIENT_AGE", PATIENT_AGE,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("SENDED_DEPT", SENDED_DEPT,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("SENDED_DOCTOR",SENDED_DOCTOR,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("BODY_OF_CASE",BODY_OF_CASE,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("SENDED_HOSPITAL",SENDED_HOSPITAL,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("CHECK_METHOD", CHECK_METHOD,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("REPORT_DOCNAME", REPORT_DOCNAME,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("AUDIT_DOCNAME", AUDIT_DOCNAME,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("REPORT_DATE",REPORT_DATE,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("POSITIVE_FLAG",POSITIVE_FLAG,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("REPORT_DESC_TXT",REPORT_DESC_TXT,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("REPORT_DIAG_TXT",REPORT_DIAG_TXT,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("CLINIC_DESC",CLINIC_DESC,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("CLINIC_DIAG",CLINIC_DIAG,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("CLINICAL_ADVICE",CLINICAL_ADVICE,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("HOSPITAL_CARDID",HOSPITAL_CARDID,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("CLINIC_HOSPITALNO",CLINIC_HOSPITALNO,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("FTPPATH",FTPPATH,Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("SENDED_DATE",SENDED_DATE,Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
numberindex = writer.numDocs();
writer.optimize();
writer.close();
if(IndexWriter.isLocked(directory)){
IndexWriter.unlock(directory);
}
long end=new Date().getTime();
log.info("Opration sql: \n " + sql + " \n success,Create index " + numberindex + " sucess,spend time is " + (end-start) + " ms!");
} catch (Exception e) {
e.printStackTrace();
log.info("Create index failed......");
}
return numberindex;
}
二、从索引中查询数据
/**
* 从本地索引文件中搜索相关关键字
* @param indexDir 所要查询的索引文件保存地址
* @param fieldString 需要搜索出来的字段
* @param search 搜索关键字的字符串数组
* @throws CorruptIndexException
* @throws IOException
* @throws ParseException
* return List<Document>
*/
@SuppressWarnings({ "deprecation", "resource" })
public List<Document> searchData(File indexDir,String[] fieldString, List<ClassicCasesKeyWords> search) throws CorruptIndexException, IOException, ParseException{
List<Document> docList = new ArrayList<Document>();
long start=new Date().getTime();
IndexSearcher searcher= new IndexSearcher(FSDirectory.open(indexDir),true);
BooleanQuery bQuery = new BooleanQuery(); //组合查询
WildcardQuery w1 = null;
for (int i = 0; i < fieldString.length; i++) {
for (int j = 0; j < search.size(); j++) {
w1 = new WildcardQuery(new Term(fieldString[i], "*" +search.get(j).getKeyName() + "*"));//加通配符才能正常查询完整的模糊查询数据
bQuery.add(w1,BooleanClause.Occur.SHOULD);//or关系
}
}
TopDocs topDocs = searcher.search(bQuery,TOP_NUM);//
ScoreDoc[] hits = topDocs.scoreDocs;
for (int j = 0; j < hits.length; j++) {
int DocId = hits[j].doc;
Document document = searcher.doc(DocId);
docList.add(document);
}
long end=new Date().getTime();
// log.info("\n\n Query "+docList.size()+" result on "+(end-start)+" ms!");
System.out.println("\n\n Query "+docList.size()+" result on "+(end-start)+" ms!");
return docList;
}
以上返回Document的集合,再将每个Document中值遍历
document.getField("BODY_OF_CASE").stringValue();
- IKAnalyzer2012.jar (1.1 MB)
- 下载次数: 0
- lucene-highlighter-3.6.2.jar (87.1 KB)
- 下载次数: 0
- lucene-memory-3.6.2.jar (29.1 KB)
- 下载次数: 0
- lucene-analyzers-3.6.2.jar (1.1 MB)
- 下载次数: 0
- lucene-core-3.6.2.jar (1.5 MB)
- 下载次数: 0