mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-13 17:43:59 +02:00
* Catch CLuceneError exceptions.
This commit is contained in:
@@ -45,7 +45,16 @@ void
|
|||||||
FuzzyIndex::beginIndexing()
|
FuzzyIndex::beginIndexing()
|
||||||
{
|
{
|
||||||
m_mutex.lock();
|
m_mutex.lock();
|
||||||
IndexWriter luceneWriter = IndexWriter( m_luceneDir, m_analyzer, true );
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IndexWriter luceneWriter = IndexWriter( m_luceneDir, m_analyzer, true );
|
||||||
|
}
|
||||||
|
catch( CLuceneError& error )
|
||||||
|
{
|
||||||
|
qDebug() << "Caught CLucene error:" << error.what();
|
||||||
|
Q_ASSERT( false );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,39 +69,47 @@ FuzzyIndex::endIndexing()
|
|||||||
void
|
void
|
||||||
FuzzyIndex::appendFields( const QString& table, const QMap< unsigned int, QString >& fields )
|
FuzzyIndex::appendFields( const QString& table, const QMap< unsigned int, QString >& fields )
|
||||||
{
|
{
|
||||||
delete m_luceneSearcher;
|
try
|
||||||
delete m_luceneReader;
|
|
||||||
m_luceneSearcher = 0;
|
|
||||||
m_luceneReader = 0;
|
|
||||||
|
|
||||||
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();
|
delete m_luceneSearcher;
|
||||||
unsigned int id = it.key();
|
delete m_luceneReader;
|
||||||
QString name = it.value();
|
m_luceneSearcher = 0;
|
||||||
|
m_luceneReader = 0;
|
||||||
|
|
||||||
|
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() )
|
||||||
{
|
{
|
||||||
Field* field = _CLNEW Field( table.toStdWString().c_str(), name.toStdWString().c_str(),
|
it.next();
|
||||||
Field::STORE_YES | Field::INDEX_UNTOKENIZED );
|
unsigned int id = it.key();
|
||||||
doc.add( *field );
|
QString name = it.value();
|
||||||
|
|
||||||
|
{
|
||||||
|
Field* field = _CLNEW Field( table.toStdWString().c_str(), 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();
|
||||||
Field* field = _CLNEW Field( _T( "id" ), QString::number( id ).toStdWString().c_str(),
|
}
|
||||||
Field::STORE_YES | Field::INDEX_NO );
|
catch( CLuceneError& error )
|
||||||
doc.add( *field );
|
{
|
||||||
}
|
qDebug() << "Caught CLucene error:" << error.what();
|
||||||
|
Q_ASSERT( false );
|
||||||
luceneWriter.addDocument( &doc );
|
|
||||||
doc.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
luceneWriter.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -109,47 +126,55 @@ FuzzyIndex::search( const QString& table, const QString& name )
|
|||||||
QMutexLocker lock( &m_mutex );
|
QMutexLocker lock( &m_mutex );
|
||||||
|
|
||||||
QMap< int, float > resultsmap;
|
QMap< int, float > resultsmap;
|
||||||
if ( !m_luceneReader )
|
try
|
||||||
{
|
{
|
||||||
if ( !IndexReader::indexExists( TomahawkUtils::appDataDir().absoluteFilePath( "tomahawk.lucene" ).toStdString().c_str() ) )
|
if ( !m_luceneReader )
|
||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO << "index didn't exist.";
|
if ( !IndexReader::indexExists( TomahawkUtils::appDataDir().absoluteFilePath( "tomahawk.lucene" ).toStdString().c_str() ) )
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO << "index didn't exist.";
|
||||||
|
return resultsmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_luceneReader = IndexReader::open( m_luceneDir );
|
||||||
|
m_luceneSearcher = _CLNEW IndexSearcher( m_luceneReader );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( name.isEmpty() )
|
||||||
return resultsmap;
|
return resultsmap;
|
||||||
}
|
|
||||||
|
|
||||||
m_luceneReader = IndexReader::open( m_luceneDir );
|
SimpleAnalyzer analyzer;
|
||||||
m_luceneSearcher = _CLNEW IndexSearcher( m_luceneReader );
|
QueryParser parser( table.toStdWString().c_str(), m_analyzer );
|
||||||
}
|
Hits* hits = 0;
|
||||||
|
|
||||||
if ( name.isEmpty() )
|
FuzzyQuery* qry = _CLNEW FuzzyQuery( _CLNEW Term( table.toStdWString().c_str(), name.toStdWString().c_str() ) );
|
||||||
return resultsmap;
|
hits = m_luceneSearcher->search( qry );
|
||||||
|
|
||||||
SimpleAnalyzer analyzer;
|
for ( int i = 0; i < hits->length(); i++ )
|
||||||
QueryParser parser( table.toStdWString().c_str(), m_analyzer );
|
|
||||||
Hits* hits = 0;
|
|
||||||
|
|
||||||
FuzzyQuery* qry = _CLNEW FuzzyQuery( _CLNEW Term( table.toStdWString().c_str(), name.toStdWString().c_str() ) );
|
|
||||||
hits = m_luceneSearcher->search( qry );
|
|
||||||
|
|
||||||
for ( int i = 0; i < hits->length(); i++ )
|
|
||||||
{
|
|
||||||
Document* d = &hits->doc( i );
|
|
||||||
|
|
||||||
float score = hits->score( i );
|
|
||||||
int id = QString::fromWCharArray( d->get( _T( "id" ) ) ).toInt();
|
|
||||||
QString result = QString::fromWCharArray( d->get( table.toStdWString().c_str() ) );
|
|
||||||
|
|
||||||
if ( result.toLower() == name.toLower() )
|
|
||||||
score = 1.0;
|
|
||||||
else
|
|
||||||
score = qMin( score, (float)0.99 );
|
|
||||||
|
|
||||||
if ( score > 0.05 )
|
|
||||||
{
|
{
|
||||||
resultsmap.insert( id, score );
|
Document* d = &hits->doc( i );
|
||||||
// qDebug() << "Hitres:" << result << id << score << table << name;
|
|
||||||
|
float score = hits->score( i );
|
||||||
|
int id = QString::fromWCharArray( d->get( _T( "id" ) ) ).toInt();
|
||||||
|
QString result = QString::fromWCharArray( d->get( table.toStdWString().c_str() ) );
|
||||||
|
|
||||||
|
if ( result.toLower() == name.toLower() )
|
||||||
|
score = 1.0;
|
||||||
|
else
|
||||||
|
score = qMin( score, (float)0.99 );
|
||||||
|
|
||||||
|
if ( score > 0.05 )
|
||||||
|
{
|
||||||
|
resultsmap.insert( id, score );
|
||||||
|
// qDebug() << "Hitres:" << result << id << score << table << name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch( CLuceneError& error )
|
||||||
|
{
|
||||||
|
qDebug() << "Caught CLucene error:" << error.what();
|
||||||
|
Q_ASSERT( false );
|
||||||
|
}
|
||||||
|
|
||||||
return resultsmap;
|
return resultsmap;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user