⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 视频文字.txt

📁 lucene视频教程(附源码)!!欢迎下载!
💻 TXT
字号:
大家好。我系爱迪。今日畀大家一个视频教程大家。系关于lucene的使用的。
1:建一个索引类。
y_indexer.java
2:建立搜索类
y_searcher.java
好。打开eclipse新建两个类
y_indexer
y_searcher
关于源码。我已同视频一起打包了。
而家开始!睇我操作啦!!
要建立两个folder一个是y一个是yuyang yuyang这个folder现在还是空的。等一下运行就会有文件了。运行一下y_indexer这个类先
y是要建立索引的.html文件
yuyang
是索引文件存放的folder
run一下先。正在建立索引中.........
好了。y folder中的.html文件全部都建立索引了。到yuyang这个folder去睇睇先.
睇到没。索引建立好了。现在就是搜索了。

也run一下先!
然后再建立search类。来search一下刚才所建立的索引
睇到没。好了。
end..............
QQ:306037774
author:eddy


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class Y_searcher {
	  
	 public List search(){
		 List searchResult = new ArrayList();//创建一个List接口的一个实例类ArrayList类
		 try{
		 Hits hits = null;   
	     String key = "手机短信";   
	     Query query = null;   
	     IndexSearcher searcher = new IndexSearcher("C:\\yuyang"); 
	        Analyzer analyzer = new StandardAnalyzer();   //创建一个Analyzer接口的一个实例类StandardAnalyzer
	       
	            QueryParser qp = new QueryParser("title", analyzer);   
	            query = qp.parse(key);   
	       
	        if (searcher != null) {   
	        	  Date start=new Date();
	            hits = searcher.search(query);   //遍历hist结果的length
	            if(hits.length()==0){
	            	 System.out.println("对唔住。没你想要的结果!");
	            }
	            else{
	            for(int i=0;i<hits.length();i++){ 
	            	 Date end=new Date();
	              //  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(ParseException ex){
		        	  
		          }
	            catch(IOException e){
	            	
	            }
	            return searchResult;
	            }   

	 public static void main(String args[]){
		      Y_searcher y_s=new Y_searcher();
		      y_s.search();
	 }

}


import java.io.BufferedReader;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.util.Date;   
  
import org.apache.lucene.analysis.Analyzer;   
import org.apache.lucene.analysis.standard.StandardAnalyzer;   
import org.apache.lucene.document.Document;   
import org.apache.lucene.document.Field;   
import org.apache.lucene.index.IndexWriter;   

public class Y_indexer {
	  public static void main(String[] args) throws Exception {   
	        /* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */  
	        File fileDir = new File("C:\\y");   
	  
	        /* 这里放索引文件的位置 */  
	        File indexDir = new File("c:\\yuyang");   
	        Analyzer luceneAnalyzer = new StandardAnalyzer();   
	        IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,   
	                true);   
	        File[] textFiles = fileDir.listFiles();   
	        long startTime = new Date().getTime();   
	           
	        //增加document到索引去   
	        for (int i = 0; i < textFiles.length; i++) {   
	            if (textFiles[i].isFile()   
	                    && textFiles[i].getName().endsWith(".html")) {   
	                System.out.println("File " + textFiles[i].getCanonicalPath()   
	                        + "正在被索引....");   
	                String temp = FileReaderAll(textFiles[i].getCanonicalPath(),   
	                        "GBK");   
	                System.out.println(temp);   
	                Document document = new Document();   
	                Field FieldPath = new Field("path", textFiles[i].getPath(),   
	                        Field.Store.YES, Field.Index.NO);   
	                Field FieldBody = new Field("body", temp, Field.Store.YES,   
	                        Field.Index.TOKENIZED,   
	                        Field.TermVector.WITH_POSITIONS_OFFSETS);   
	                Field FieldTitle = new Field("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();   
	           
	        //测试一下索引的时间   
	        long endTime = new Date().getTime();   
	        System.out   
	                .println("这花费了"  
	                        + (endTime - startTime)   
	                        + " 毫秒来把文档增加到索引里面去!"  
	                        + fileDir.getPath());   
	    }   
	  
	    public static String FileReaderAll(String FileName, String charset)   
	            throws IOException {   
	        BufferedReader reader = new BufferedReader(new InputStreamReader(   
	                new FileInputStream(FileName), charset));   
	        String line = new String();   
	        String temp = new String();   
	           
	        while ((line = reader.readLine()) != null) {   
	            temp += line;   
	        }   
	        reader.close();   
	        return temp;   
	    }   
	}  



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -