天天看點

Lucene學習筆記(應用)

lucene學習筆記

一lucene配置

1:下載下傳lucene

       到官方網站下載下傳最新版的lucene,最新版本是2.1的。下載下傳網址http://lucene.apache.org/java/docs/index.html ,在windows下應用,隻需下載下傳lucene-2.1.0-src.zip,lucene-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.zip,nlpack1-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:運作eclipse,eclipse會自動加載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));

          }     

}

添加記錄

import java.io.filereader;

import java.io.reader;

import org.apache.lucene.document.document;

import org.apache.lucene.document.field;

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();

進行查詢

epackage lucene;

import java.util.arraylist;

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;

                  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"));

              }

           }

              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));

繼續閱讀