在这里给大家分享一下基于Clucene 索引数据读取程序。 该程序是基于VC++ ,使用Clucene 0.92 版本
#include "stdafx.h"
#include "CLucene.h"
#include "CLucene/util/Reader.h"
#include "CLucene/util/Misc.h"
#include "CLucene/util/dirent.h"
#include "Clucene/store/FSDirectory.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <time.h>
#include <String.h>
#include <cstringt.h>
using namespace std;
using namespace lucene::index;
using namespace lucene::analysis;
using namespace lucene::util;
using namespace lucene::store;
using namespace lucene::document;
// Get wstr length
int wstrlen(const _TCHAR * wstr)
{
int l_idx = 0;
while (((char*)wstr)[l_idx] != 0) l_idx += 2;
return l_idx;
}
// TCHAR->char->string
char *wstrdup(const _TCHAR *wSrc)
{
int l_idx = 0;
int l_len = wstrlen(wSrc);
char *l_nstr = (char *)malloc(l_len);
if (l_nstr) {
do {
l_nstr[l_idx] = (char)wSrc[l_idx];
l_idx++;
} while ((char)wSrc[l_idx] != 0);
}
l_nstr[l_idx] = 0;
return l_nstr;
}
void getdata(IndexReader* indexReader,int32_t begin, int32_t end , int32_t max)
{
if (indexReader == NULL)
{
return ;
}
cout <<"\n";
TCHAR** fields= indexReader->getFieldNames();
if(end > max){
end = max ;
}
if(fields == NULL){
cout<< " fields is null " << endl;
return ;
}
for(int n = begin ; n < end ; n++)
{
Document* document =indexReader->document(n);
if(document != NULL )
{
for(int i=1; i < 30; i ++)
{
if(fields[i] == NULL)
{
continue;
}
const TCHAR* key = fields[i];
char* strkey = wstrdup(key);
if( document->get(key) != NULL){
const _TCHAR* value = document->get(key);
char* result = wstrdup(value);
cout<<"[" << result ;
}
else{
cout << "[null " ;
}
free(document);
}
}
cout << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char* fileName = "e:\test.log";
fstream* file1;
if(argc <3 )
{
cout << "please input index directory .. " << endl;
cout << "please input begin line number and end line number " << endl;
//printf("please input index directory .. ");
exit(0);
}
//TCHAR* dirName = argv[1];
int32_t start = _ttoi(argv[1]);
int32_t end = _ttoi(argv[2]);;
FSDirectory* dir = FSDirectory::getDirectory("e:/testdata/xsm" , false);
IndexReader* indexReader = IndexReader::open(dir, false);
int32_t max = indexReader->maxDoc();
//printf("max is %i " ,max);
getdata(indexReader,start ,end ,max);
exit(0);
}