索引服务(Indexing Service)实现文档搜索(C++代码完整实现)
Indexing Service是微软提供的为简单实现全文搜索引擎的一个服务,其开发的简便性简直是令人叹为观止,我也看到了很多人
将它作为网站的站内搜索引擎来做,使用ASP或VBScript就能实现.
Indexing Service使用的ADO的接口,可以表明该技术也是使用MS的数据库实现的,具体架构可以参见我以前写的搜索引擎架构的
文章.在建立索引服务的时候,你要通过计算机管理里面的索引服务的选项增加你要索引的目录,注意编录名称这栏填写的就是你将来使用
的Data Source的名称,编录目录就是你要索引的目录,然后启动该服务,在控制面板里也要记得启动indexing service服务,如果你要检查
是否配置成功了,可以使用查询编录的页面做简单查询工作.
在网上我也看到了很多人求助与如何使用C++来开发Index Service的帖子,希望是完整的源代码,本着毫不利己,专门利人的思想
,我这里把完整的C++代码贴上:
//All right revsered by yoki2009
//mailto:imj040144@tom.com
//微出版 www.epube.biz 相信梦想无界
#include "stdafx.h"
#include <string>
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#import "C://Program Files//Common Files//System//ado//msado15.dll" rename("EOF","adoEOF")
using namespace ADODB;
using namespace std;
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed/n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
CString keyword("keyword");
if (argc < 2)
{
cout<<"You should use keyword to search ,or use default keyword: ";
}else
{
keyword = argv[1];
}
CoInitialize(NULL);
try
{
const CString strConnection = "Provider=MSIDXS; Data Source=book";
if (S_OK != m_pConnection.CreateInstance(__uuidof(Connection)))
{
return FALSE;
}
if (S_OK != m_pConnection->Open(_bstr_t(strConnection),"","",adModeUnknown))
{
return FALSE;
}
m_pConnection->put_ConnectionTimeout(long(5));
m_pCommand->CommandTimeout = 15;
m_pCommand->ActiveConnection = m_pConnection;
m_pRecordset.CreateInstance(__uuidof(Recordset));
}catch(_com_error exception)
{
// cout<<exception.Description();
// return FALSE;
}
CString strSearch ;
strSearch.Format("SELECT DocTitle, Path, FileName, Characterization, Size, write FROM SCOPE() WHERE CONTAINS
('%s')",keyword.GetBuffer());
m_pRecordset = m_pConnection->Execute(_bstr_t(strSearch),NULL,adCmdText);
cout<<"DocTitle"<<" "<<"PATH"<<" "<<"FileName"<<" "<<"Characterization"<<endl;
while(!(m_pRecordset->adoEOF))
{
cout<<(CString)m_pRecordset->GetCollect("DocTitle").bstrVal<<" "<<(CString)m_pRecordset->GetCollect
("Path").bstrVal<<
" "<<(CString)m_pRecordset->GetCollect("FileName").bstrVal<<" "<<(CString)m_pRecordset-
>GetCollect("Characterization").bstrVal<<endl;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
if (m_pConnection->State)
{
m_pConnection->Close();
m_pConnection = NULL;
}
getchar();
return nRetCode;
}