当前位置: 首页>>代码示例>>C++>>正文


C++ IndexWriter类代码示例

本文整理汇总了C++中IndexWriter的典型用法代码示例。如果您正苦于以下问题:C++ IndexWriter类的具体用法?C++ IndexWriter怎么用?C++ IndexWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了IndexWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: cmd_add

int cmd_add( int_t argc, char** argv ){
	if ( argc < 2 )
		usage();

	bool dontRecurse = false;
	bool optimize = false;
	bool store = false;
	char_t* path = NULL;
	
	for ( int i=0;i< argc-2;i++ ){
		if ( strcmp(argv[i], "-dontrecurse")==0 )
			dontRecurse = true;
		else if ( strcmp(argv[i],"-optimize")==0 )
			optimize = true;
		else if ( strcmp(argv[i],"-store")==0 )
			store = true;
	}
	
	char_t* target = TO_CHAR_T(argv[argc-2]);
	path = TO_CHAR_T(argv[argc-1]);

	IndexWriter* writer = NULL;
	Directory* d = NULL;
	lucene::analysis::standard::StandardAnalyzer an;
	
	if ( IndexReader::indexExists(target) ){
		d = &FSDirectory::getDirectory( target,false );
		writer = new IndexWriter( *d, an, false);
	}else{
		d = &FSDirectory::getDirectory(target,true);
		writer = new IndexWriter( *d ,an, true);
	}
	
	struct Struct_Stat buf;
	if ( Cmd_Stat(path,&buf) == 0 ){
		if ( buf.st_mode & S_IFREG ){
			Document& doc = FileDocument( path, store );
			writer->addDocument( doc );
			delete &doc;
		}else if ( buf.st_mode & S_IFDIR ){
			indexDocs(writer, path, dontRecurse, store);
		}else{
			cerr << "File/directory is not valid.";
		}
	}else{
		printf( "File/directory does not exist." );
	}
		if ( optimize )
			writer->optimize();

		writer->close();
		delete[] target;
		delete[] path;

	return 1;
}
开发者ID:shiwenxiang,项目名称:clucene,代码行数:56,代码来源:add.cpp

示例2: testEndThreadException

void testEndThreadException(CuTest *tc) {

    const int MAX_DOCS=1500;
	RAMDirectory ram;
	WhitespaceAnalyzer an;
	IndexWriter* writer = _CLNEW IndexWriter(&ram, &an, true);

    // add some documents
    Document doc;
    for (int i = 0; i < MAX_DOCS; i++) {
        TCHAR * tmp = English::IntToEnglish(i);
        doc.add(* new Field(_T("content"), tmp, Field::STORE_YES | Field::INDEX_UNTOKENIZED));
        writer->addDocument(&doc);
        doc.clear();
        _CLDELETE_ARRAY( tmp );
    }

    CuAssertEquals(tc, MAX_DOCS, writer->docCount());
    
    writer->close();
	_CLLDELETE(writer);
    
    // this sequence is OK: delete searcher after search thread finish
    {
        IndexSearcher * searcher = _CLNEW IndexSearcher(&ram);
        _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher);
        SCOPED_LOCK_MUTEX(searchMutex);

        CONDITION_WAIT(searchMutex, searchCondition);
//        _LUCENE_SLEEP(9999); //make sure that deleteMutex is being waited on...
        CONDITION_NOTIFYALL(deleteCondition);

        _LUCENE_THREAD_JOIN(thread);

        searcher->close();
        _CLLDELETE(searcher);
    }

    // this produces memory exception: delete searcher after search finish but before thread finish
    {
        IndexSearcher * searcher = _CLNEW IndexSearcher(&ram);
        _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher);
        SCOPED_LOCK_MUTEX(searchMutex);

        CONDITION_WAIT(searchMutex, searchCondition);
        searcher->close();
        _CLLDELETE(searcher);
        CONDITION_NOTIFYALL(deleteCondition);

        _LUCENE_THREAD_JOIN(thread);
    }


    ram.close();
}
开发者ID:Blue-Rocket,项目名称:clucene,代码行数:55,代码来源:TestIndexSearcher.cpp

