使用Lucene进行全文检索(二)---得到有效的内容

scud(飞云小侠) http://www.jscud.com 转载请注明来源/作者

关键字:lucene,html parser,全文检索,IndexReader,Document,Field,IndexWriter,Term,HTMLPAGE


  在使用lucene对相关内容进行索引时,会遇到各种格式的内容,例如Html,PDF,Word等等,那么我们如何从这么文档中得到我们需要的内容哪?例如Html的内容,一般我们不需要对Html标签建立索引,因为那不是我们需要搜索的内容.这个时候,我们就需要从Html内容中解析出我们所需要的内容.对于PDF,Word文档,也是类似的要求.
 
  总之,我们只需要从内容中提取出我们需要的文本来建立索引,这样用户就能搜索到需要的内容,然后访问对应的资源即可.

  Lucene本身带的例子中有一个解析Html的代码,不过不是纯JAVA的,所以在网上我又找到了另外一个Html解析器,网址如下:http://htmlparser.sourceforge.net.
 
  对PDF解析的相关项目有很多,例如PDFBox.在PDFBox里面提出pdf的文本内容只需要一句话即可:  
  
   
Document doc = LucenePDFDocument.getDocument( file );  

  
  当然如果需要更高级的设置,就要使用PDFBox中PDFTextStripper等类来实现更高级的操作了.
 
 
  对Word文档解析的相关有POI,网址是 http://jakarta.apache.org/poi/.
 
  HtmlParser本身提供的功能很强大,我们下面主要来关注我们需要的功能.首先给出几个函数如下:
 

 /**
 * 解析一个Html页面,返回一个Html页面类.
 *
 * @param resource 文件路径或者网址
 */
    public static SearchHtmlPage parseHtmlPage(String resource)
    {
        String title = "";
        String body = "";
        try
        {
            Parser myParser = new Parser(resource);

            //设置编码:根据实际情况修改
            myParser.setEncoding("GBK");

            HtmlPage visitor = new HtmlPage(myParser);

            myParser.visitAllNodesWith(visitor);

            title = visitor.getTitle();

            body = combineNodeText(visitor.getBody().toNodeArray());
        }
        catch (ParserException e)
        {
            LogMan.error("Parse Html Page " + resource + " Error!");
        }

        SearchHtmlPage result = new SearchHtmlPage(title, body);

        return result;
    }

    /**
     * 解析Html内容,得到普通文本和链接的内容.
     *
     * @param content 要解析的内容
     * @return 返回解析后的内容
     */
    public static String parseHtmlContent(String content)
    {
        Parser myParser;
        NodeList nodeList = null;

        myParser = Parser.createParser(content, "GBK");

        NodeFilter textFilter = new NodeClassFilter(TextNode.class);
        NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);

        //暂时不处理 meta
        //NodeFilter metaFilter = new NodeClassFilter(MetaTag.class);

        OrFilter lastFilter = new OrFilter();
        lastFilter.setPredicates(new NodeFilter[] { textFilter, linkFilter });

        try
        {
            nodeList = myParser.parse(lastFilter);
        }
        catch (ParserException e)
        {
            LogMan.warn("Parse Content Error", e);
        }

        //中场退出了
        if (null == nodeList)
        {
            return "";
        }

        Node[] nodes = nodeList.toNodeArray();

        String result = combineNodeText(nodes);
        return result;
    }

 //合并节点的有效内容
    private static String combineNodeText(Node[] nodes)
    {
        StringBuffer result = new StringBuffer();

        for (int i = 0; i < nodes.length; i++)
        {
            Node anode = (Node) nodes[i];

            String line = "";
            if (anode instanceof TextNode)
            {
                TextNode textnode = (TextNode) anode;
                //line = textnode.toPlainTextString().trim();
                line = textnode.getText();
            }
            else if (anode instanceof LinkTag)
            {
                LinkTag linknode = (LinkTag) anode;

                line = linknode.getLink();
                //过滤jsp标签
                line = StringFunc.replace(line, "<%.*%>", "");
            }

            if (StringFunc.isTrimEmpty(line)) continue;

            result.append(" ").append(line);
        }

        return result.toString();
    }


  
  其中SearchHtmlPage类是表示一个Html页面的模型,包含标题和内容,代码如下:
  
 package com.jscud.www.support.search;
 
 /**
  * 搜索时解析Html后返回的页面模型.
  *
  * @author scud(飞云小侠) http://www.jscud.com
  * 
  */
 public class SearchHtmlPage
 {
     /**标题*/
     private String title;
 
     /**内容*/
     private String body;
    
     public SearchHtmlPage(String title, String body)
     {
         this.title = title;
         this.body = body;
     }
    
     public String getBody()
     {
         return body;
     }
 
     public void setBody(String body)
     {
         this.body = body;
     }
 
     public String getTitle()
     {
         return title;
     }
 
     public void setTitle(String title)
     {
         this.title = title;
     }
 }
 


 
  当然,使用HtmlParser解析Html资源还有很多其他的方法,可以设置很多的条件来满足用户的解析要求,用户可以阅读其他的文章或者HtmlParser的文档来了解,在此不多介绍.
 
  下一节讲解如何进行搜索.

 

posted on 2005-08-12 17:33 Scud(飞云小侠) 阅读(799) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
 
<2005年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

公告

文章发布许可
创造共用协议:署名,非商业,保持一致

我的邮件
cnscud # gmail


常用链接

留言簿(15)

随笔分类(113)

随笔档案(103)

相册

友情链接

技术网站

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