`
ljm3256748
  • 浏览: 43840 次
  • 性别: 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>


分享到:
评论

相关推荐

    开发自己的搜索引擎《lucene2.0+heritrix》一书对应的源码资料

    而`myReserch-可用的网络搜索引擎`可能包含一个完整的搜索引擎实现,展示如何结合Heritrix抓取网页并用Lucene处理抓取到的数据。 此外,`testDWR`可能是Direct Web Remoting (DWR)的相关代码。DWR是一种JavaScript...

    lucene 2.0 api以及lucene 3.0 api

    1. **索引构建**: Lucene 2.0 提供了 `IndexWriter` 类,用于创建和更新索引。开发者可以使用 `Document` 类来封装待索引的数据,然后通过 `addDocument()` 方法添加到索引中。 2. **查询构造**: 通过 `QueryParser...

    lucene2.0与其分词工具包

    它以其强大的搜索功能和高效的性能在Java开发领域中备受推崇。作为一款开源项目,Lucene为开发者提供了丰富的API,使得构建搜索引擎变得简单而高效。然而,随着时间的推移,Lucene已经发展到更高的版本,如现在的...

    Lucene2.0+Heritrix(源代码)

    首先,我们要了解Lucene2.0和Heritrix的基本概念和功能。 Lucene2.0是Apache软件基金会的一个项目,它是一个高性能、全文检索库,提供了一个简单的API,允许开发者将全文搜索功能集成到他们的应用中。Lucene的核心...

    lucene2.0+Heritrix配套源码

    Lucene是一个高性能、可扩展的信息检索库,它实现了全文检索的基本算法,包括分词、索引和查询处理。在Lucene中,主要涉及以下知识点: 1. **分词器(Tokenizer)**:Lucene的核心之一是分词,将输入的文本分割成一...

    lucene-2.0

    《深入理解Lucene 2.0:开源全文检索框架解析》 Lucene 2.0是一款强大的开源全文检索库,由Apache软件基金会开发并维护,是Java编程语言中的一个核心工具,广泛应用于搜索引擎的构建和其他信息检索场景。该版本发布...

    Lucene.2.0.API

    8. **高级功能**:包括倒排索引的优化(`MergePolicy`)、多字段查询、近实时搜索(NRT,Near Real Time)等,这些都极大地增强了Lucene的功能和性能。 Lucene 2.0虽然较旧,但其核心概念和机制在后续版本中仍被...

    Lucene 2.0+Heriterx书源代码-ch7lib

    《深入理解Lucene 2.0与Heritrix:源码剖析》是一本关于搜索引擎开发的经典著作,书中详细讲解了如何使用Lucene 2.0和Heritrix这两个开源工具进行信息检索。在这个名为"Lucene 2.0+Heriterx书源代码-ch7lib"的压缩包...

    Lucene.Net 2.0 源码+文档

    《深入理解Lucene.Net 2.0:源码与文档解析》 Lucene.Net是一个开源的全文搜索引擎库,它是Apache Lucene项目在.NET平台上的实现,由DotLucene发展而来,广泛应用于各种信息检索和文本挖掘场景。这个资料包包含了...

    Lucene-2.0学习文档

    《深入理解Lucene-2.0:从源码到应用》 Lucene是一个开源的全文检索库,由Apache软件基金会开发并维护。它为Java开发者提供了强大的文本搜索功能,广泛应用于各种信息检索系统中。本篇文章将围绕"Lucene-2.0学习...

    《开发自己的搜索引擎-Lucene 2.0 Heritrix》cd(全)(下载地址)

    - **高效性**:通过优化的数据结构和技术实现,Lucene 2.0能够快速地索引和检索大量数据。 - **可扩展性**:支持索引各种不同类型的数据,如文本、数字等,并且可以轻松地扩展到处理更大规模的数据集。 - **高度可...

    Lucene.net 2.0 API + DLL 下载

    总的来说,Lucene.NET 2.0 API是.NET开发者实现全文搜索引擎的关键工具,无论是在网站、应用还是后台系统中,都能大大提高文本数据的检索效率和用户体验。通过深入学习和实践,开发者可以充分利用其强大的功能,打造...

    lucene2.0+heritrix 随书光盘

    光盘中的各个文件夹很可能包含了丰富的代码示例和实践项目,例如如何集成Lucene和Heritrix来创建一个完整的搜索引擎系统。这些示例可以帮助读者通过动手实践,巩固理论知识,提升实际开发能力。 总的来说,这个随...

    Lucene2.0+Heritrix搜索引擎 随书光盘

    结合Lucene和Heritrix,开发者可以构建一个完整的搜索引擎系统,首先使用Heritrix爬取网络内容,然后用Lucene建立索引,最终实现快速、精准的搜索功能。随书光盘中的资源可能包括源代码、示例项目、教程文档等,帮助...

    Lucene.net 2.0源码

    《深入剖析Lucene.NET 2.0:打造高效全文搜索引擎》 Lucene.NET 2.0 是一个基于 Apache Lucene 的开源全文检索库,专为 .NET Framework 设计。它提供了一种强大而灵活的方式来在应用程序中实现全文搜索功能,支持...

    【完整光盘24.5M】开发自己的搜索引擎-Lucene 2.0+Heritrix.zip

    3. **索引构建**:掌握如何使用Lucene API创建和更新索引,包括添加、删除和更新文档。 4. **查询处理**:学习Lucene的查询语言,如何构造布尔、短语、近似和范围查询,以及如何优化查询性能。 5. **搜索结果排序**...

    Lucene.Net2.0(C#)

    这个开源项目是Apache Lucene的.NET版本,旨在为.NET开发者提供与Java版本相同的功能和性能,使.NET应用程序能够快速实现高效的全文检索。 一、Lucene.Net概述 Lucene.Net的核心功能包括文档索引、查询解析、评分和...

    Lucene2.0+Nutch0.8 API帮助文档(CHM格式)

    《Lucene2.0+Nutch0.8 API帮助文档》是一个综合性的技术资源,它包含了对Lucene 2.0和Nutch 0.8这两个关键的开源搜索引擎库的详细接口和功能说明。这两个组件在信息检索、全文搜索以及网络爬虫领域有着广泛的应用。 ...

    【大搜集:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    基于Java的全文索引引擎.doc lucene测试代码.txt lucene为数据库搜索建立增量索引.txt lucene数据库索引.txt 新闻系统全文检索的思绪.txt ... 关于lucene2.0的创建、检索和删除功能的完整实现.doc weblucene.txt

Global site tag (gtag.js) - Google Analytics