示例3: testIncludeLowerTrue

void testIncludeLowerTrue(CuTest* tc)
{
    WhitespaceAnalyzer a;
    RAMDirectory* index = _CLNEW RAMDirectory();
    IndexWriter* writer = _CLNEW IndexWriter(index,
        &a, true);

    Document doc;
    doc.add(*_CLNEW Field(_T("Category"), _T("a 1"), Field::STORE_YES | Field::INDEX_TOKENIZED));
    writer->addDocument(&doc); doc.clear();

    doc.add(*_CLNEW Field(_T("Category"), _T("a 2"), Field::STORE_YES | Field::INDEX_TOKENIZED));
    writer->addDocument(&doc); doc.clear();

    doc.add(*_CLNEW Field(_T("Category"), _T("a 3"), Field::STORE_YES | Field::INDEX_TOKENIZED));
    writer->addDocument(&doc); doc.clear();

    writer->close();
    _CLLDELETE(writer);

    IndexSearcher* s = _CLNEW IndexSearcher(index);
    Filter* f = _CLNEW RangeFilter(_T("Category"), _T("3"), _T("3"), true, true);

    Term* t = _CLNEW Term(_T("Category"), _T("a"));
    Query* q1 = _CLNEW TermQuery(t);
    _CLLDECDELETE(t);

    t = _CLNEW Term(_T("Category"), _T("3"));
    Query* q2 = _CLNEW TermQuery(t);
    _CLLDECDELETE(t);

    Hits* h = s->search(q1);
    assertTrue(h->length() == 3);
    _CLLDELETE(h);

    h = s->search(q2);
    assertTrue(h->length() == 1);
    _CLLDELETE(h);

    h = s->search(q1, f);
    assertTrue(h->length() == 1);
    _CLLDELETE(h);

    s->close();
    _CLLDELETE(s);
    _CLLDELETE(q1);
    _CLLDELETE(q2);
    _CLLDELETE(f);

    index->close();
    _CLLDECDELETE(index);
}
开发者ID:AlanForeverAi,项目名称:WizQTClient,代码行数:52,代码来源:TestRangeFilter.cpp

示例4: RAMDirectory

void TestSpansAdvanced::setUp()
{
    directory = _CLNEW RAMDirectory();
    Analyzer * analyzer = _CLNEW StandardAnalyzer();
    IndexWriter * writer = _CLNEW IndexWriter( directory, analyzer, true );

    addDocuments( writer );

    writer->close();
    _CLDELETE( writer );
    _CLDELETE( analyzer );

    searcher = _CLNEW IndexSearcher( directory );
}
开发者ID:Beirdo,项目名称:beirdobot,代码行数:14,代码来源:TestSpansAdvanced.cpp

示例5: testBooleanScorer

/// TestBooleanScorer.java, ported 5/9/2009
void testBooleanScorer(CuTest *tc) {
    const TCHAR* FIELD = _T("category");
    RAMDirectory directory;

    TCHAR* values[] = { _T("1"), _T("2"), _T("3"), _T("4"), NULL};

    try {
        WhitespaceAnalyzer a;
        IndexWriter* writer = _CLNEW IndexWriter(&directory, &a, true);
        for (size_t i = 0; values[i]!=NULL; i++) {
            Document* doc = _CLNEW Document();
            doc->add(*_CLNEW Field(FIELD, values[i], Field::STORE_YES | Field::INDEX_TOKENIZED));
            writer->addDocument(doc);
            _CLLDELETE(doc);
        }
        writer->close();
        _CLLDELETE(writer);

        BooleanQuery* booleanQuery1 = _CLNEW BooleanQuery();
        Term *t = _CLNEW Term(FIELD, _T("1"));
        booleanQuery1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
        _CLDECDELETE(t);
        t = _CLNEW Term(FIELD, _T("2"));
        booleanQuery1->add(_CLNEW TermQuery(t), true, BooleanClause::SHOULD);
        _CLDECDELETE(t);

        BooleanQuery* query = _CLNEW BooleanQuery();
        query->add(booleanQuery1, true, BooleanClause::MUST);
        t = _CLNEW Term(FIELD, _T("9"));
        query->add(_CLNEW TermQuery(t), true, BooleanClause::MUST_NOT);
        _CLDECDELETE(t);

        IndexSearcher *indexSearcher = _CLNEW IndexSearcher(&directory);
        Hits *hits = indexSearcher->search(query);
        CLUCENE_ASSERT(2 == hits->length()); // Number of matched documents
        _CLLDELETE(hits);
        _CLLDELETE(indexSearcher);

        _CLLDELETE(query);
    }
    catch (CLuceneError& e) {
        CuFail(tc, e.twhat());
    }
}
开发者ID:Beirdo,项目名称:beirdobot,代码行数:45,代码来源:TestBoolean.cpp

