索引服务器作用 windows索引服务
windows 系统调用.Windows索引服务.实列在Web访问索引服务
涉及技术
MSIDXS OLDB 驱动程序
取得扩展名对应的 mime 类型( 不知道文件的mime无法设置,Response.ContentType 无法实现下载功能 )
索引服务相关,知识及查询语句
编程背景(假设)
IndexServer 公司重199X 年起建立了一个文档共享服务器(ServerA) ,在此存放各种文档如word,pdf等
但由于现在文件存放过多导致,检索文件非常困难。
所以 IndexServer 公司需要在其共享文件服务器上快速检索、大家共享的文件并、提供Web,
检索下载界面。
编程需求
1. 实现对收藏文件,的全文检索(除了文本文件外还要有 office文件,pdf 等文件的检索功能)
2. 提供下载功能
分析
windows2000 开始ms提供了,索引服务、可以利用,不过默认是不支持 PDF 的。
网上有好多索引服务插件,可以下载可以解决 PDF 无法索引的问题、
如下地质 http://addins.china.msn.com/addins_category_desktop.aspx 就有,还可以用在MS桌面搜索中
步骤一
先学习如何配置索引服务
名称:就是“编录”的名称了(任意输入)
位置:存放索引文件的位置(选择即可)
本列中:
名称:IndexServer
位置:I:\IndexServer
本列中:
别名:Favorite
路径:I:\QuBinDocs\Favorite\
好了现在文档索引已经开始建立了,不过如果文件比较多的话会比较长时间
还有,windows 默认是对全盘开 索引服务的,会占用很大空间,我个人习惯都删除了自己在配置
自己需要索引的目录。
建立完索引后如果是自己本机使用是不需要编程的,每一个 “编录” 会提供一个查询页面在“编录”的节点下就有
步骤二
开始实现编程、使任何人都可以访问了,其实很简单的
界面效果(可以看出索引服务已经生效了)代码如下(下载的代码中有一些相关的资源在代码最后的地方)
Web config 配置,编录名 < appSettings > < add key ="FileIndexName" value ="IndexServer" /> </ appSettings > ASP.net 页面 < body > < form id ="form1" runat ="server" > < div > < table > < tr > < td style ="width: 100px" > 查询编录 </ td > < td style ="width: 100px" > < asp:TextBox ID ="txtIndex" runat ="server" ReadOnly ="true" Text ="<%$ AppSettings:FileIndexName %>" ></ asp:TextBox ></ td > < td style ="width: 100px" > </ td > </ tr > < tr > < td style ="width: 100px" > 查询词条 </ td > < td style ="width: 100px" > < asp:TextBox ID ="txtQueryText" runat ="server" ></ asp:TextBox ></ td > < td style ="width: 100px" > </ td > </ tr > < tr > < td style ="width: 100px" > </ td > < td style ="width: 100px" > </ td > < td style ="width: 100px" > </ td > </ tr > </ table > </ div > < asp:Button ID ="btnQuery" runat ="server" OnClick ="btnQuery_Click" Text ="Button" />< br /> < asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" Width ="100%" DataKeyNames ="Path" OnRowCommand ="GridView1_RowCommand" > < Columns > < asp:BoundField DataField ="RANK" HeaderText ="匹配度" /> < asp:BoundField DataField ="FILENAME" HeaderText ="文件名" /> < asp:BoundField DataField ="SIZE" HeaderText ="大小(byte)" /> < asp:BoundField DataField ="WRITE" HeaderText ="最后更新时间" /> < asp:BoundField DataField ="HITCOUNT" HeaderText ="匹配字数" /> < asp:ButtonField HeaderText ="" Text ="下载" CommandName ="download" /> </ Columns > </ asp:GridView > </ form > </ body > 程序查询: protected void btnQuery_Click( object sender, EventArgs e) { string text = txtQueryText.Text.Trim(); if (text.Length > 0 ) { using (OleDbConnection conn = new OleDbConnection( " PROVIDER=MSIDXS;DATA SOURCE= " + txtIndex.Text)) { conn.Open(); using (OleDbCommand cmd = conn.CreateCommand()) { cmd.CommandText = string .Format( @" select Rank ,HitCount ,Filename ,Size ,Write ,PATH from Scope() where CONTAINS ( '{0}') order by Rank desc,WRITE desc " , txtQueryText.Text.Trim()); // MSIDXS 不支持参数 // cmd.Parameters.Add("Pram1",OleDbType.VarWChar); // cmd.Parameters[0].Value = txtQueryText.Text; using (OleDbDataAdapter da = new OleDbDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } } }
下载文件代码:
protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e) { if (e.CommandName.ToLower() == " download " ) { GridView gv = sender as GridView; if (gv != null ) { string path = gv.DataKeys[ Convert.ToInt32( e.CommandArgument) ].Values[ 0 ].ToString(); downloadFile(path); } } } private void downloadFile( string FullPath) { string extension = Path.GetExtension(FullPath); string fileName = Path.GetFileName(FullPath); string ct = "" ; try { // 取得扩展名对应的 mime 类型 RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(extension); ct = regKey.GetValue( " Content Type " ) as string ; } catch (Exception ex) { Debug.Write(ex.Message); } if (ct == null ) // 没取到的就按照文件 ct = " application/octet-stream " ; this .Response.AddHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(fileName)); this .Response.ContentType = ct; this .Response.WriteFile(FullPath); this .Response.End(); }
最后把 I:\QuBinDocs\Favorite\ NTFS 权限设置为 ASPNET 用户可以访问即可.