Clucene 文件数据读取

本文分享了一个基于Clucene的索引数据读取程序示例,该程序使用VC++编写,适用于Clucene 0.92版本。程序通过读取指定目录下的索引文件,并展示如何遍历和打印索引中的文档数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在这里给大家分享一下基于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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值