laoding
          本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風(fēng)的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
          posts - 0,  comments - 37,  trackbacks - 0

          首先下載lucene相關(guān)jar包,這里就不多說,自己網(wǎng)上找

          在eclipse下建立web工程luceneTest

          將jar包加載到你的web工程里面

          新建類Index.java,代碼如下:


          import java.io.IOException;
          import org.apache.lucene.analysis.Analyzer;
          import org.apache.lucene.analysis.SimpleAnalyzer;
          import org.apache.lucene.analysis.standard.StandardAnalyzer;
          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.Directory;
          import org.apache.lucene.store.FSDirectory;
          import org.apache.lucene.store.LockObtainFailedException;
          import org.apache.lucene.store.RAMDirectory;

          /*
           * Create Date:2007-10-26 下午02:52:53
           *
           * Author:dingkm
           *
           * Version: V1.0
           *
           * Description:對(duì)進(jìn)行修改的功能進(jìn)行描述
           *
           *
           */

          public class Index {

           /**
            * @Description 方法實(shí)現(xiàn)功能描述
            * @param args
            *            void
            * @throws 拋出異常說明
            */
           public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
             new Index().index();
             System.out.println("create index success!!!");
            } catch (CorruptIndexException e) {
             e.printStackTrace();
            } catch (LockObtainFailedException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }

           public void index() throws CorruptIndexException, LockObtainFailedException, IOException{
             long start = System.currentTimeMillis();
            
            // 建立索引的路徑
               String path = "c:\\index2";
            Document doc1 = new Document();  
                  doc1.add( new Field("name", "中華人民共和國",Field.Store.YES,Field.Index.TOKENIZED));  
                  doc1.add( new Field("content", "標(biāo)題或正文包括",Field.Store.YES,Field.Index.TOKENIZED)); 
                  doc1.add( new Field("time", "20080715",Field.Store.YES,Field.Index.TOKENIZED));
                  Document doc2 = new Document();  
                  doc2.add(new Field("name", "大中國中國",Field.Store.YES,Field.Index.TOKENIZED));  
                  IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);
                  writer.setMaxMergeDocs(10);
                  writer.setMaxFieldLength(3);  
                  writer.addDocument(doc1);  
                  writer.setMaxFieldLength(3);  
                  writer.addDocument(doc2);  
                  writer.close();  
           
           
                
                  System.out.println("=========================");
                  System.out.print(System.currentTimeMillis() - start);
            System.out.println("total milliseconds");
            System.out.println("=========================");
                 

           }

          }



          執(zhí)行這個(gè)類,可以看到結(jié)果:

          =========================
          375total milliseconds
          =========================
          create index success!!!

          可以看到索引創(chuàng)建成功。


          下面我們來創(chuàng)建搜索類,Search.java

          import java.io.IOException;

          import org.apache.lucene.analysis.standard.StandardAnalyzer;
          import org.apache.lucene.document.Document;
          import org.apache.lucene.index.CorruptIndexException;
          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;

          /*
           * Create Date:2007-10-26 下午02:56:12
           *
           * Author:dingkm
           *
           * Version: V1.0
           *
           * Description:對(duì)進(jìn)行修改的功能進(jìn)行描述
           *
           * 
           */

          public class Search {

           /** 
            *   @Description 方法實(shí)現(xiàn)功能描述 
            *   @param args
            *   void
            *   @throws  拋出異常說明
            */
           public static void main(String[] args) {
            // TODO Auto-generated method stub
             String path = "c:\\index2";
             try {
             new Search().search(path);
            } catch (CorruptIndexException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            } catch (ParseException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }

           }
           
           
           public void search(String path) throws CorruptIndexException, IOException, ParseException{
             IndexSearcher searcher = new IndexSearcher(path);  
                   Hits hits = null;  
                   Query query = null;  
                   QueryParser qp = new QueryParser("name",new StandardAnalyzer());  

                      query = qp.parse("中");
                   hits = searcher.search(query); 
                      java.text.NumberFormat   format   =   java.text.NumberFormat.getNumberInstance();  
                   System.out.println("查找到共" + hits.length() + "個(gè)結(jié)果");  
                      for   (int   i   =   0;   i   <   hits.length();   i++)   {  
                            //開始輸出查詢結(jié)果  
                            Document   doc   =   hits.doc(i);  
                            System.out.println(doc.get("name"));  
                            System.out.println("content="+doc.get("content"));
                            System.out.println("time="+doc.get("time"));
                            System.out.println("準(zhǔn)確度為:"   +   format.format(hits.score(i)   *   100.0)   +   "%");  
          //                  System.out.println(doc.get("CONTENT"));  
                        } 
               
           }

          }


          執(zhí)行它,會(huì)得到以下結(jié)果:

          查找到共2個(gè)結(jié)果
          中華人民共和國
          content=標(biāo)題或正文包括
          time=20080715
          準(zhǔn)確度為:29.727%
          大中國中國
          content=null
          time=null
          準(zhǔn)確度為:29.727%


          這樣就完成了我們的程序

          這是我第一次發(fā)表文章
          說的比較簡(jiǎn)單,可能很多地方說的不清楚
          希望大家多多支持

          有什么不明白的歡迎留言。

          posted on 2008-09-04 13:06 老丁 閱讀(539) 評(píng)論(0)  編輯  收藏 所屬分類: 搜索引擎 lucene
          本博客主為學(xué)習(xí)和復(fù)習(xí)之用,無關(guān)其他,想罵人的繞道
          Email:dkm123456@126.com
          大家一起交流進(jìn)步
          QQ:283582761


          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(4)

          我參與的團(tuán)隊(duì)

          文章分類(50)

          文章檔案(48)

          相冊(cè)

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 96532
          • 排名 - 600

          最新評(píng)論

          主站蜘蛛池模板: 米林县| 伊春市| 屏东市| 沙洋县| 枝江市| 苗栗县| 平和县| 海口市| 泸定县| 郁南县| 岳阳县| 遂平县| 广元市| 扬中市| 舒城县| 石屏县| 郓城县| 巴楚县| 鲁山县| 安西县| 兴山县| 辽阳市| 三原县| 凤台县| 白玉县| 台东县| 德清县| 罗山县| 定边县| 策勒县| 拜泉县| 砀山县| 板桥市| 鸡东县| 苍山县| 丘北县| 晋城| 晋中市| 乌拉特后旗| 衡东县| 化州市|