示例6: IndexText

  static Handle<Value> IndexText(const Arguments& args)
  {
    HandleScope scope;
    	IndexWriter* writer = NULL;
	lucene::analysis::WhitespaceAnalyzer an;

	if (IndexReader::indexExists(*String::Utf8Value(args[2])) ){
		if ( IndexReader::isLocked(*String::Utf8Value(args[2])) ){
			printf("Index was locked... unlocking it.\n");
			IndexReader::unlock(*String::Utf8Value(args[2]));
		}

		writer = _CLNEW IndexWriter( *String::Utf8Value(args[2]), &an, false);
	}else{
		writer = _CLNEW IndexWriter( *String::Utf8Value(args[2]) ,&an, true);
	}
	// We can tell the writer to flush at certain occasions
    //writer->setRAMBufferSizeMB(0.5);
    //writer->setMaxBufferedDocs(3);

    // To bypass a possible exception (we have no idea what we will be indexing...)
    writer->setMaxFieldLength(0x7FFFFFFFL); // LUCENE_INT32_MAX_SHOULDBE

    // Turn this off to make indexing faster; we'll turn it on later before optimizing
    writer->setUseCompoundFile(false);

	uint64_t str = Misc::currentTimeMillis();

	Document doc;

    doc.clear();

    TCHAR path[CL_MAX_DIR];
    STRCPY_AtoT(path,*String::Utf8Value(args[0]),CL_MAX_DIR);

    TCHAR contents[CL_MAX_DIR];
    STRCPY_AtoT(contents,*String::Utf8Value(args[1]),CL_MAX_DIR);

    (&doc)->add( *_CLNEW Field(_T("path"), path, Field::STORE_YES | Field::INDEX_UNTOKENIZED ) );
    (&doc)->add( *_CLNEW Field(_T("contents"), contents, Field::STORE_YES | Field::INDEX_TOKENIZED) );

    writer->addDocument( &doc );

    // Make the index use as little files as possible, and optimize it
    writer->setUseCompoundFile(true);
    writer->optimize();

    // Close and clean up
    writer->close();
	_CLLDELETE(writer);

	printf("Indexing took: %d ms.\n\n", (int32_t)(Misc::currentTimeMillis() - str));IndexWriter(*String::Utf8Value(args[2]), &an, false);

    return scope.Close(Undefined());

  }
开发者ID:tjgillies,项目名称:node-lucene,代码行数:56,代码来源:lucene_bindings.cpp

示例7: qDebug

void
FuzzyIndex::appendFields( const QString& table, const QMap< unsigned int, QString >& fields )
{
    try
    {
        qDebug() << "Appending to index:" << fields.count();
        bool create = !IndexReader::indexExists( TomahawkUtils::appDataDir().absoluteFilePath( "tomahawk.lucene" ).toStdString().c_str() );
        IndexWriter luceneWriter = IndexWriter( m_luceneDir, m_analyzer, create );
        Document doc;

        QMapIterator< unsigned int, QString > it( fields );
        while ( it.hasNext() )
        {
            it.next();
            unsigned int id = it.key();
            QString name = it.value();

            {
                Field* field = _CLNEW Field( table.toStdWString().c_str(), DatabaseImpl::sortname( name ).toStdWString().c_str(),
                                            Field::STORE_YES | Field::INDEX_UNTOKENIZED );
                doc.add( *field );
            }

            {
                Field* field = _CLNEW Field( _T( "id" ), QString::number( id ).toStdWString().c_str(),
                Field::STORE_YES | Field::INDEX_NO );
                doc.add( *field );
            }

            luceneWriter.addDocument( &doc );
            doc.clear();
        }

        luceneWriter.close();
    }
    catch( CLuceneError& error )
    {
        qDebug() << "Caught CLucene error:" << error.what();
        Q_ASSERT( false );
    }
}
开发者ID:LittleForker,项目名称:tomahawk,代码行数:41,代码来源:fuzzyindex.cpp

