Lucene

public void appendIndex(TInformationBmsVO informationVO, String path) throws IOException {
String indexFile = path + "file\\temp\\upload\\data\\index";
String scourceFile = path + informationVO.getFFilepathInformation();
String fileContent = "";
IndexWriter writer=null;
try {
Analyzer analyzer = new IKAnalyzer();
Directory directory=FSDirectory.open(new File(indexFile));
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_36,analyzer);
iwc.setOpenMode(OpenMode.APPEND);
writer=new IndexWriter(directory, iwc);
Document doc=null;
File file = new File(scourceFile);
if(file.isFile()){
if(file.getName() != null && (file.getName().trim().toLowerCase().endsWith(".doc") || file.getName().trim().toLowerCase().endsWith(".docx") || file.getName().trim().toLowerCase().endsWith(".xls") || file.getName().trim().toLowerCase().endsWith(".xlsx") || file.getName().trim().toLowerCase().endsWith(".txt") || file.getName().trim().toLowerCase().endsWith(".ppt") || file.getName().trim().toLowerCase().endsWith(".pptx") || file.getName().trim().toLowerCase().endsWith(".pdf"))){
fileContent = this.readAllFileType(scourceFile);
}
doc=new Document();
doc.add(new Field("id",String.valueOf(System.currentTimeMillis()), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("content",file.getName() + fileContent, Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("fileName",file.getName(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(writer!=null){
writer.close();
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}


List<String> fileNameList = new Vector<String>();
String indexFile = informationVO.getTemp_field11() + "\\index";
IndexReader reader = null;
Directory directory = null;
try {
Analyzer analyzer = new IKAnalyzer();
directory=FSDirectory.open(new File(indexFile));
reader =IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
QueryParser parser=new QueryParser(Version.LUCENE_36,"content",analyzer);
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query=parser.parse(informationVO.getTemp_field10().trim());
TopDocs tds=searcher.search(query,100);
ScoreDoc[] sds=tds.scoreDocs;
for(ScoreDoc sd:sds){
Document d = searcher.doc(sd.doc);
if(!fileNameList.contains(d.get("fileName"))){
fileNameList.add(d.get("fileName"));
}
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
finally{
try {
reader.close();
directory.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

return fileNameList;


/**
* 删除索引
* @param informationVO
* @throws IOException
*/
public void deleteIndex(TInformationBmsVO informationVO, String path)throws IOException{
String deleteFileName = informationVO.getFFilepathInformation();
deleteFileName = deleteFileName.substring(deleteFileName.lastIndexOf("\\") + 1);
String indexFile = path + "file\\temp\\upload\\data\\index";
IndexReader reader= IndexReader.open(FSDirectory.open(new File(indexFile)), false);
Document doc = null;
int sum = reader.numDocs();
String indexFileName = "";
for(int i = 0; i < sum; i ++){
doc = reader.document(i);
indexFileName = doc.get("fileName");
if(deleteFileName != null && indexFileName != null && deleteFileName.equals(indexFileName)){
Term term1=new Term("id",doc.get("id"));
reader.deleteDocuments(term1);
}
}
reader.close();
}

/**
* 更新索引
* @param informationVO
* @param path 工程绝对路径
* @throws IOException
*/
@SuppressWarnings("deprecation")
public void updateIndex(TInformationBmsVO informationVO, String path, String sourceFileName)throws IOException{
//索引路径
Document doc = null;
IndexWriter writer = null;
String indexFile = path + "file\\temp\\upload\\data\\index";
String newFileName = informationVO.getFFilepathInformation();
File file = new File(newFileName);
String fileContent = "";
Analyzer analyzer = new IKAnalyzer();
try {
Directory directory = FSDirectory.open(new File(indexFile));
IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
writerConfig.setOpenMode(OpenMode.APPEND);
writer = new IndexWriter(directory, writerConfig);
String sourceId = this.queryIdBySourceFile(path, sourceFileName);
doc = new Document();
fileContent = this.readAllFileType(path + "file\\temp\\upload\\data\\" + file.getName());
doc.add(new Field("id", String.valueOf(System.currentTimeMillis()), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("content",file.getName() + fileContent, Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("fileName",file.getName(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
Term term = new Term("id", sourceId);
writer.updateDocument(term, doc);
writer.optimize();
writer.close();
directory.close();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(writer!=null){
writer.close();
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 通过原文件名查找索引id
* @param fileName
* @return
*/
public String queryIdBySourceFile(String path, String fileName){

fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
String indexFile = path + "file\\temp\\upload\\data\\index";
IndexReader reader;
try {
reader = IndexReader.open(FSDirectory.open(new File(indexFile)), false);
Document doc = null;
int sum = reader.numDocs();
String indexFileName = "";
for(int i = 0; i < sum; i ++){
doc = reader.document(i);
indexFileName = doc.get("fileName");
if(fileName != null && indexFileName != null && fileName.equals(indexFileName)){
reader.close();
return doc.get("id");
}
}
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值