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

转载:

http://blog.csdn.net/xiaodaoxiaodao/archive/2006/09/10/1203959.aspx<o:p></o:p>

<o:p></o:p>

关于lucene2.0的创建、检索和删除功能的完整实现<o:p></o:p>

<o:p> </o:p>

最近要做一个站内的全文检索功能,主要是针对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-2.0.0src\\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字段创建索引,detailDB中是clob字段,内容为html文本<o:p></o:p>

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

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

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

        // HTMLParser这个类可以在lucenedemo中找到<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-2.0.0src\\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>

                // 根据主健去db中取纪录,返回到hitsList<o:p></o:p>

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

                    Article article = articleManager.getArticle(id);<o:p></o:p>

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

                    article = null;<o:p></o:p>

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

                       // 如果没有找到该纪录,表示该纪录已经不存在,不必添加到hitsList<o:p></o:p>

                if(article!=null)  hitsList.add(article);<o:p></o:p>

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

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

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

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

            System.out.println("search files: " + (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>

        } catch (ParseException 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>

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

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

    // 删除索引<o:p></o:p>

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

        String index = "E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index";<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>

            int numFiles = reader.numDocs();<o:p></o:p>

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

                // 这里的删除只是给文档做一个删除标记,你可以看到执行deleteDocument后会产生一个del后缀的文件,<o:p></o:p>

                // 用来记录这些标记过的文件<o:p></o:p>

                reader.deleteDocument(i);<o:p></o:p>

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

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

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

            System.out.println("delete 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>

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

    // 恢复已删除的索引<o:p></o:p>

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

        String index = "E:\\lucene_Learning\\lucene-2.0.0src\\src\\demo\\index";<o:p></o:p>

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

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

            reader.undeleteAll();<o:p></o:p>

            reader.close();<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>

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

<o:p> </o:p>

Field就像我们学过的数据库中的字段,简单的说,就是一个名值对。这个域有三种属性,分别是<o:p></o:p>

isStored - 是否被存储
isIndexed -
是否被索引
isTokenized -
是否分词<o:p></o:p>

这些属性的组合又构成了四种不同类型的Field,而且各有用途<o:p></o:p>

<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>

<o:p> </o:p>

关于Field2.0.0版本和1.4.3版本方法相比改动比较大,具体见下表<o:p></o:p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值