欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
關(guān)于lucene2.0的創(chuàng )建、檢索和刪除功能的完整實(shí)現 - xiaodaoxiaodao—...

最近要做一個(gè)站內的全文檢索功能,主要是針對 clob 字段的,于是去網(wǎng)上找了點(diǎn) lucene 的資料,現在新版本的是 2.0.0 ,網(wǎng)上的例子多是 1.4.3 的,有些方法已經(jīng)廢棄了,搞了 n 久終于把 2.0.0 的功能實(shí)現了,呵呵,下面把實(shí)現的代碼貼出來(lái),實(shí)現了索引的創(chuàng )建、檢索和刪除功能,并可以從檢索結果去查詢(xún)數據庫 ~  

    // 創(chuàng )建索引

    public void indexFiles() {

         // 創(chuàng )建索引文件存放路徑

        File indexDir = new File("E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index");

 

        try {

            Date start = new Date();

             // 創(chuàng )建分析器 , 主要用于從文本中抽取那些需要建立索引的內容 , 把不需要參與建索引的文本內容去掉 .

            // 比如去掉一些 a the 之類(lèi)的常用詞 , 還有決定是否大小寫(xiě)敏感 .

            StandardAnalyzer standardAnalyzer = new StandardAnalyzer();

             // 參數 true 用于確定是否覆蓋原有索引的

            IndexWriter indexWriter = new IndexWriter(indexDir, standardAnalyzer, true);

            indexWriter.setMergeFactor(100);

            indexWriter.setMaxBufferedDocs(100);

             // 只索引這個(gè) Field 的前 5000 個(gè)字,默認為 10000

              indexWriter.setMaxFieldLength(5000);

             // 從數據庫取出所有紀錄

            List articleList = articleManager.getArticles(null);

            for (int i = 0; i < articleList.size(); i++) {

                Article article = (Article) articleList.get(i);

                 // Document 方法是創(chuàng )建索引的具體代碼

                Document doc = Document(article);

                indexWriter.addDocument(doc);

            }

             // Optimize 的過(guò)程就是要減少剩下的 Segment 的數量 , 盡量讓它們處于一個(gè)文件中 .

            indexWriter.optimize();

              indexWriter.close();

            Date end = new Date();

            System.out.println("create index: " + (end.getTime() - start.getTime()) + " total milliseconds");

        } catch (IOException e) {

            System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());

        }

    }

    public static Document Document(Article article)

            throws java.io.IOException {

        Document doc = new Document();

         // article 表的主健創(chuàng )建索引,關(guān)于 Field 的幾個(gè)參數下面有詳細解釋

        Field fieldId = new Field("uid", article.getArticleId(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES);

         // detail 字段創(chuàng )建索引, detail DB 中是 clob 字段,內容為 html 文本

        String contentHtml = article.getDetail();

        Reader read = new StringReader(contentHtml);

          // HTMLParser detail 字段中的 HTML 分析成文本在索引

        // HTMLParser 這個(gè)類(lèi)可以在 lucene demo 中找到

        HTMLParser htmlParser = new HTMLParser(read);

        BufferedReader breader = new BufferedReader(htmlParser.getReader());

        String htmlContent ="";

        String tempContent = breader.readLine();

        while (tempContent != null && tempContent.length() > 0) {

            htmlContent = htmlContent + tempContent;

            tempContent = breader.readLine();

        }

        Field fieldContents = new Field("content", htmlContent,

                Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES);

        // db 中的每條紀錄對應一個(gè) doc ,每個(gè)字段對應一個(gè) field

        doc.add(fieldId);

        doc.add(fieldContents);

        return doc;

    }

     // 搜索文件, keyword 是你在頁(yè)面上輸入的查找關(guān)鍵字,這里查找的是 detail 字段

    public List searchFiles(String keyword){

        String index = "E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index";

         // hitsList 用來(lái)保存 db 的紀錄,這些紀錄可以通過(guò)查詢(xún)結果取到

        List hitsList = new ArrayList();

        try {

            Date start = new Date();

            IndexReader reader = IndexReader.open(index);

            Searcher searcher = new IndexSearcher(reader);

            Analyzer analyzer = new StandardAnalyzer();

            QueryParser parser = new QueryParser("content", analyzer);

             // 解析查詢(xún)關(guān)鍵字,比如輸入的是以空格等分開(kāi)的多個(gè)查詢(xún)關(guān)鍵字,這里解析后,可以多條件查詢(xún)

            Query query = parser.parse(keyword);

             // hits 用來(lái)保存查詢(xún)結果,這里的 hits 相當于 sql 中的 result

            Hits hits = searcher.search(query);

             for (int i = 0; i < hits.length(); i++) {

                Document doc = hits.doc(i);

                 // 獲得 article 表的主健

                String id = doc.get("uid");

                 // 根據主健去 db 中取紀錄,返回到 hitsList

                try {

                    Article article = articleManager.getArticle(id);

                } catch (ObjectRetrievalFailureException e) {

                    article = null;

                }

                         // 如果沒(méi)有找到該紀錄,表示該紀錄已經(jīng)不存在,不必添加到 hitsList

                if(article!=null)  hitsList.add(article);

            }

            searcher.close();

            reader.close();

            Date end = new Date();

            System.out.println("search files: " + (end.getTime() - start.getTime()) + " total milliseconds");

        } catch (IOException e) {

            System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());

        } catch (ParseException e) {

              System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());

        }

         return hitsList;

    }

     // 刪除索引

    public void deleteIndex(){

        String index = "E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index";

        try {

            Date start = new Date();

            IndexReader reader = IndexReader.open(index);

            int numFiles = reader.numDocs();

            for (int i = 0; i < numFiles; i++) {

                 // 這里的刪除只是給文檔做一個(gè)刪除標記,你可以看到執行 deleteDocument 后會(huì )產(chǎn)生一個(gè) del 后綴的文件,

                // 用來(lái)記錄這些標記過(guò)的文件

                reader.deleteDocument(i);

            }

            reader.close();

            Date end = new Date();

            System.out.println("delete index: " + (end.getTime() - start.getTime()) + " total milliseconds");

        } catch (IOException e) {

             System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());

        }

 

    }

     // 恢復已刪除的索引

    public void unDeleteIndex(){

        String index = "E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index";

        try {

             IndexReader reader = IndexReader.open(index);

            reader.undeleteAll();

            reader.close();

        } catch (IOException e) {

            System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());

         }

 

}

 

