隨筆-153  評論-235  文章-19  trackbacks-0
               最近看下Lucene的東西,把它寫下來可以看下。Lucene結構和工作原理我就不說了,網上好多。

          我的環境是Lucene2.0
          寫一個簡單使用Lucene的示例。此類首創建索引,然后顯示索引文檔的情況,最后搜索(只在content找,和在title或content里找)。

          package net.blogjava.chenlb.lucene;

          import org.apache.lucene.analysis.standard.StandardAnalyzer;
          import org.apache.lucene.document.Document;
          import org.apache.lucene.document.Field;
          import org.apache.lucene.index.IndexReader;
          import org.apache.lucene.index.IndexWriter;
          import org.apache.lucene.queryParser.MultiFieldQueryParser;
          import org.apache.lucene.queryParser.QueryParser;
          import org.apache.lucene.search.BooleanClause;
          import org.apache.lucene.search.Hits;
          import org.apache.lucene.search.IndexSearcher;
          import org.apache.lucene.search.Query;

          /**
           * Lucene簡單使用
           * 
          @author chenlb 2008-3-8 下午11:42:55
           
          */
          public class LuceneUse {

              
          public static void main(String[] args) throws Exception {
                  LuceneUse liu 
          = new LuceneUse();
                  
          //索引
                  IndexWriter iw = new IndexWriter("index"new StandardAnalyzer(), true);
                  
          //添加要索引的Lucene文檔
                  Document doc = liu.createDoc("Lucene創建索引示例""chenlb""2008-03-08""Lucene索引的內容在這里,這些內容不被存儲.");
                  iw.addDocument(doc);
                  
                  doc 
          = liu.createDoc("文檔2""bin""2007-10-03""這是索引的另一個文檔");
                  iw.addDocument(doc);
                  
                  doc 
          = liu.createDoc("學習內容""chenlb""2008-3-3""要努力奮斗,祝網友們天天快樂");
                  iw.addDocument(doc);
                  
                  iw.optimize();    
          //優化
                  iw.close();
                  
                  
          //
                  System.out.println("===========索引文檔內容=============");
                  IndexReader reader 
          = IndexReader.open("index");
                  
          for(int i=0; i<reader.numDocs(); i++) {
                      Document d 
          = reader.document(i);
                      liu.printDoc(d);
                  }
                  
                  System.out.println(
          "===========以下是單域查找'天天'結果============");
                  
          //單域搜索
                  IndexSearcher searcher = new IndexSearcher("index");
                  QueryParser parser 
          = new QueryParser("content"new StandardAnalyzer());
                  Query q 
          = parser.parse("天天"); 
                  
                  
          long start = System.currentTimeMillis();
                  Hits hits 
          = searcher.search(q);
                  
          long end = System.currentTimeMillis();
                  
          for(int i=0; i<hits.length(); i++) {
                      liu.printDoc(hits.doc(i));
                  }
                  System.out.println(
          "共找到: "+hits.length()+" 個文檔,花了:"+(end-start)+"ms");
                  
                  
          //多域搜索
                  System.out.println("===========以下多域是查找'內容'結果============");
                  
          //從title或content找
                  q = MultiFieldQueryParser.parse("內容"new String[] {"title""content"}, new BooleanClause.Occur[] {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD}, new StandardAnalyzer());
                  start 
          = System.currentTimeMillis();
                  hits 
          = searcher.search(q);
                  end 
          = System.currentTimeMillis();
                  
          for(int i=0; i<hits.length(); i++) {
                      liu.printDoc(hits.doc(i));
                  }
                  System.out.println(
          "共找到: "+hits.length()+" 個文檔,花了:"+(end-start)+"ms");
              }
              
              
          /**
               * 顯示文檔內容
               
          */
              
          private void printDoc(Document d) {
                  System.out.println(
          "標題: "+d.get("title")+", 作者: "+d.get("author")+", 日期: "+d.get("date")+", 內容: "+d.get("content"));
              }
              
              
          /**
               * 創建一個Lucene文檔
               
          */
              
          private Document createDoc(String title, String author, String date, String content) {
                  Document doc 
          = new Document();
                  doc.add(
          new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
                  doc.add(
          new Field("author", author, Field.Store.YES, Field.Index.NO));
                  doc.add(
          new Field("date", date, Field.Store.YES, Field.Index.NO));
                  doc.add(
          new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
                  
          return doc;
              }
          }
          posted on 2008-03-09 00:47 流浪汗 閱讀(979) 評論(0)  編輯  收藏 所屬分類: Lucene

           
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          留言簿(14)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          友情鏈接

          同學鏈接

          學習鏈接

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 560728
          • 排名 - 86

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 冷水江市| 安溪县| 禹州市| 岳普湖县| 礼泉县| 枞阳县| 泸定县| 蛟河市| 和龙市| 县级市| 边坝县| 贡嘎县| 平乐县| 洞口县| 怀化市| 桐乡市| 杭州市| 察哈| 格尔木市| 绥江县| 湘阴县| 临城县| 麻栗坡县| 武宣县| 栾城县| 陆丰市| 启东市| 马公市| 绍兴县| 新田县| 徐汇区| 宁陵县| 阿图什市| 泰来县| 福州市| 黔西县| 南华县| 福泉市| 丘北县| 水城县| 富顺县|