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

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

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

    laoding
    本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風(fēng)的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
    posts - 0,  comments - 37,  trackbacks - 0
    因為lucene索引的時候是將String型的信息建立索引的,所以這里必須是將word/pdf/html等文件的內(nèi)容轉(zhuǎn)化問字符型。
    lucene的jar包自己去下載。
    首先是建立索引的代碼:

    public class TextFileIndexer {   
        
    public static void main(String[] args) throws Exception {   
            
    /* 指明要索引文件夾的位置,這里是d盤的s文件夾下 */  
            File fileDir 
    = new File("d:\\s");   
      
            
    /* 這里放索引文件的位置 */  
            File indexDir 
    = new File("d:\\index");   
            Analyzer luceneAnalyzer 
    = new StandardAnalyzer();   
            IndexWriter indexWriter 
    = new IndexWriter(indexDir, luceneAnalyzer,   
                    
    true);   
            File[] textFiles 
    = fileDir.listFiles();   
            
    long startTime = new Date().getTime();   
               
            
    //增加document到索引去     
                    System.out.println("File正在被索引.");  
                    
                    
    /*
                     * 注意要變的就是這里,路徑和讀取文件的方法
                     * 
    */
                    String path 
    ="d:\\s\\2.doc";
                    String temp 
    = ReadFile.readWord(path);
    //                String path ="d:\\s\\index.htm"; 
    //                String temp = ReadFile.readHtml(path);
                    Document document = new Document();   
                    Field FieldPath 
    = new Field("path",path, 
                            Field.Store.YES, Field.Index.NO);   
                    Field FieldBody 
    = new Field("body", temp, Field.Store.YES,   
                            Field.Index.TOKENIZED,   
                            Field.TermVector.WITH_POSITIONS_OFFSETS);   
                    document.add(FieldPath);   
                    document.add(FieldBody);   
                    indexWriter.addDocument(document);   
                 
              
            
    //optimize()方法是對索引進(jìn)行優(yōu)化   
            indexWriter.optimize();   
            indexWriter.close();   
               
            
    //測試一下索引的時間   
            long endTime = new Date().getTime();   
            System.out   
                    .println(
    "這花費了"  
                            
    + (endTime - startTime)   
                            
    + " 毫秒來把文檔增加到索引里面去!"  
                            
    + fileDir.getPath());   
        }  
     }

    上面已經(jīng)注釋了要換的地方,我們要做的就是換文件的路徑和讀取文件的方法。

    下面來具體看下讀取文件的方法

    1.首先來看WORD文檔:
    我這里用的是poi,相關(guān)jar包自己去下載,然后加到工程中(以下所要用的jar包也是,不再重復(fù)說)。

    來看相關(guān)代碼:
        public static String readWord(String path) {
            StringBuffer content 
    = new StringBuffer("");// 文檔內(nèi)容
            try {

                HWPFDocument doc 
    = new HWPFDocument(new FileInputStream(path));
                Range range 
    = doc.getRange();
                
    int paragraphCount = range.numParagraphs();// 段落
                for (int i = 0; i < paragraphCount; i++) {// 遍歷段落讀取數(shù)據(jù)
                    Paragraph pp = range.getParagraph(i);
                    content.append(pp.text());
                }

            } 
    catch (Exception e) {

            }
            
    return content.toString().trim();
        }

    2.PDF文件用的是PDFbox:

    public static String readPdf(String path) throws Exception {
            StringBuffer content 
    = new StringBuffer("");// 文檔內(nèi)容
            FileInputStream fis = new FileInputStream(path);
            PDFParser p 
    = new PDFParser(fis);
            p.parse();
            PDFTextStripper ts 
    = new PDFTextStripper();
            content.append(ts.getText(p.getPDDocument()));
            fis.close();
            
    return content.toString().trim();
        }

    3.html文件:

    public static String readHtml(String urlString) {

            StringBuffer content 
    = new StringBuffer("");
            File file 
    = new File(urlString);
            FileInputStream fis 
    = null;
            
    try {
                fis 
    = new FileInputStream(file);
                
    // 讀取頁面
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        fis,
    "utf-8"));//這里的字符編碼要注意,要對上html頭文件的一致,否則會出亂碼
                
                String line 
    = null;

                
    while ((line = reader.readLine()) != null) {
                    content.append(line 
    + "\n");
                }
                reader.close();
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
            String contentString 
    = content.toString();
            
    return contentString;
        }

    4.txt文件:

    public static String readTxt(String path) {
            StringBuffer content 
    = new StringBuffer("");// 文檔內(nèi)容
            try {
                FileReader reader 
    = new FileReader(path);
                BufferedReader br 
    = new BufferedReader(reader);
                String s1 
    = null;

                
    while ((s1 = br.readLine()) != null) {
                    content.append(s1 
    + "\r");
                }
                br.close();
                reader.close();
            } 
    catch (IOException e) {
                e.printStackTrace();
            }
            
    return content.toString().trim();
        }

    接下來數(shù)搜索代碼:

    public class TestQuery {   
        
    public static void main(String[] args) throws IOException, ParseException {   
            Hits hits 
    = null;   
            
    //搜索內(nèi)容自己換
            String queryString = "根據(jù)國務(wù)院的決定";   
            Query query 
    = null;  
            
            IndexSearcher searcher 
    = new IndexSearcher("d:\\index"); //這里注意索引存放的路徑 
      
            Analyzer analyzer 
    = new StandardAnalyzer();   
            
    try {   
                QueryParser qp 
    = new QueryParser("body", analyzer);   
                
    /**
                 * 建索引的時候我們指定了body建立為內(nèi)容,我們搜索的時候也是針對body的,所以
                 *   QueryParser qp = new QueryParser("body", analyzer); 
                 *   這句和建立索引時候
                    Field FieldBody = new Field("body", temp, Field.Store.YES,   
                            Field.Index.TOKENIZED,   
                            Field.TermVector.WITH_POSITIONS_OFFSETS); 
                 *的這句的"body"是對應(yīng)的。
                 
    */
                query 
    = qp.parse(queryString);   
            } 
    catch (ParseException e) {
                System.out.println(
    "異常"); 
            }   
            
    if (searcher != null) {   
                hits 
    = searcher.search(query);   
                
    if (hits.length() > 0) {   
                    System.out.println(
    "找到:" + hits.length() + " 個結(jié)果!");  
                    
    for (int i = 0; i < hits.length(); i++) {//輸出搜索信息 
                         Document document = hits.doc(i);
                         System.out.println(
    "contents:"+document.get("body"));
                         
    //同樣原理這里的document.get("body")就是取得建立在索引文件里面的額body的所有內(nèi)容
                         
    //你若想輸出文件路徑就用document.get("path")就可以了
                    }
                } 
    else{
                    System.out.println(
    "0個結(jié)果!"); 
                }   
            }  
        } 
    posted on 2008-10-31 19:05 老丁 閱讀(10539) 評論(4)  編輯  收藏 所屬分類: 搜索引擎 lucene

    FeedBack:
    # re: lucene索引word/pdf/html/txt文件及檢索(搜索引擎)[未登錄]
    2012-03-21 09:51 | ZXD
    想問,怎么這篇文章 里面的代碼 不行啊 一直報錯、包都引了啊。。。。  回復(fù)  更多評論
      
    # re: lucene索引word/pdf/html/txt文件及檢索(搜索引擎)
    2012-07-23 15:58 |
    為什么自能搜索中文字符串,英文的都不能搜索到  回復(fù)  更多評論
      
    # re: lucene索引word/pdf/html/txt文件及檢索(搜索引擎)
    2014-12-10 14:12 | 古城奔馬
    老兄,你用的是哪個版本的jar包?  回復(fù)  更多評論
      
    # re: lucene索引word/pdf/html/txt文件及檢索(搜索引擎)
    2016-01-07 10:50 | 大頭頭
    String temp = ReadFile.readWord(path);我想請問一下,這里面的ReadFile是哪個類里的對象啊?需要導(dǎo)入什么樣的jar包嗎?  回復(fù)  更多評論
      
    本博客主為學(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)隊

    文章分類(50)

    文章檔案(48)

    相冊

    朋友

    搜索

    •  

    積分與排名

    • 積分 - 96432
    • 排名 - 600

    最新評論

    主站蜘蛛池模板: 国产免费不卡视频| 中文字幕第13亚洲另类| 亚洲精品无码av片| 亚洲精品国产福利一二区| 特级做A爰片毛片免费看无码 | 久久乐国产精品亚洲综合| 99精品免费观看| 日本亚洲欧美色视频在线播放 | 国产a不卡片精品免费观看 | 国产国产成年年人免费看片| 精品国产免费人成网站| 亚洲人成7777| 最新国产AV无码专区亚洲| 久久精品无码一区二区三区免费| 国产精品无码免费专区午夜| 亚洲欧洲日本在线观看| 亚洲日韩中文字幕在线播放| 色视频色露露永久免费观看| 久久精品免费观看国产| 日韩a毛片免费观看| 中文字幕亚洲综合小综合在线 | 四虎成人精品国产永久免费无码| 精品亚洲aⅴ在线观看| 亚洲熟伦熟女新五十路熟妇| 色婷婷7777免费视频在线观看 | 免费无码又爽又高潮视频 | 免费看片免费播放| 99在线在线视频免费视频观看| 久久亚洲精品无码av| 亚洲最新黄色网址| 亚洲视频精品在线| 国产亚洲精品久久久久秋霞 | 久久影视国产亚洲| 手机看片久久国产免费| 久久久高清免费视频| 日韩人妻无码精品久久免费一 | 亚洲综合色成在线播放| 国产精品视_精品国产免费| 妻子5免费完整高清电视| 麻豆成人久久精品二区三区免费| 中国国产高清免费av片|