示例8: RAMDirectory

void TestBasics::setUp()
{
    directory = _CLNEW RAMDirectory();
    Analyzer * analyzer = _CLNEW SimpleAnalyzer();
    IndexWriter * writer = _CLNEW IndexWriter( directory, analyzer, true );
    TCHAR buffer[ 200 ];

    for( int32_t i = 0; i < 1000; i++ )
    {
        Document doc;
        English::IntToEnglish( i, buffer, 200 );
        doc.add( * _CLNEW Field( _T( "field" ), buffer, Field::STORE_YES | Field::INDEX_TOKENIZED ));
        writer->addDocument( &doc );
    }
    
    writer->close();
    _CLDELETE( writer );
    _CLDELETE( analyzer );

    searcher = _CLNEW IndexSearcher( directory );
}
开发者ID:AlanForeverAi,项目名称:WizQTClient,代码行数:21,代码来源:TestBasics.cpp

示例9: testRAMDirectorySize

void testRAMDirectorySize(CuTest * tc)  {

    MockRAMDirectory * ramDir = _CLNEW MockRAMDirectory(indexDir);
    WhitespaceAnalyzer analyzer;
    IndexWriter * writer = _CLNEW IndexWriter(ramDir, &analyzer, false);
    writer->optimize();

    CuAssertTrue(tc, ramDir->sizeInBytes == ramDir->getRecomputedSizeInBytes(), _T("RAMDir size"));

    _LUCENE_THREADID_TYPE* threads = _CL_NEWARRAY(_LUCENE_THREADID_TYPE, numThreads);
    ThreadData * tdata = _CL_NEWARRAY(ThreadData, numThreads);

    for (int i=0; i<numThreads; i++) {
        tdata[i].num = i;
        tdata[i].dir = ramDir;
        tdata[i].tc = tc;
        tdata[i].writer = writer;
        threads[i] = _LUCENE_THREAD_CREATE(&indexDocs, &tdata[i]);
    }

    for (int i=0; i<numThreads; i++) {
        _LUCENE_THREAD_JOIN(threads[i]);
    }

    _CLDELETE_ARRAY(threads);
    _CLDELETE_ARRAY(tdata);

    writer->optimize();
    CuAssertTrue(tc, ramDir->sizeInBytes == ramDir->getRecomputedSizeInBytes(), _T("RAMDir size"));

    CuAssertEquals(tc, docsToAdd + (numThreads * (docsPerThread-1)), writer->docCount(), _T("document count"));

    writer->close();
    _CLLDELETE(writer);

    ramDir->close();
    _CLLDELETE(ramDir);
}
开发者ID:Chen-Zhihui,项目名称:WizQTClient,代码行数:38,代码来源:TestRAMDirectory.cpp

示例10: testRAMDirectorySetUp

// setup the index
void testRAMDirectorySetUp (CuTest *tc) {

    if (strlen(cl_tempDir) + 13 > CL_MAX_PATH)
        CuFail(tc, _T("Not enough space in indexDir buffer"));

    sprintf(indexDir, "%s/RAMDirIndex", cl_tempDir);
    WhitespaceAnalyzer analyzer;
    IndexWriter * writer  = new IndexWriter(indexDir, &analyzer, true);

    // add some documents
    TCHAR * text;
    for (int i = 0; i < docsToAdd; i++) {
        Document doc;
        text = English::IntToEnglish(i);
        doc.add(* new Field(_T("content"), text, Field::STORE_YES | Field::INDEX_UNTOKENIZED));
        writer->addDocument(&doc);
        _CLDELETE_ARRAY(text);
    }

    CuAssertEquals(tc, docsToAdd, writer->docCount(), _T("document count"));
    writer->close();
    _CLDELETE( writer );
}
开发者ID:Chen-Zhihui,项目名称:WizQTClient,代码行数:24,代码来源:TestRAMDirectory.cpp