Field 就像我們學(xué)過(guò)的數據庫中的字段,簡(jiǎn)單的說(shuō),就是一個(gè)名值對。這個(gè)域有三種屬性,分別是

isStored - 是否被存儲
isIndexed -
是否被索引
isTokenized -
是否分詞

這些屬性的組合又構成了四種不同類(lèi)型的 Field ,而且各有用途

 

Stored

Indexed

Tokenized

Keyword

Y

Y

N

UnIndexed

Y

N

N

UnStored

N

Y

Y

Text: String

Y

Y

Y

Text : Reader

N

Y

Y

 

關(guān)于 Field , 2.0.0 版本和 1.4.3 版本方法相比改動(dòng)比較大,具體見(jiàn)下表

 

1.4.3 版本中的下面方法都被 Field(String name, String value, Store store, Index index, TermVector termVector) 取代

Keyword(String name, String value) // only version 1.4.3
存儲、索引、不分詞,用于 URI (比如 MSN 聊天記錄的日期域、比如 MP3 文件的文件全路徑等等)
Field(String name, String value, Field.Store.YES, Field.Index.UN_TOKENIZED) // version 2.0.0

UnIndexed(String name, String value) // only version 1.4.3
存儲、不索引、不分詞,比如文件的全路徑
Field(String name, String value,Field.Store.YES, Field.Index.NO)// version 2.0.0

UnStored(String name, String value) // only version 1.4.3
不存儲、索引、分詞,比如 HTML 的正文、 Word 的內容等等,這部分內容是要被索引的,但是由于具體內容通常很大,沒(méi)有必要再進(jìn)行存儲,可以到時(shí)候根據 URI 再來(lái)挖取。所以,這部分只分詞、索引,而不存儲。
Field(String name, String value,Field.Store.YES, Field.Index.TOKENIZED)// version 2.0.0

Text(String name, String value) // only version 1.4.3
存儲、索引、分詞,比如文件的各種屬性,比如 MP3 文件的歌手、專(zhuān)輯等等。 Field.Store.YES, Field(String name, String value,Field.Index.TOKENIZED)// version 2.0.0

Text(String name, Reader value) // only version 1.4.3

Field(String name, Reader reader)   // version 2.0.0
不存儲、索引、分詞。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
<轉>Lucene1.4與Lucene2.0的Field_㎡-流逝-時(shí)光-Ψ
lucene入門(mén)與使用
Lucene 索引和搜索過(guò)程核心類(lèi)詳解【基礎】
lucene3.0創(chuàng )建索引及多目錄搜索詳解
Lucene.net 系列二 --- index (上)
lucene爬數據庫中的數據無(wú)非也是查詢(xún)數據。所有我們用lucene搜索數據主要有下面幾個(gè)步驟
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久