public class SearchTxtFile {
public static void main(String[] args) throws IOException {
String strQuery="曹";
File indexFile=new File("E:\\luceneIndex4"); //获取索引文件
if(!indexFile.exists()){
System.out.println("文件不存在");
}
Directory dir=FSDirectory.open(indexFile); //索引存放的位置
IndexReader indexReader=IndexReader.open(dir); //读取索引
IndexSearcher indexSearch=new IndexSearcher(indexReader); //搜索索引
Term term=new Term("contents",strQuery.toLowerCase()); //词条
TermQuery termQuery=new TermQuery(term); //词条查询
TopDocs topDocs=indexSearch.search(termQuery, 1000); //返回得分最高的Document
ScoreDoc[] scoDoc=topDocs.scoreDocs;
if(scoDoc==null||scoDoc.length==0){
System.out.println("索引不存在");
}
for(int i=0;i<scoDoc.length;i++){
Document doc=indexSearch.doc(scoDoc[i].doc);
System.out.println(doc.get("contents"));
}
indexReader.close();
}
}
public static void main(String[] args) throws IOException {
String strQuery="曹";
File indexFile=new File("E:\\luceneIndex4"); //获取索引文件
if(!indexFile.exists()){
System.out.println("文件不存在");
}
Directory dir=FSDirectory.open(indexFile); //索引存放的位置
IndexReader indexReader=IndexReader.open(dir); //读取索引
IndexSearcher indexSearch=new IndexSearcher(indexReader); //搜索索引
Term term=new Term("contents",strQuery.toLowerCase()); //词条
TermQuery termQuery=new TermQuery(term); //词条查询
TopDocs topDocs=indexSearch.search(termQuery, 1000); //返回得分最高的Document
ScoreDoc[] scoDoc=topDocs.scoreDocs;
if(scoDoc==null||scoDoc.length==0){
System.out.println("索引不存在");
}
for(int i=0;i<scoDoc.length;i++){
Document doc=indexSearch.doc(scoDoc[i].doc);
System.out.println(doc.get("contents"));
}
indexReader.close();
}
}