Lucene学习笔记(应用)

Lucene学习笔记

lucene配置

1:下载lucene

       到官方网站下载最新版的lucene,最新版本是2.1的。下载网址http://lucene.apache.org/java/docs/index.html ,在windows下应用,只需下载lucene- 2.1.0 -src.ziplucene-2.1.0.zip两个zip文件即可。

2:配置java环境,参考我写的
WinNT+JDK+TomCat+AXIS+MySQL+MYSQLAdministrator+WinTookit详细配置
http://blog.csdn.net/ugg/archive/2006/03/02/614164.aspx 

3:下载eclipse

到官方网站下载eclipse最新版本,
中文版推荐下载http://down.oyksoft.com/Download.asp?ID=1854
中文包下载http://down.oyksoft.com/Download.asp?ID=2973 下载到本地,分别把eclipse-SDK- 3.2.1 -win32.zipNLpack1-eclipse-SDK-3.2.1-win32.zip解压,然后把NLpack1-eclipse-SDK-3.2.1-win32.zip文件夹内的内容覆盖到eclipse-SDK-3.2.1-win32.zip解压的文件内,此时运行eclipse,就可以应用中文版的eclipse

4:运行eclipseeclipse会自动加载java包,我们应用lucenc时,需要把lucene-core- 2.1.0 .jar加入加入eclipse项目中,我们创建一个简单的创建索引,添加记录,查询。

创建索引文件

package lucene;
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;

public class CreateDataBase {
       public static void main(String[] args){
           CreateDataBase temp= new CreateDataBase();
           if(temp.createDataBase("e://lucene//holendb")==1){
               System.out.println("db init succ");
           }
        }

        public CreateDataBase(){
           }
        public int createDataBase(File file){
               int returnValue=0;
               if(!file.isDirectory()){
                      file.mkdirs();
            }
               try{
                      IndexWriter indexWriter= new IndexWriter(file,new StandardAnalyzer(),true);
                indexWriter.close();
                returnValue=1;
              }              

               catch(Exception ex){
                  ex.printStackTrace();
              }
              return returnValue;
           }

          public int createDataBase(String file){
              return this.createDataBase(new File(file));
      
   }     
}
 

添加记录

package lucene;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
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 InsertRecords{
        public static void main(String[] args){
              InsertRecords temp= new InsertRecords();
              String dbpath="e://lucene//holendb";
              //holen1.txt
中包含关键字"holen""java"
              if(temp.insertRecords(dbpath,"e://lucene//ugg1.txt")==1){
                  System.out.println("add file1 succ");
              }

              //holen2.txt中包含关键字"holen""chen"
              if(temp.insertRecords(dbpath,"e://lucene//ugg2.txt")==1){
                  System.out.println("add file2 succ");
              }
           }      

    public InsertRecords(){
    }   

    public int insertRecords(String dbpath,File file){
           int returnValue=0;
           try{
                  IndexWriter indexWriter = new IndexWriter(dbpath,new StandardAnalyzer(),false);
                  this.addFiles(indexWriter,file);
                  returnValue=1;

       }catch(Exception ex){
           ex.printStackTrace();
       }
       return returnValue;
    }

    public int insertRecords(String dbpath,String file){
       return this.insertRecords(dbpath,new File(file));
    }   

    public void addFiles(IndexWriter indexWriter,File file){
       Document doc= new Document();
       try{
              doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.UN_TOKENIZED));
           //
以下两句只能取一句,前者是索引不存储,后者是索引且存储
              doc.add(new Field("contents", new FileReader(file)));
           indexWriter.addDocument(doc);
           indexWriter.close();
       }catch(Exception ex){
           ex.printStackTrace();
       }
    }
}

 

进行查询

epackage lucene;

import java.util.ArrayList;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

public class QueryRecords{
    public QueryRecords(){
    } 

    public ArrayList queryRecords(String searchkey,String dbpath,String searchfield){
           ArrayList list= null;
           try{
                  Searcher searcher= new IndexSearcher(dbpath);
                  QueryParser parser = new QueryParser(searchfield, new StandardAnalyzer());
                  Query query = parser.parse(searchkey);
                  Hits hits=searcher.search(query);
                  if(hits!= null){
                         list= new ArrayList();
                         int temp_hitslength=hits.length();
                         Document doc= null;
                         for(int i=0;i<temp_hitslength;i++){
                                doc=hits.doc(i);
                                list.add(doc.get("filename"));
              }
           }
       }catch(Exception ex){
              ex.printStackTrace();
       }
       return list;
    }

    public static void main(String[] args){
       QueryRecords temp= new QueryRecords();   
      
ArrayList list= null;
       list=temp.queryRecords("acronyms","e://lucene//holendb","contents");
       for(int i=0;i<list.size();i++){
           System.out.println((String)list.get(i));
       }
    }
}

 

你可能感兴趣的:(eclipse,exception,String,File,Lucene,import)