示例11: setUp

    void setUp()
    {
        TCHAR       tbuffer[16];
        const TCHAR*      data[] = 
        {   
            _T( "A 1 2 3 4 5 6" ),
            _T( "Z       4 5 6" ),
            NULL,
            _T( "B   2   4 5 6" ),
            _T( "Y     3   5 6" ),
            NULL,
            _T( "C     3     6" ),
            _T( "X       4 5 6" )
        };
        
        m_pSmall = _CLNEW RAMDirectory();
        Analyzer * pAnalyzer = _CLNEW WhitespaceAnalyzer();
        IndexWriter * pWriter = _CLNEW IndexWriter( m_pSmall, pAnalyzer, true );
        
        for( size_t i = 0; i < sizeof( data ) / sizeof( data[0] ); i++ )
        {
            _itot( i, tbuffer, 10 ); 
            Document doc;
            doc.add( * _CLNEW Field( _T( "id" ), tbuffer, Field::STORE_YES | Field::INDEX_UNTOKENIZED ));
            doc.add( * _CLNEW Field( _T( "all" ), _T( "all" ), Field::STORE_YES | Field::INDEX_UNTOKENIZED ));
            if( data[ i ] )
                doc.add( * _CLNEW Field( _T( "data" ), data[ i ], Field::STORE_YES | Field::INDEX_TOKENIZED ));

            pWriter->addDocument( &doc );
        }
        
        pWriter->optimize();
        pWriter->close();
        _CLDELETE( pWriter );
        _CLDELETE( pAnalyzer );
    }
开发者ID:AlanForeverAi,项目名称:WizQTClient,代码行数:36,代码来源:TestConstantScoreRangeQuery.cpp

示例12: EIO_Index

    static int EIO_Index(eio_req* req) {
        index_baton_t* baton = static_cast<index_baton_t*>(req->data);

        lucene::analysis::standard::StandardAnalyzer an;
        IndexWriter* writer = 0;
        bool writerOpen = false;
        
        try {
          bool needsCreation = true;
          if (IndexReader::indexExists(*(*baton->index))) {
              if (IndexReader::isLocked(*(*baton->index))) {
                  IndexReader::unlock(*(*baton->index));
              }
              needsCreation = false;
          }
          writer = new IndexWriter(*(*baton->index), &an, needsCreation);
          writerOpen = true;
        
          // To bypass a possible exception (we have no idea what we will be indexing...)
          writer->setMaxFieldLength(0x7FFFFFFFL); // LUCENE_INT32_MAX_SHOULDBE
          // Turn this off to make indexing faster; we'll turn it on later before optimizing
          writer->setUseCompoundFile(false);
          uint64_t start = Misc::currentTimeMillis();

          writer->addDocument(baton->doc->document());
          
          // Make the index use as little files as possible, and optimize it
          writer->setUseCompoundFile(true);
          writer->optimize();

          baton->indexTime = (Misc::currentTimeMillis() - start);
        } catch (CLuceneError& E) {
          baton->error.assign(E.what());
        } catch(...) {
          baton->error = "Got an unknown exception";
        }
        
        // Close and clean up
        if (writerOpen == true) {
           writer->close();
        }       
        delete writer;
        //(*(*baton->index), &an, false);

        return 0;
    }
开发者ID:temas,项目名称:node-lucene,代码行数:46,代码来源:clucene_bindings.cpp

