隨筆-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
          主站蜘蛛池模板: 石景山区| 扬中市| 玉山县| 准格尔旗| 密山市| 施甸县| 即墨市| 昌平区| 辽中县| 海阳市| 晋江市| 綦江县| 恩平市| 松滋市| 尉氏县| 临桂县| 长寿区| 鹿邑县| 来安县| 开平市| 安福县| 朝阳市| 金昌市| 宜昌市| 北宁市| 乌鲁木齐市| 顺义区| 射洪县| 沂源县| 崇信县| 怀宁县| 银川市| 梅河口市| 巩留县| 长乐市| 房产| 泰兴市| 太湖县| 祁阳县| 桃江县| 名山县|