lucence网页索引建立

 

一、创建索引
package bindex;
import java.io.File;
import tool.FileText;
import tool.FileList;
import java.io.*;
                                                                                    
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class FileIndexer {

 /**
  * @param args
  */

 public static void main(String[] args) {
  // TODO Auto-generated method stub
        String indexPath ="indexes";
        try {       
  IndexWriter indexWriter = new IndexWriter(indexPath,new StandardAnalyzer());
        String[] files=FileList.getFiles("htmls");
        int num=files.length;
        for(int i=0;i<num;i++){ 
            Document doc=new Document();   
            File f=new File(files[i]);
            
            String name=f.getName();
            String content=FileText.getText(f);
            String path=f.getPath();
            Field field=new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED);
            doc.add(field);
            field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
            doc.add(field);
            field=new Field("path",path,Field.Store.YES,Field.Index.NO);
            doc.add(field);
            indexWriter.addDocument(doc);  
            System.out.println("File:"+path+name+" indexed!");
        }
     System.out.println("OK!");
     indexWriter.close();
 } catch (CorruptIndexException e) {
   // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (LockObtainFailedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

}

运行结果:

File:htmls\wy\360安全中心 - 360安全卫士官网 - 360安全卫士下载免费杀毒木马查杀杀木马电脑安全恶意软件查杀流氓软件查杀.htm360安全中心 - 360安全卫士官网 - 360安全卫士下载免费杀毒木马查杀杀木马电脑安全恶意软件查杀流氓软件查杀.htm indexed!
File:htmls\腾讯首页11.htm腾讯首页11.htm indexed!
OK!

二、查找

package bindex;
import java.io.IOException;
import java.lang.StringBuffer;

import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.document.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

public class BindexSearcher {
  
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
     String indexPath="indexes";
     String searchField="content";
     String searchPhrase="迅";
     StringBuffer sb=new StringBuffer("");
     
     try {
   IndexSearcher searcher=new IndexSearcher(indexPath);
   Term t=new Term(searchField,searchPhrase);
   Query q=new TermQuery(t);
   Hits hs=searcher.search(q);
   int num=hs.length();
   for (int i=0;i<num;i++){
    Document doc=hs.doc(i);
    Field fname=doc.getField("name");
    Field fcontent=doc.getField("content");    
    sb.append("name:\n");
    sb.append(fname.stringValue()+"\n");
    sb.append("content:\n");
    sb.append(fcontent.stringValue().substring(0, 100)+"\n");
    sb.append("------------"+"\n");    
   }
   searcher.close();
   System.out.println(sb);   
  } catch (CorruptIndexException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

运行结果:
name:
腾讯首页11.htm
content:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html4
------------


三、查找另一内容
package bindex;
import java.io.IOException;
import java.lang.StringBuffer;

import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.document.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

public class BindexSearcher {
  
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
     String indexPath="indexes";
     String searchField="content";
     String searchPhrase="360";
     StringBuffer sb=new StringBuffer("");
     
     try {
   IndexSearcher searcher=new IndexSearcher(indexPath);
   Term t=new Term(searchField,searchPhrase);
   Query q=new TermQuery(t);
   Hits hs=searcher.search(q);
   int num=hs.length();
   for (int i=0;i<num;i++){
    Document doc=hs.doc(i);
    Field fname=doc.getField("name");
    Field fcontent=doc.getField("content");    
    sb.append("name:\n");
    sb.append(fname.stringValue()+"\n");
    sb.append("content:\n");
    sb.append(fcontent.stringValue().substring(0, 100)+"\n");
    sb.append("------------"+"\n");    
   }
   searcher.close();
   System.out.println(sb);   
  } catch (CorruptIndexException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

运行结果:

name:
360安全中心 - 360安全卫士官网 - 360安全卫士下载免费杀毒木马查杀杀木马电脑安全恶意软件查杀流氓软件查杀.htm
content:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html4
------------

四、工具类

package tool;
import java.io.*;

public class FileText {

 /**
  * @param args
  */
    public static String getText(File f){
     
     StringBuffer sb=new StringBuffer("");
     try{
      FileReader fr=new FileReader(f);
      BufferedReader br=new BufferedReader(fr);
      String s=br.readLine();
      while(s!=null){
       sb.append(s);
       s=br.readLine();
      }
      br.close();
     }     
     catch (Exception e){
      sb.append("");      
     }
  return sb.toString();
    }
    public static String getText(String s){
     String t="";
     try{
      File f=new File(s);
      t=getText(f);
     }
     catch (Exception e){
      t="";
     }
     return t;
    }
    public static void main(String[] args) throws IOException{
     String s=FileText.getText("htmls/home.htm");
     System.out.println(s);
    }
}

package tool;
import java.io.*;;

//返回某个目录下的所有文件
public class FileList{
   private static final String SEP="/";
   private static StringBuffer sb=new StringBuffer("");
   
   public static String[] getFiles(File f) throws IOException{
    if(f.isDirectory()){
     File[] fs=f.listFiles();
     for(int i=0;i<fs.length;i++){
      getFiles(fs[i]);
     }
    }
    else{
     sb.append(f.getPath()+SEP);
    }
    String s=sb.toString();
    String[] list=s.split(SEP);
    return list;
   }
   
   public static String[] getFiles(String  t) throws IOException{
    
    File f=new File(t);
    if(f.isDirectory()){
     File[] fs=f.listFiles();
     for(int i=0;i<fs.length;i++){
      getFiles(fs[i]);
     }
    }
    else{
     sb.append(f.getPath()+SEP);
    }
    String s=sb.toString();
    String[] list=s.split(SEP);
    return list;
   }        
}

 

你可能感兴趣的:(网页索引)