示例13: IndexFiles

  static Handle<Value> IndexFiles(const Arguments& args)
  {
    HandleScope scope;
	IndexWriter* writer = NULL;
	lucene::analysis::WhitespaceAnalyzer an;
	
	if (IndexReader::indexExists(*String::Utf8Value(args[1])) ){
		if ( IndexReader::isLocked(*String::Utf8Value(args[1])) ){
			printf("Index was locked... unlocking it.\n");
			IndexReader::unlock(*String::Utf8Value(args[1]));
		}

		writer = _CLNEW IndexWriter( *String::Utf8Value(args[1]), &an, false);
	}else{
		writer = _CLNEW IndexWriter( *String::Utf8Value(args[1]) ,&an, true);
	}

    //writer->setInfoStream(&std::cout);

    // We can tell the writer to flush at certain occasions
    //writer->setRAMBufferSizeMB(0.5);
    //writer->setMaxBufferedDocs(3);

    // To bypass a possible exception (we have no idea what we will be indexing...)
    writer->setMaxFieldLength(0x7FFFFFFFL); // LUCENE_INT32_MAX_SHOULDBE
    
    // Turn this off to make indexing faster; we'll turn it on later before optimizing
    writer->setUseCompoundFile(false);

	uint64_t str = Misc::currentTimeMillis();

	indexDocs(writer, *String::Utf8Value(args[0]));
	
    // Make the index use as little files as possible, and optimize it
    writer->setUseCompoundFile(true);
    writer->optimize();
	
    // Close and clean up
    writer->close();
	_CLLDELETE(writer);

	printf("Indexing took: %d ms.\n\n", (int32_t)(Misc::currentTimeMillis() - str));IndexWriter(*String::Utf8Value(args[1]), &an, false);
    
    //Lucene* lucene = ObjectWrap::Unwrap<Lucene>(args.This());

    return scope.Close(String::New("foo"));
  }
开发者ID:tjgillies,项目名称:node-lucene,代码行数:47,代码来源:lucene_bindings.cpp

示例14: xesamquery


//.........这里部分代码省略.........
        printf ("No results for search\n");
    }

    while (matches.size() > 0) {
        for (vector<IndexedDocument>::iterator it = matches.begin();
            it != matches.end(); ++it) {
                printf ("\"%s\" matched\n", it->uri.c_str());
                printIndexedDocument(*it);
            }

        results += (int)matches.size();

        if (matches.size() == batchsize) {
            // maybe there're other results
            matches = reader->query(query, results + 1, 10);
        } else {
            matches.clear(); // force while() exit
        }
    }

    if (results != 0)
        printf ("Query returned %i results\n", results);

    IndexPluginLoader::deleteIndexManager(manager);
    return 0;
}
*/
int
deindex(int argc, char** argv) {
    // parse arguments
    parseArguments(argc, argv);
    string backend = options['t'];
    string indexdir = options['d'];

    // check arguments: indexdir
    if (indexdir.length() == 0) {
        pe("Provide the directory with the index.\n");
        return usage(argc, argv);
    }

    // check arguments: dirs
    if (arguments.size() == 0) {
        pe("'%s' '%s'\n", backend.c_str(), indexdir.c_str());
        pe("Provide directories/files to deindex.\n");
        return usage(argc, argv);
    }
    
    AnalyzerConfiguration config;
    
    // create an index manager
    IndexManager* manager = getIndexManager(backend, indexdir);
    if (manager == 0) {
        return usage(argc, argv);
    }
    
    // retrieve all indexed files at level 0 (archives contents aren't returned)
    map<string, time_t> indexedFiles;
/* TODO: make this code not use indexReader->files(0)

    IndexReader* indexReader = manager->indexReader();
    indexedFiles = indexReader->files(0);
*/
    vector<string> toDelete;
    
    for (vector<string>::iterator iter = arguments.begin();
         iter != arguments.end(); ++iter)
    {
        // find all indexed files whose path starts with *iter
        FindIndexedFiles match (*iter);
        map<string, time_t>::iterator itBeg, itEnd, itMatch;
        itBeg = indexedFiles.begin();
        itEnd = indexedFiles.end();
        
        itMatch = find_if (itBeg, itEnd, match);
        while (itMatch != itEnd)
        {
            toDelete.push_back (itMatch->first);
            itBeg = ++itMatch;
            itMatch = find_if (itBeg, itEnd, match);
        }
    }
    
    if (toDelete.empty())
        printf ("no file will be deindexed\n");
    else
    {
        for (vector<string>::iterator iter = toDelete.begin();
             iter != toDelete.end(); ++iter)
        {
            printf ("%s will be deindex\n", iter->c_str());
        }
        IndexWriter* writer = manager->indexWriter();
        writer->deleteEntries(toDelete);
        writer->commit();
        writer->optimize();
    }
    
    IndexPluginLoader::deleteIndexManager(manager);
    return 0;
}
开发者ID:KDE,项目名称:strigidaemon,代码行数:101,代码来源:strigicmd.cpp

