---
title: "转载：关于 lucene2.0 的创建、检索和删除功能的完整实现"
author: "ljm3256748"
date: "2007-05-14T21:22:13Z"
tags: []
slug: "lucene2-0"
draft: false
lastmod: "2007-05-14T21:22:13Z"
id: 79897
url: "https://jimmy.iteye.com/blog/79897"
---

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

&lt;o:p&gt;&lt;/o:p&gt;

    **//** **创建索引** &lt;o:p&gt;&lt;/o:p&gt;

    public void indexFiles() { &lt;o:p&gt;&lt;/o:p&gt;

         // 创建索引文件存放路径 &lt;o:p&gt;&lt;/o:p&gt;

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

  &lt;o:p&gt;&lt;/o:p&gt;

        try { &lt;o:p&gt;&lt;/o:p&gt;

            Date start = new Date(); &lt;o:p&gt;&lt;/o:p&gt;

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

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

            StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); &lt;o:p&gt;&lt;/o:p&gt;

             // 参数 true 用于确定是否覆盖原有索引的 &lt;o:p&gt;&lt;/o:p&gt;

            IndexWriter indexWriter = new IndexWriter(indexDir, standardAnalyzer, true); &lt;o:p&gt;&lt;/o:p&gt;

            indexWriter.setMergeFactor(100); &lt;o:p&gt;&lt;/o:p&gt;

            indexWriter.setMaxBufferedDocs(100); &lt;o:p&gt;&lt;/o:p&gt;

             // 只索引这个 Field 的前 5000 个字，默认为 10000 &lt;o:p&gt;&lt;/o:p&gt;

              indexWriter.setMaxFieldLength(5000); &lt;o:p&gt;&lt;/o:p&gt;

             // 从数据库取出所有纪录 &lt;o:p&gt;&lt;/o:p&gt;

            List articleList = articleManager.getArticles(null); &lt;o:p&gt;&lt;/o:p&gt;

            for (int i = 0; i &lt; articleList.size(); i++) { &lt;o:p&gt;&lt;/o:p&gt;

                Article article = (Article) articleList.get(i); &lt;o:p&gt;&lt;/o:p&gt;

                 // 在 Document 方法是创建索引的具体代码 &lt;o:p&gt;&lt;/o:p&gt;

                Document doc = Document(article); &lt;o:p&gt;&lt;/o:p&gt;

                indexWriter.addDocument(doc); &lt;o:p&gt;&lt;/o:p&gt;

            } &lt;o:p&gt;&lt;/o:p&gt;

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

            indexWriter.optimize(); &lt;o:p&gt;&lt;/o:p&gt;

              indexWriter.close(); &lt;o:p&gt;&lt;/o:p&gt;

            Date end = new Date(); &lt;o:p&gt;&lt;/o:p&gt;

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

        } catch (IOException e) { &lt;o:p&gt;&lt;/o:p&gt;

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

        } &lt;o:p&gt;&lt;/o:p&gt;

    } &lt;o:p&gt;&lt;/o:p&gt;

    public static Document Document(Article article) &lt;o:p&gt;&lt;/o:p&gt;

            throws java.io.IOException { &lt;o:p&gt;&lt;/o:p&gt;

        Document doc = new Document(); &lt;o:p&gt;&lt;/o:p&gt;

         // 为 article 表的主健创建索引，关于 Field 的几个参数下面有详细解释 &lt;o:p&gt;&lt;/o:p&gt;

        Field fieldId = new Field("uid", article.getArticleId(), Field.Store.YES, Field.Index.UN\_TOKENIZED, Field.TermVector.YES); &lt;o:p&gt;&lt;/o:p&gt;

         // 为 detail 字段创建索引， detail 在 DB 中是 clob 字段，内容为 html 文本 &lt;o:p&gt;&lt;/o:p&gt;

        String contentHtml = article.getDetail(); &lt;o:p&gt;&lt;/o:p&gt;

        Reader read = new StringReader(contentHtml); &lt;o:p&gt;&lt;/o:p&gt;

          // 用 HTMLParser 把 detail 字段中的 HTML 分析成文本在索引 &lt;o:p&gt;&lt;/o:p&gt;

        // HTMLParser 这个类可以在 lucene 的 demo 中找到 &lt;o:p&gt;&lt;/o:p&gt;

        HTMLParser htmlParser = new HTMLParser(read); &lt;o:p&gt;&lt;/o:p&gt;

        BufferedReader breader = new BufferedReader(htmlParser.getReader()); &lt;o:p&gt;&lt;/o:p&gt;

        String htmlContent =""; &lt;o:p&gt;&lt;/o:p&gt;

        String tempContent = breader.readLine(); &lt;o:p&gt;&lt;/o:p&gt;

        while (tempContent != null && tempContent.length() &gt; 0) { &lt;o:p&gt;&lt;/o:p&gt;

            htmlContent = htmlContent + tempContent; &lt;o:p&gt;&lt;/o:p&gt;

            tempContent = breader.readLine(); &lt;o:p&gt;&lt;/o:p&gt;

        } &lt;o:p&gt;&lt;/o:p&gt;

        Field fieldContents = new Field("content", htmlContent, &lt;o:p&gt;&lt;/o:p&gt;

                Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES); &lt;o:p&gt;&lt;/o:p&gt;

        // db 中的每条纪录对应一个 doc ，每个字段对应一个 field &lt;o:p&gt;&lt;/o:p&gt;

        doc.add(fieldId); &lt;o:p&gt;&lt;/o:p&gt;

        doc.add(fieldContents); &lt;o:p&gt;&lt;/o:p&gt;

        return doc; &lt;o:p&gt;&lt;/o:p&gt;

    } &lt;o:p&gt;&lt;/o:p&gt;

     **//** **搜索文件， keyword 是你在页面上输入的查找关键字，这里查找的是 detail 字段** &lt;o:p&gt;&lt;/o:p&gt;

    public List searchFiles(String keyword){ &lt;o:p&gt;&lt;/o:p&gt;

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

         // hitsList 用来保存 db 的纪录，这些纪录可以通过查询结果取到 &lt;o:p&gt;&lt;/o:p&gt;

        List hitsList = new ArrayList(); &lt;o:p&gt;&lt;/o:p&gt;

        try { &lt;o:p&gt;&lt;/o:p&gt;

            Date start = new Date(); &lt;o:p&gt;&lt;/o:p&gt;

            IndexReader reader = IndexReader.open(index); &lt;o:p&gt;&lt;/o:p&gt;

            Searcher searcher = new IndexSearcher(reader); &lt;o:p&gt;&lt;/o:p&gt;

            Analyzer analyzer = new StandardAnalyzer(); &lt;o:p&gt;&lt;/o:p&gt;

            QueryParser parser = new QueryParser("content", analyzer); &lt;o:p&gt;&lt;/o:p&gt;

             // 解析查询关键字，比如输入的是以空格等分开的多个查询关键字，这里解析后，可以多条件查询 &lt;o:p&gt;&lt;/o:p&gt;

            Query query = parser.parse(keyword); &lt;o:p&gt;&lt;/o:p&gt;

             // hits 用来保存查询结果，这里的 hits 相当于 sql 中的 result &lt;o:p&gt;&lt;/o:p&gt;

            Hits hits = searcher.search(query); &lt;o:p&gt;&lt;/o:p&gt;

             for (int i = 0; i &lt; hits.length(); i++) { &lt;o:p&gt;&lt;/o:p&gt;

                Document doc = hits.doc(i); &lt;o:p&gt;&lt;/o:p&gt;

                 // 获得 article 表的主健 &lt;o:p&gt;&lt;/o:p&gt;

                String id = doc.get("uid"); &lt;o:p&gt;&lt;/o:p&gt;

                &lt;sp&gt;

&lt;o:p&gt;&lt;/o:p&gt;

Stored &lt;o:p&gt;&lt;/o:p&gt;

Indexed &lt;o:p&gt;&lt;/o:p&gt;

Tokenized &lt;o:p&gt;&lt;/o:p&gt;

Keyword &lt;o:p&gt;&lt;/o:p&gt;

Y &lt;o:p&gt;&lt;/o:p&gt;

Y &lt;o:p&gt;&lt;/o:p&gt;

N &lt;o:p&gt;&lt;/o:p&gt;

UnIndexed &lt;o:p&gt;&lt;/o:p&gt;

Y &lt;o:p&gt;&lt;/o:p&gt;

N &lt;o:p&gt;&lt;/o:p&gt;

N &lt;o:p&gt;&lt;/o:p&gt;

**UnStored** &lt;o:p&gt;&lt;/o:p&gt;

**N** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Text: String** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Text : Reader** &lt;o:p&gt;&lt;/o:p&gt;

**N** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;

**Y** &lt;o:p&gt;&lt;/o:p&gt;