Indexing Service simple sample

来源:互联网 发布:musixmatch for mac 编辑:程序博客网 时间:2024/05/20 04:15
Indexing Service 简单例子

1.要使用Windows的全文搜索服务,首先要确保在“添加删除程序/添加删除Windows组件/”中安装了“索引服务”:

然后就可以使用Indexing Service了,包括进行编程。
安装了索引服务后就可以到“计算机管理/服务和应用程序/”中找到索引服务,并对之进行设置和管理:

默认情况下会有两个编目(Catalog),System和Web,分别用于建立和维护本地文件和本地网站的索引。每个编目包含了多个目录(Scope),并有一个属性(Property)集合。
 
2.关于Windows Indexing Service的相关文档、编程参考和代码片断都可以在msdn中找到,下面是链接:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/indexserv.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/indexsrv/html/ixrefobj_3skz.asp
 
3.由于IndexingService相关API都是通过COM组件的形式提供的,因此如果要在程序中调用相关API,就必须首先添加对如下Type Library的引用:

API Reference Library Admin Helper Indexing Service Administration Type Library 1.0 CIODMLib Query Helper ixsso Control Library Cisso ADO Micorsoft ActiveX Data Objects (ADO) version Library, where version is one of the available versions. ADODB
下面是对Indexing Service进行设置和管理的编程对象:
Object nameDescriptionAdminIndexServer Manages Indexing Service. Allows access to all the functionality provided by the Microsoft Management Console (MMC) snap-in.CatAdm Manages the collection of scopes for a catalog.ScopeAdm Manages an individual scope.
下面是通过IndexingService进行全文搜索的编程对象:

Object nameDescriptionQuery Manages the query definition and result-set creation and navigation.Utility Helpful utilities for managing the queries and result sets.
msdn的文档中提供了一些比较简单的示例代码,分别由VBScript, JScript, Visual Basic, Visual C++, 和Visual J++的代码,好像没有.NET的代码。
需要添加com引用
Interop.CIODMLib.dll
Interop.Cisso.dll
可用object browser查看里面的类
try
            {
                Cisso.CissoQueryClass query = new Cisso.CissoQueryClass();
               
                query.Columns = "Filename,Path,Size";
                query.Query = tbKeywords.Text;
                //query.CiScope = "";
                query.Catalog = "Web";// "testCatalog";
                OleDbDataAdapter sda = new OleDbDataAdapter();
                DataTable dtResult = new DataTable();
                sda.Fill(dtResult, query.CreateRecordset("nonsequential"));
                //dtResult.Columns[
                gvResult.DataSource = dtResult;
                gvResult.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        private DataTable GetIndexingServiceSearchResult()
        {
            Cisso.CissoQueryClass q = new Cisso.CissoQueryClass();
            Cisso.CissoUtilClass util = new Cisso.CissoUtilClass();
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            q.Query = tbKeywords.Text;
            q.Catalog = FullTextSearchCatalog;
            q.SortBy = "Filename";
            q.Columns = "Filename,Path,Size";
            util.AddScopeToQuery(q, FullTextSearchScope, "deep");
            da.Fill(dt, q.CreateRecordset("nonsequential"));
            return dt;
        }
原创粉丝点击