示例15: myDebug

int ZefaniaLex::buildIndex()
{
    DEBUG_FUNC_NAME;

    myDebug() << "building index!!!";
    QFile file(m_modulePath);
    Document indexdoc;
    const QString index = indexPath();
    QDir dir("/");
    dir.mkpath(index);


    m_refText.setSettings(m_settings);
    IndexWriter* writer = nullptr;
    const TCHAR* stop_words[] = { nullptr };
    standard::StandardAnalyzer an(stop_words);

    //open the xml file
    if(!file.open(QFile::ReadOnly | QFile::Text))
        return 1;
    m_xml = new QXmlStreamReader(&file);

    try {

        if(IndexReader::indexExists(index.toStdString().c_str())) {
            if(IndexReader::isLocked(index.toStdString().c_str())) {
                myDebug() << "Index was locked... unlocking it.";
                IndexReader::unlock(index.toStdString().c_str());
            }
        }
        writer = new IndexWriter(index.toStdString().c_str() , &an, true);

        writer->setMaxFieldLength(0x7FFFFFFFL);
        writer->setUseCompoundFile(false);

        TCHAR *buffer = SearchTools::createBuffer();

        if(m_xml->readNextStartElement()) {
            if(cmp(m_xml->name(), "dictionary")) {
                while(m_xml->readNextStartElement()) {
                    if(cmp(m_xml->name(), "item")) {
                        QString content;
                        const QString key = m_xml->attributes().value("id").toString();
                        bool hasTitle = false;
                        while(true) {
                            m_xml->readNext();

                            if(m_xml->tokenType() == QXmlStreamReader::EndElement && (cmp(m_xml->name(), QLatin1String("item"))))
                                break;

                            if(m_xml->tokenType() == QXmlStreamReader::Characters) {
                                content += m_xml->text().toString();
                            } else if(cmp(m_xml->name(), "title")) {
                                const QString title = parseTitle();
                                content += "<h3 class='title'>" + key;
                                if(!title.isEmpty()) {
                                    content.append(" - " +  title);
                                }
                                content.append("</h3>");
                                hasTitle = true;
                            } else if(cmp(m_xml->name(), "transliteration")) {
                                const QString trans = parseTrans();
                                if(!trans.isEmpty()) {
                                    content += "<span class='transliteration'>" + trans + "</span>" ;
                                }
                            }  else if(cmp(m_xml->name(), "pronunciation")) {
                                const QString pr = parsePron();
                                if(!pr.isEmpty()) {
                                    content += "<span class='pronunciation'>" + pr + "</span>";
                                }
                            } else if(cmp(m_xml->name(), "description")) {
                                content += "<span class='description'>" + parseDesc() + "</span>";
                            } else {
                                content += m_xml->readElementText(QXmlStreamReader::IncludeChildElements);
                            }
                        }
                        if(!hasTitle) {
                            content.prepend("<h3 class='title'>" + key + "</h3>");
                        }
                        indexdoc.clear();
                        indexdoc.add(*_CLNEW Field(_T("key"), SearchTools::toTCHAR(key, buffer), Field::STORE_YES |  Field::INDEX_TOKENIZED));
                        indexdoc.add(*_CLNEW Field(_T("content"), SearchTools::toTCHAR(content, buffer), Field::STORE_YES |  Field::INDEX_TOKENIZED));
                        writer->addDocument(&indexdoc);

                    } else {
                        m_xml->skipCurrentElement();
                    }
                }
            } else {
                myWarning() << "not a file";
            }
        }

        writer->setUseCompoundFile(true);
        writer->optimize();

        writer->close();
        delete writer;
    }
    catch(...) {
//.........这里部分代码省略.........
开发者ID:metaxy,项目名称:openBibleViewer,代码行数:101,代码来源:zefania-lex.cpp


注:本文中的IndexWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。