欢迎访问 生活随笔!

ag凯发k8国际

当前位置: ag凯发k8国际 > 编程语言 > asp.net >内容正文

asp.net

lucene.net学习 -ag凯发k8国际

发布时间:2024/8/5 asp.net 104 豆豆
ag凯发k8国际 收集整理的这篇文章主要介绍了 lucene.net学习 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

          很早就知道lucene.net但一直都没时间认真实践一下.最近项目需要.所以考虑学习一下lucene.net引擎.终于做出了第一个demo感觉挺实用的.还不错.代码贴上.各位博友可参考学习一下.


        protected void btnaddindex_click(object sender, eventargs e)
        {
            writer = new indexwriter(server.mappath("index"), new lucene.net.analysis.simpleanalyzer(), false);
            indexdirectory(writer, new fileinfo(server.mappath("doc")));
            writer.close();
        }
        public void indexdirectory(indexwriter writer, fileinfo file)
        {
            if (system.io.directory.exists(file.fullname))
            {
                string[] files = system.io.directory.getfilesystementries(file.fullname);
                if (files != null)
                {
                    for (int i = 0; i < files.length; i )
                    {
                        indexdirectory(writer, new fileinfo(files[i])); 
                    }
                }
            }
            else if (file.extension == ".txt" || file.extension == ".htm" || file.extension == ".html")
            {
                indexfile(file, writer);
            }
        }

        private void indexfile(fileinfo file, indexwriter writer)
        {
            try
            {
                document doc = new document();
                txtlistindex.text = txtlistindex.text "\n文件路径:" file.fullname;
                doc.add(field.keyword("filename", path.getfilename(file.fullname).replace(file.extension,"")));
                //doc.add(field.text("contents", new streamreader(file.fullname, system.text.encoding.default)));
                doc.add(field.text("contents", file.fullname));
                writer.adddocument(doc);
            }
            catch (filenotfoundexception fnfe)
            {
                response.write(fnfe.message);
            }
        }

        protected void button1_click(object sender, eventargs e)
        {
            indexsearcher searcher = new indexsearcher(server.mappath("index"));

            query query = queryparser.parse("linux", "filename", new lucene.net.analysis.whitespaceanalyzer());

            hits hits = searcher.search(query);

            if (hits.length() > 0)
            {
                for (int num = 0; num <= hits.length() - 1; num )
                {
                    document docnew = hits.doc(num);
                    txtlistindex.text = txtlistindex.text "\n" docnew.get("contents");
                }
            }
        }

转载于:https://www.cnblogs.com/witer666/archive/2008/04/26/1172597.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是ag凯发k8国际为你收集整理的lucene.net学习的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得ag凯发k8国际网站内容还不错,欢迎将ag凯发k8国际推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图