<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    DANCE WITH JAVA

    開發出高質量的系統

    常用鏈接

    統計

    積分與排名

    好友之家

    最新評論

    apache lucene 一個最簡單的實例

    就像每個程序都有一個Hello World來讓人體驗它一樣,lucene也可以很簡單的提供一個實例。如下(來自lucene in action的例子)有兩個類組成:
    一個是建立索引
    package my;

    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Date;

    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 Indexer {
        
    public static void main(String[] args) throws Exception {
            
    if (args.length != 2{
                
    throw new Exception("Usage: java " + Indexer.class.getName()
                        
    + " <index dir> <data dir>");
            }

            File indexDir 
    = new File(args[0]);
            File dataDir 
    = new File(args[1]);
            
    long start = new Date().getTime();
            
    int numIndexed = index(indexDir, dataDir);
            
    long end = new Date().getTime();
            System.out.println(
    "Indexing " + numIndexed + " files took "
                    
    + (end - start) + " milliseconds");
        }


        
    // open an index and start file directory traversal
        public static int index(File indexDir, File dataDir) throws IOException {
            
    if (!dataDir.exists() || !dataDir.isDirectory()) {
                
    throw new IOException(dataDir
                        
    + " does not exist or is not a directory");
            }

            IndexWriter writer 
    = new IndexWriter(indexDir, new StandardAnalyzer(),
                    
    true);
            writer.setUseCompoundFile(
    false);
            indexDirectory(writer, dataDir);
            
    int numIndexed = writer.docCount();
            writer.optimize();
            writer.close();
            
    return numIndexed;
        }


        
    // recursive method that calls itself when it finds a directory
        private static void indexDirectory(IndexWriter writer, File dir)
                
    throws IOException {
            File[] files 
    = dir.listFiles();
            
    for (int i = 0; i < files.length; i++{
                File f 
    = files[i];
                
    if (f.isDirectory()) {
                    indexDirectory(writer, f);
                }
     else if (f.getName().endsWith(".txt")) {
                    indexFile(writer, f);
                }

            }

        }


        
    // method to actually index file using Lucene
        private static void indexFile(IndexWriter writer, File f)
                
    throws IOException {
            
    if (f.isHidden() || !f.exists() || !f.canRead()) {
                
    return;
            }

            System.out.println(
    "Indexing " + f.getCanonicalPath());
            Document doc 
    = new Document();
            doc.add(Field.Text(
    "contents"new FileReader(f)));
            doc.add(Field.Keyword(
    "filename", f.getCanonicalPath()));
            writer.addDocument(doc);
        }

    }
    另一個是搜索:
    package my;

    import java.io.File;
    import java.util.Date;

    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.store.Directory;
    import org.apache.lucene.store.FSDirectory;

    public class Searcher {
        
    public static void main(String[] args) throws Exception {
            
    if (args.length != 2{
                
    throw new Exception("Usage: java " + Searcher.class.getName()
                        
    + " <index dir> <auery>");
            }

            File indexDir 
    = new File(args[0]);
            String q 
    = args[1];
            
    if (!indexDir.exists() || !indexDir.isDirectory()) {
                
    throw new Exception(indexDir
                        
    + " does not exist or is not a directory.");
            }

            search(indexDir, q);
        }


        
    public static void search(File indexDir, String q) throws Exception {
            Directory fsDir 
    = FSDirectory.getDirectory(indexDir, false);
            IndexSearcher is 
    = new IndexSearcher(fsDir);
            Query query 
    = QueryParser.parse(q, "contents"new StandardAnalyzer());
            
    long start = new Date().getTime();
            Hits hits 
    = is.search(query);
            
    long end = new Date().getTime();
            System.err.println(
    "Found " + hits.length() + " document(s) (in "
                    
    + (end - start) + " milliseconds) that matched query ‘" + q
                    
    + "’:");
            
    for (int i = 0; i < hits.length(); i++{
                Document doc 
    = hits.doc(i);
                System.out.println(doc.get(
    "filename"));
            }

        }

    }
    ok,這樣就簡單實現了,在搜索目錄下所有txt,找出包括某一個字符串的txt文件名的功能。
    下篇文章將介紹一下lucene的核心類

    posted on 2007-06-12 09:46 dreamstone 閱讀(5084) 評論(5)  編輯  收藏 所屬分類: 搜索引擎lucence

    評論

    # re: apache lucene 一個最簡單的實例 2010-05-25 09:35 yuanfangzhou

    太好了,非常感謝!  回復  更多評論   

    # re: apache lucene 一個最簡單的實例 2010-07-15 15:32 solidfish

    你的博客寫的很好,支持你哦!  回復  更多評論   

    # re: apache lucene 一個最簡單的實例 2010-07-23 17:34 人字拖

    good  回復  更多評論   

    # re: apache lucene 一個最簡單的實例[未登錄] 2011-04-29 21:05 Talin

    不錯,很好。另外,我的博客里發了一篇類似的相關的入門示例,可以去看看http://javafans.info  回復  更多評論   

    # re: apache lucene 一個最簡單的實例[未登錄] 2013-11-14 15:35 椰子

    樓主,您好。你這個lucene的版本是?   回復  更多評論   

    主站蜘蛛池模板: 波多野结衣在线免费视频| 久久久久久久久久久免费精品 | 国产免费黄色大片| 亚洲人成电影在线观看青青| 无码人妻久久一区二区三区免费| 亚洲91av视频| 精品无码国产污污污免费网站| 91在线亚洲精品专区| 亚洲一级免费毛片| ww亚洲ww在线观看国产| 嫩草影院免费观看| 粉色视频免费入口| 国产亚洲精久久久久久无码AV| 一级大黄美女免费播放| 亚洲无线码一区二区三区| 国产免费一区二区三区不卡| 99久久亚洲精品无码毛片| 18禁免费无码无遮挡不卡网站| 亚洲国产一区二区三区在线观看| 免费一级毛片不卡在线播放| 久久久精品视频免费观看| 久久久久亚洲AV无码麻豆| 最近2019中文免费字幕| 美女18毛片免费视频| 国产v亚洲v天堂无码网站| 曰批全过程免费视频播放网站 | 精品久久亚洲一级α| 亚洲精品成人在线| 一级毛片免费观看不卡视频| 亚洲美免无码中文字幕在线| 在线观看免费a∨网站| v片免费在线观看| 亚洲视频国产视频| 国产免费一区二区三区VR| 中文毛片无遮挡高清免费| 亚洲第一永久在线观看| 国产精品视频免费一区二区三区| 成在人线av无码免费高潮喷水| 亚洲一本之道高清乱码| 亚洲精品国产高清嫩草影院| 3d动漫精品啪啪一区二区免费|