`
ljm3256748
  • 浏览: 43290 次
  • 性别: Icon_minigender_1
  • 来自: 地球
社区版块
存档分类
最新评论

转载:关于 lucene2.0 的创建、检索和删除功能的完整实现

阅读更多

 

最近要做一个站内的全文检索功能,主要是针对 clob 字段的,于是去网上找了点 lucene 的资料,现在新版本的是  2.0.0  ,网上的例子多是 1.4.3 的,有些方法已经废弃了,搞了 n 久终于把 2.0.0 的功能实现了,呵呵,下面把实现的代码贴出来,实现了索引的创建、检索和删除功能,并可以从检索结果去查询数据库 ~

<o:p></o:p>

    // 创建索引 <o:p></o:p>


    public void indexFiles() { <o:p></o:p>


         // 创建索引文件存放路径 <o:p></o:p>


        File indexDir = new File("E:\\lucene_Learning\\lucene-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">2.0.0</st1:chsdate>src\\src\\demo\\index"); <o:p></o:p>


  <o:p></o:p>


        try { <o:p></o:p>


            Date start = new Date(); <o:p></o:p>


             // 创建分析器 , 主要用于从文本中抽取那些需要建立索引的内容 , 把不需要参与建索引的文本内容去掉 . <o:p></o:p>


            // 比如去掉一些 a the 之类的常用词 , 还有决定是否大小写敏感 . <o:p></o:p>


            StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); <o:p></o:p>


             // 参数 true 用于确定是否覆盖原有索引的 <o:p></o:p>


            IndexWriter indexWriter = new IndexWriter(indexDir, standardAnalyzer, true); <o:p></o:p>


            indexWriter.setMergeFactor(100); <o:p></o:p>


            indexWriter.setMaxBufferedDocs(100); <o:p></o:p>


             // 只索引这个 Field 的前 5000 个字,默认为 10000 <o:p></o:p>


              indexWriter.setMaxFieldLength(5000); <o:p></o:p>


             // 从数据库取出所有纪录 <o:p></o:p>


            List articleList = articleManager.getArticles(null); <o:p></o:p>


            for (int i = 0; i < articleList.size(); i++) { <o:p></o:p>


                Article article = (Article) articleList.get(i); <o:p></o:p>


                 // Document 方法是创建索引的具体代码 <o:p></o:p>


                Document doc = Document(article); <o:p></o:p>


                indexWriter.addDocument(doc); <o:p></o:p>


            } <o:p></o:p>


             // Optimize 的过程就是要减少剩下的 Segment 的数量 , 尽量让它们处于一个文件中 . <o:p></o:p>


            indexWriter.optimize(); <o:p></o:p>


              indexWriter.close(); <o:p></o:p>


            Date end = new Date(); <o:p></o:p>


            System.out.println("create index: " + (end.getTime() - start.getTime()) + " total milliseconds"); <o:p></o:p>


        } catch (IOException e) { <o:p></o:p>


            System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); <o:p></o:p>


        } <o:p></o:p>


    } <o:p></o:p>


    public static Document Document(Article article) <o:p></o:p>


            throws java.io.IOException { <o:p></o:p>


        Document doc = new Document(); <o:p></o:p>


         // article 表的主健创建索引,关于 Field 的几个参数下面有详细解释 <o:p></o:p>


        Field fieldId = new Field("uid", article.getArticleId(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES); <o:p></o:p>


         // detail 字段创建索引, detail DB 中是 clob 字段,内容为 html 文本 <o:p></o:p>


        String contentHtml = article.getDetail(); <o:p></o:p>


        Reader read = new StringReader(contentHtml); <o:p></o:p>


          // HTMLParser detail 字段中的 HTML 分析成文本在索引 <o:p></o:p>


        // HTMLParser 这个类可以在 lucene demo 中找到 <o:p></o:p>


        HTMLParser htmlParser = new HTMLParser(read); <o:p></o:p>


        BufferedReader breader = new BufferedReader(htmlParser.getReader()); <o:p></o:p>


        String htmlContent =""; <o:p></o:p>


        String tempContent = breader.readLine(); <o:p></o:p>


        while (tempContent != null && tempContent.length() > 0) { <o:p></o:p>


            htmlContent = htmlContent + tempContent; <o:p></o:p>


            tempContent = breader.readLine(); <o:p></o:p>


        } <o:p></o:p>


        Field fieldContents = new Field("content", htmlContent, <o:p></o:p>


                Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES); <o:p></o:p>


        // db 中的每条纪录对应一个 doc ,每个字段对应一个 field <o:p></o:p>


        doc.add(fieldId); <o:p></o:p>


        doc.add(fieldContents); <o:p></o:p>


        return doc; <o:p></o:p>


    } <o:p></o:p>


     // 搜索文件, keyword 是你在页面上输入的查找关键字,这里查找的是 detail 字段 <o:p></o:p>


    public List searchFiles(String keyword){ <o:p></o:p>


        String index = "E:\\lucene_Learning\\lucene-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">2.0.0</st1:chsdate>src\\src\\demo\\index"; <o:p></o:p>


         // hitsList 用来保存 db 的纪录,这些纪录可以通过查询结果取到 <o:p></o:p>


        List hitsList = new ArrayList(); <o:p></o:p>


        try { <o:p></o:p>


            Date start = new Date(); <o:p></o:p>


            IndexReader reader = IndexReader.open(index); <o:p></o:p>


            Searcher searcher = new IndexSearcher(reader); <o:p></o:p>


            Analyzer analyzer = new StandardAnalyzer(); <o:p></o:p>


            QueryParser parser = new QueryParser("content", analyzer); <o:p></o:p>


             // 解析查询关键字,比如输入的是以空格等分开的多个查询关键字,这里解析后,可以多条件查询 <o:p></o:p>


            Query query = parser.parse(keyword); <o:p></o:p>


             // hits 用来保存查询结果,这里的 hits 相当于 sql 中的 result <o:p></o:p>


            Hits hits = searcher.search(query); <o:p></o:p>


             for (int i = 0; i < hits.length(); i++) { <o:p></o:p>


                Document doc = hits.doc(i); <o:p></o:p>


                 // 获得 article 表的主健 <o:p></o:p>


                String id = doc.get("uid"); <o:p></o:p>


                <sp>


<o:p></o:p>



Stored <o:p></o:p>



Indexed <o:p></o:p>



Tokenized <o:p></o:p>



Keyword <o:p></o:p>



Y <o:p></o:p>



Y <o:p></o:p>



N <o:p></o:p>



UnIndexed <o:p></o:p>



Y <o:p></o:p>



N <o:p></o:p>



N <o:p></o:p>



UnStored <o:p></o:p>



N <o:p></o:p>



Y <o:p></o:p>



Y <o:p></o:p>



Text: String <o:p></o:p>



Y <o:p></o:p>



Y <o:p></o:p>



Y <o:p></o:p>



Text : Reader <o:p></o:p>



N <o:p></o:p>



Y <o:p></o:p>



Y <o:p></o:p>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics