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


    // 创建索引


    public void indexFiles() {


         // 创建索引文件存放路径


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


 


        try {


            Date start = new Date();


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


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


            StandardAnalyzer standardAnalyzer = new StandardAnalyzer();


             // 参数 true 用于确定是否覆盖原有索引的


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


            indexWriter.setMergeFactor(100);


            indexWriter.setMaxBufferedDocs(100);


             // 只索引这个 Field 的前 5000 个字,默认为 10000


              indexWriter.setMaxFieldLength(5000);


             // 从数据库取出所有纪录


            List articleList = articleManager.getArticles(null);


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


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


                 // Document 方法是创建索引的具体代码


                Document doc = Document(article);


                indexWriter.addDocument(doc);


            }


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


            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 表的主健创建索引,关于 Field 的几个参数下面有详细解释


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


         // detail 字段创建索引, detail DB 中是 clob 字段,内容为 html 文本


        String contentHtml = article.getDetail();


        Reader read = new StringReader(contentHtml);


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


        // HTMLParser 这个类可以在 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 中的每条纪录对应一个 doc ,每个字段对应一个 field


        doc.add(fieldId);


        doc.add(fieldContents);


        return doc;


    }


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


    public List searchFiles(String keyword){


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


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


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


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


            Query query = parser.parse(keyword);


             // hits 用来保存查询结果,这里的 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");


               




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


评论
发表评论

您还没有登录,请登录后发表评论

ljm3256748
搜索本博客
最近加入圈子
存档
最新评论
评论排行榜