- Y_indexer.java建索引
- packagecom.hapark.lucene;
- importjava.io.BufferedReader;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.io.InputStreamReader;
- importjava.util.ArrayList;
- importjava.util.Date;
- importorg.apache.lucene.analysis.Analyzer;
- importorg.apache.lucene.analysis.standard.StandardAnalyzer;
- importorg.apache.lucene.document.Document;
- importorg.apache.lucene.document.Field;
- importorg.apache.lucene.index.IndexWriter;
- publicclassY_indexer{
- publicstaticvoidmain(String[]args)throwsException{
- /*指明要索引文件夹的位置,这里是D盘的y文件夹下*/
- FilefileDir=newFile("d:\\y");
- /*这里放索引文件的位置*/
- FileindexDir=newFile("d:\\index");
- AnalyzerluceneAnalyzer=newStandardAnalyzer();
- IndexWriterindexWriter=newIndexWriter(indexDir,luceneAnalyzer,
- true);
- ArrayListlist=newArrayList();
- getList(fileDir,list);
- File[]textFiles=(File[])list.toArray(newFile[0]);
- System.out.println(textFiles.length);
- longstartTime=newDate().getTime();
- //增加document到索引去
- for(inti=0;i<textFiles.length;i++){
- if(textFiles[i].isFile()
- &&textFiles[i].getName().endsWith(".html")){
- System.out.println("File"+textFiles[i].getCanonicalPath()
- +"正在被索引....");
- Stringtemp=FileReaderAll(textFiles[i].getCanonicalPath(),
- "GBK");
- System.out.println(temp);
- Documentdocument=newDocument();
- FieldFieldPath=newField("path",textFiles[i].getPath(),
- Field.Store.YES,Field.Index.NO);
- FieldFieldBody=newField("body",temp,Field.Store.YES,
- Field.Index.TOKENIZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
- FieldFieldTitle=newField("title",temp,Field.Store.YES,
- Field.Index.TOKENIZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
- document.add(FieldPath);
- document.add(FieldBody);
- document.add(FieldTitle);
- indexWriter.addDocument(document);
- }
- }
- //optimize()方法是对索引进行优化
- indexWriter.optimize();
- indexWriter.close();
- //测试一下索引的时间
- longendTime=newDate().getTime();
- System.out
- .println("这花费了"
- +(endTime-startTime)
- +"毫秒来把文档增加到索引里面去!"
- +fileDir.getPath());
- }
- /**
- *多层文件夹
- *@paramfile
- *@paramlist
- */
- publicstaticvoidgetList(Filefile,ArrayListlist){
- if(file.isDirectory()&&file.getName().indexOf(".")!=0){
- File[]file2=file.listFiles();
- for(inti=0;i<file2.length;i++)
- getList(file2[i],list);
- }else{
- list.add(file);
- }
- }
- publicstaticStringFileReaderAll(StringFileName,Stringcharset)
- throwsIOException{
- BufferedReaderreader=newBufferedReader(newInputStreamReader(
- newFileInputStream(FileName),charset));
- Stringline=newString();
- Stringtemp=newString();
- while((line=reader.readLine())!=null){
- temp+=line;
- }
- reader.close();
- returntemp;
- }
- }
- Y_searcherlucene搜索
- packagecom.hapark.lucene;
- importjava.io.IOException;
- importjava.util.ArrayList;
- importjava.util.Date;
- importjava.util.List;
- importorg.apache.lucene.analysis.Analyzer;
- importorg.apache.lucene.analysis.standard.StandardAnalyzer;
- importorg.apache.lucene.queryParser.ParseException;
- importorg.apache.lucene.queryParser.QueryParser;
- importorg.apache.lucene.search.Hits;
- importorg.apache.lucene.search.IndexSearcher;
- importorg.apache.lucene.search.Query;
- publicclassY_searcher{
- publicListsearch(){
- ListsearchResult=newArrayList();//创建一个List接口的一个实例类ArrayList类
- try{
- Hitshits=null;
- Stringkey="苏";
- Queryquery=null;
- IndexSearchersearcher=newIndexSearcher("d:\\yuyang");
- Analyzeranalyzer=newStandardAnalyzer();//创建一个Analyzer接口的一个实例类StandardAnalyzer
- QueryParserqp=newQueryParser("title",analyzer);
- query=qp.parse(key);
- if(searcher!=null){
- Datestart=newDate();
- hits=searcher.search(query);//遍历hist结果的length
- if(hits.length()==0){
- System.out.println("对不起。没你想要的结果!");
- }
- else{
- for(inti=0;i<hits.length();i++){
- Dateend=newDate();
- //System.out.println("找到:"+hits.length()+"Totalresult!");
- System.out.println("文件的路径:"+hits.doc(i).get("path"));
- //System.out.println("内容:"+hits.doc(i).get("body"));
- System.out.println(hits.doc(i).get("title"));
- System.out.println("检索完成,用时"+(end.getTime()-start.getTime())+"毫秒");
- }
- }
- }
- }
- catch(ParseExceptionex){
- }
- catch(IOExceptione){
- }
- returnsearchResult;
- }
- publicstaticvoidmain(Stringargs[]){
- Y_searchery_s=newY_searcher();
- y_s.search();
- }
- }