📄 test.java
字号:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.StringReader;
import java.util.Date;
import javax.swing.JOptionPane;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
public class test
{
public static void main(String[] args)
{
searcher s=new searcher();
try
{
s.action();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class searcher
{
MMAnalyzer analyzer=new MMAnalyzer();
String s;
String dir_Path="E:/amusement and rest/文章欣赏";//需要被检索的文件目录
public void setKeyword(String keyword)
{
s=keyword;
}
public void setDirPath(String dir_Path)
{
this.dir_Path=dir_Path;
}
public void action()
{
String search_field="contents";//要搜索的字段名,目前有title,contents,path字段
String index_path="./indexs";//设置索引所在目录
File f=new File(index_path);
try
{
if(!f.exists())
{
//定义目录路径并建立索引
IndexWriter index=new IndexWriter("./indexs",analyzer,true);
//index.setMaxFieldLength(1000);
idx(index,dir_Path);
index.close();
}
s=JOptionPane.showInputDialog("输入要查找的关键词:").trim();
//开始检索
Date begin=new Date();
IndexSearcher is=new IndexSearcher(index_path);
BooleanClause.Occur[] clause={BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD};
Query qa=MultiFieldQueryParser.parse(s,new String[]{"title",search_field},clause,analyzer);
Hits hits=is.search(qa);
Date end=new Date();
System.out.println("总共搜索出:" + hits.length()+"个结果 "
+"用时:"+(end.getTime()-begin.getTime())+"毫秒");
Highlighter highlighter = new Highlighter(new QueryScorer(qa));
for(int i=0;i<hits.length();i++)
{
System.out.println(i+1);
System.out.println("文档标题:" + hits.doc(i).get("title"));
//返回文件所在路径
System.out.println("文档路径:" +hits.doc(i).get("path"));
//高亮显示部分内容
String suffer=hits.doc(i).get("path").substring(hits.doc(i).get("path").length()-4).toLowerCase();
if(suffer.equals(".txt")||suffer.equals(".htm")||suffer.equals("html")||suffer.equals(".asp")||suffer.equals("aspx")||suffer.equals(".xml"))
s = loadFile(new File(hits.doc(i).get("path")));
else
s=null;
String result="";
if(s!=null)
{
SimpleHTMLFormatter smt=new SimpleHTMLFormatter("<font color='red'>","</font>");
highlighter = new Highlighter(smt,new QueryScorer(qa));
highlighter.setTextFragmenter(new SimpleFragmenter(100));
TokenStream tokenStream =analyzer.tokenStream("contents", new StringReader(hits.doc(i).get("contents")));
result=highlighter.getBestFragments(tokenStream,hits.doc(i).get("contents"),3, "……");
}
System.out.println("内容:" + result);
}
is.close();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
System.exit(0);
}
private void idx(IndexWriter index,String strPath) throws Exception
{
File f=new File(strPath);
File[] fList=f.listFiles();
for(int j=0;j<fList.length;j++)
{
if(fList[j].isDirectory())
{
idx(index,fList[j].getPath()); //在idx函数里面又调用了idx函数本身
}
else if(fList[j].isFile())
{
String doc_path=fList[j].getParent()+"\\"+fList[j].getName();
Document doc=new Document();
Field f1=new Field("title",fList[j].getName(),Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
Field f3=new Field("path",doc_path,Field.Store.YES,Field.Index.NO);
doc.add(f1);
doc.add(f3);
int i_path=doc_path.lastIndexOf(".");
String suffer=doc_path.substring(i_path).toLowerCase();
if(suffer.equals(".txt")||suffer.equals(".htm")||suffer.equals("html")||suffer.equals(".asp")||suffer.equals("aspx")||suffer.equals(".xml"))
{
Field f2=new Field("contents",loadFile(fList[j]),Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
doc.add(f2);
}
index.addDocument(doc);
}
}
}
private String loadFile(File file)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
StringBuffer sb=new StringBuffer();
String line=br.readLine();
while(line!=null)
{
sb.append(line);
line=br.readLine();
}
br.close();
return sb.toString();
}
catch(Exception e)
{
return null;
}
}
public void rebuild_index() throws Exception
{
//建立索引
IndexWriter index=new IndexWriter("./indexs",analyzer,true);
//index.setMaxFieldLength(1000);
idx(index,dir_Path);
index.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -