本文整理汇总了C++中BooleanQuery类的典型用法代码示例。如果您正苦于以下问题:C++ BooleanQuery类的具体用法?C++ BooleanQuery怎么用?C++ BooleanQuery使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BooleanQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CL_NS
//static
Query* Query::mergeBooleanQueries(Query** queries) {
CL_NS(util)::CLVector<BooleanClause*> allClauses;
int32_t i = 0;
while ( queries[i] != NULL ){
BooleanQuery* bq = (BooleanQuery*)queries[i];
int32_t size = bq->getClauseCount();
BooleanClause** clauses = _CL_NEWARRAY(BooleanClause*, size);
bq->getClauses(clauses);
for (int32_t j = 0;j<size;++j ){
allClauses.push_back(clauses[j]);
j++;
}
_CLDELETE_ARRAY(clauses);
i++;
}
BooleanQuery* result = _CLNEW BooleanQuery();
CL_NS(util)::CLVector<BooleanClause*>::iterator itr = allClauses.begin();
while (itr != allClauses.end() ) {
result->add(*itr);
}
return result;
}
示例2: testSrchWildcard
void testSrchWildcard(CuTest *tc ){
#ifdef NO_WILDCARD_QUERY
CuNotImpl(tc,_T("Wildcard"));
#else
CuAssert(tc,_T("Searcher was not open"),s!=NULL);
//testWildcard
_TestSearchesRun(tc, &a,s, _T("term*") );
_TestSearchesRun(tc, &a,s, _T("term*^2") );
_TestSearchesRun(tc, &a,s, _T("term~") );
_TestSearchesRun(tc, &a,s, _T("term^2~") );
_TestSearchesRun(tc, &a,s, _T("term~^2") );
_TestSearchesRun(tc, &a,s, _T("term*germ") );
_TestSearchesRun(tc, &a,s, _T("term*germ^3") );
//test problem reported by Gary Mangum
BooleanQuery* bq = _CLNEW BooleanQuery();
Term* upper = _CLNEW Term(_T("contents"),_T("0105"));
Term* lower = _CLNEW Term(_T("contents"),_T("0105"));
RangeQuery* rq=_CLNEW RangeQuery(lower,upper,true);
bq->add(rq,true,true,false);
_CLDECDELETE(upper);
_CLDECDELETE(lower);
Term* prefix = _CLNEW Term(_T("contents"),_T("reuters21578"));
PrefixQuery* pq = _CLNEW PrefixQuery(prefix);
_CLDECDELETE(prefix);
bq->add(pq,true,true,false);
Hits* h = NULL;
try{
h = s->search( bq );
}_CLFINALLY(
_CLDELETE(h);
_CLDELETE(bq);
);
示例3: BooleanQuery
//static
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** fields, const uint8_t* flags, Analyzer* analyzer)
{
BooleanQuery* bQuery = _CLNEW BooleanQuery();
int32_t i = 0;
while ( fields[i] != NULL )
{
Query* q = QueryParser::parse(query, fields[i], analyzer);
uint8_t flag = flags[i];
switch (flag)
{
case MultiFieldQueryParser::REQUIRED_FIELD:
bQuery->add(q, true, true, false);
break;
case MultiFieldQueryParser::PROHIBITED_FIELD:
bQuery->add(q, true, false, true);
break;
default:
bQuery->add(q, true, false, false);
break;
}
i++;
}
return bQuery;
}
示例4: testBoost
void testBoost()
{
// NOTE: uses index build in *this* setUp
IndexReader * pReader = IndexReader::open( m_pSmall );
IndexSearcher * pSearch = _CLNEW IndexSearcher( pReader );
Hits * pResult;
// test for correct application of query normalization
// must use a non score normalizing method for this.
Query * q = csrq( _T( "data" ), _T( "1" ), _T( "6" ), true, true );
q->setBoost( 100 );
pResult = pSearch->search( q );
for( size_t i = 1; i < pResult->length(); i++ )
{
assertTrueMsg( _T( "score was not was not correct" ), 1.0f == pResult->score( i ));
}
_CLDELETE( pResult );
_CLDELETE( q );
//
// Ensure that boosting works to score one clause of a query higher
// than another.
//
Query * q1 = csrq( _T( "data" ), _T( "A" ), _T( "A" ), true, true ); // matches document #0
q1->setBoost( .1f );
Query * q2 = csrq( _T( "data" ), _T( "Z" ), _T( "Z" ), true, true ); // matches document #1
BooleanQuery * bq = _CLNEW BooleanQuery( true );
bq->add( q1, true, BooleanClause::SHOULD );
bq->add( q2, true, BooleanClause::SHOULD );
pResult = pSearch->search( bq );
assertEquals( 1, pResult->id( 0 ));
assertEquals( 0, pResult->id( 1 ));
assertTrue( pResult->score( 0 ) > pResult->score( 1 ));
_CLDELETE( pResult );
_CLDELETE( bq );
q1 = csrq( _T( "data" ), _T( "A" ), _T( "A" ), true, true ); // matches document #0
q1->setBoost( 10.0f );
q2 = csrq( _T( "data" ), _T( "Z" ), _T( "Z" ), true, true ); // matches document #1
bq = _CLNEW BooleanQuery( true );
bq->add( q1, true, BooleanClause::SHOULD );
bq->add( q2, true, BooleanClause::SHOULD );
pResult = pSearch->search( bq );
assertEquals( 0, pResult->id( 0 ));
assertEquals( 1, pResult->id( 1 ));
assertTrue( pResult->score( 0 ) > pResult->score( 1 ));
_CLDELETE( pResult );
_CLDELETE( bq );
pSearch->close();
_CLDELETE( pSearch );
pReader->close();
_CLDELETE( pReader );
}
示例5: BooleanQuery
Query*
CLuceneIndexReader::Private::createMultiFieldQuery(const Strigi::Query& query) {
BooleanQuery* bq = _CLNEW BooleanQuery();
for (vector<string>::const_iterator i = query.fields().begin();
i != query.fields().end(); ++i) {
Query* q = createSingleFieldQuery(*i, query);
bq->add(q, true, false, false);
}
return bq;
}
示例6: Term
void TestBasics::testBoolean2()
{
Term * term1 = _CLNEW Term( _T( "field" ), _T( "sevento" ));
Term * term2 = _CLNEW Term( _T( "field" ), _T( "sevenly" ));
BooleanQuery * query = _CLNEW BooleanQuery();
query->add( _CLNEW TermQuery( term1 ), true, BooleanClause::MUST );
query->add( _CLNEW TermQuery( term2 ), true, BooleanClause::MUST );
_CLLDECDELETE( term1 );
_CLLDECDELETE( term2 );
checkHits( query, NULL, 0 );
_CLLDELETE( query );
}
示例7: rewrite
/**
* FIXME: Describe <code>rewrite</code> method here.
*
* @param reader an <code>IndexReader</code> value
* @return a <code>Query</code> value
* @exception IOException if an error occurs
*/
Query* RangeQuery::rewrite(IndexReader* reader){
BooleanQuery* query = _CLNEW BooleanQuery;
TermEnum* enumerator = reader->terms(lowerTerm);
Term* lastTerm = NULL;
try {
bool checkLower = false;
if (!inclusive) // make adjustments to set to exclusive
checkLower = true;
const TCHAR* testField = getField();
do {
lastTerm = enumerator->term();
if (lastTerm != NULL && lastTerm->field() == testField ) {
if (!checkLower || _tcscmp(lastTerm->text(),lowerTerm->text()) > 0) {
checkLower = false;
if (upperTerm != NULL) {
int compare = _tcscmp(upperTerm->text(),lastTerm->text());
/* if beyond the upper term, or is exclusive and
* this is equal to the upper term, break out */
if ((compare < 0) || (!inclusive && compare == 0))
break;
}
TermQuery* tq = _CLNEW TermQuery(lastTerm); // found a match
tq->setBoost(getBoost()); // set the boost
query->add(tq, true, false, false); // add to query
}
}else {
break;
}
_CLDECDELETE(lastTerm);
}
while (enumerator->next());
}catch(...){
_CLDECDELETE(lastTerm); //always need to delete this
_CLDELETE(query); //in case of error, delete the query
enumerator->close();
_CLDELETE(enumerator);
throw; //rethrow
}
_CLDECDELETE(lastTerm); //always need to delete this
enumerator->close();
_CLDELETE(enumerator);
return query;
}
示例8: Term
void TestSpansAdvanced::doTestBooleanQueryWithSpanQueries( const float_t expectedScore )
{
Term * t1 = _CLNEW Term( field_text, _T( "work" ));
Query * spanQuery = _CLNEW SpanTermQuery( t1 );
BooleanQuery * query = _CLNEW BooleanQuery();
query->add( spanQuery, false, BooleanClause::MUST );
query->add( spanQuery, false, BooleanClause::MUST );
TCHAR * expectedIds[] = { _T( "1" ), _T( "2" ), _T( "3" ), _T( "4" ) };
float_t expectedScores[] = { expectedScore, expectedScore, expectedScore, expectedScore };
assertHits( query, _T( "two span queries" ), expectedIds, expectedScores, 4 );
_CLLDECDELETE( t1 );
_CLLDELETE( spanQuery );
_CLLDELETE( query );
}
示例9: 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());
}
}
示例10: BooleanQuery
Query* PrefixQuery::rewrite(IndexReader* reader){
BooleanQuery* query = _CLNEW BooleanQuery();
TermEnum* enumerator = reader->terms(prefix);
Term* lastTerm = NULL;
try {
const TCHAR* prefixText = prefix->text();
const TCHAR* prefixField = prefix->field();
const TCHAR* tmp;
size_t i;
int32_t prefixLen = prefix->textLength();
do {
lastTerm = enumerator->term();
if (lastTerm != NULL && lastTerm->field() == prefixField ){
//now see if term->text() starts with prefixText
int32_t termLen = lastTerm->textLength();
if ( prefixLen>termLen )
break; //the prefix is longer than the term, can't be matched
tmp = lastTerm->text();
//check for prefix match in reverse, since most change will be at the end
for ( i=prefixLen-1;i!=-1;--i ){
if ( tmp[i] != prefixText[i] ){
tmp=NULL;//signals inequality
break;
}
}
if ( tmp == NULL )
break;
TermQuery* tq = _CLNEW TermQuery(lastTerm); // found a match
tq->setBoost(getBoost()); // set the boost
query->add(tq,true,false, false); // add to query
} else
break;
_CLDECDELETE(lastTerm);
} while (enumerator->next());
}_CLFINALLY(
enumerator->close();
_CLDELETE(enumerator);
_CLDECDELETE(lastTerm);
);
示例11: BooleanQuery
// static
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** _fields, const uint8_t* flags, Analyzer* analyzer) {
BooleanQuery* bQuery = _CLNEW BooleanQuery();
for (size_t i = 0; _fields[i]!=NULL; i++) {
//TODO: this is really confusing... why not refactor _fields and flags to use a array object.
//flags can be NULL since NULL == 0...
/*if (flags[i] == NULL) {
_CLLDELETE(bQuery);
_CLTHROWA(CL_ERR_IllegalArgument, "_fields.length != flags.length");
}*/
QueryParser* qp = _CLNEW QueryParser(_fields[i], analyzer);
Query* q = qp->parse(query);
if (q!=NULL && // q never null, just being defensive
(!(q->instanceOf(BooleanQuery::getClassName())) || ((BooleanQuery*)q)->getClauseCount()>0)) {
bQuery->add(q, true, (BooleanClause::Occur)flags[i]);
} else
_CLLDELETE(q);
_CLLDELETE(qp);
}
return bQuery;
}
示例12: Term
/**
* Tests a single span query that matches multiple documents.
*/
void TestSpansAdvanced2::testMultipleDifferentSpanQueries()
{
Term * t1 = _CLNEW Term( field_text, _T( "should" ));
Term * t2 = _CLNEW Term( field_text, _T( "we" ));
Query * spanQuery1 = _CLNEW SpanTermQuery( t1 );
Query * spanQuery2 = _CLNEW SpanTermQuery( t2 );
BooleanQuery * query = _CLNEW BooleanQuery();
query->add( spanQuery1, true, BooleanClause::MUST );
query->add( spanQuery2, true, BooleanClause::MUST );
TCHAR * expectedIds[] = { _T( "D" ), _T( "A" ) };
// these values were pre LUCENE-413
// final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
float_t expectedScores[] = { 1.0191123f, 0.93163157f };
assertHits( query, _T( "multiple different span queries" ), expectedIds, expectedScores, 2 );
_CLDELETE( query );
_CLLDECDELETE( t1 );
_CLLDECDELETE( t2 );
}
示例13: testBooleanOrderUnAffected
void testBooleanOrderUnAffected()
{
// NOTE: uses index build in *this* setUp
IndexReader * pReader = IndexReader::open( m_pSmall );
IndexSearcher * pSearch = _CLNEW IndexSearcher( pReader );
// first do a regular RangeQuery which uses term expansion so
// docs with more terms in range get higher scores
Term * pLower = _CLNEW Term( _T( "data" ), _T( "1" ));
Term * pUpper = _CLNEW Term( _T( "data" ), _T( "4" ));
Query * rq = _CLNEW RangeQuery( pLower, pUpper, true );
_CLLDECDELETE( pUpper );
_CLLDECDELETE( pLower );
Hits * pExpected = pSearch->search( rq );
size_t numHits = pExpected->length();
// now do a boolean where which also contains a
// ConstantScoreRangeQuery and make sure the order is the same
BooleanQuery * q = _CLNEW BooleanQuery();
q->add( rq, true, BooleanClause::MUST );
q->add( csrq( _T( "data" ), _T( "1" ), _T( "6" ), true, true ), true, BooleanClause::MUST );
Hits * pActual = pSearch->search( q );
assertEqualsMsg( _T( "wrong number of hits" ), numHits, pActual->length() );
for( size_t i = 0; i < numHits; i++ )
{
assertEqualsMsg( _T( "mismatch in docid for a hit" ), pExpected->id( i ), pActual->id( i ));
}
_CLDELETE( pActual );
_CLDELETE( pExpected );
_CLDELETE( q );
pSearch->close();
_CLDELETE( pSearch );
pReader->close();
_CLDELETE( pReader );
}
示例14: testExtractFromBooleanQuery
void testExtractFromBooleanQuery( CuTest * tc )
{
Directory * pIndex = setUpIndex();
IndexReader * pReader = IndexReader::open( pIndex );
TermSet termSet;
Term * t1 = _CLNEW Term( _T("data"), _T("aaaab") );
Term * t2 = _CLNEW Term( _T("data"), _T("aaabb") );
Term * t3 = _CLNEW Term( _T("data"), _T("aaabb") );
BooleanQuery * bq = _CLNEW BooleanQuery();
bq->add( _CLNEW TermQuery( t1 ), true, BooleanClause::SHOULD );
bq->add( _CLNEW TermQuery( t2 ), true, BooleanClause::SHOULD );
bq->add( _CLNEW TermQuery( t3 ), true, BooleanClause::SHOULD );
Query * rewrite = bq->rewrite( pReader );
rewrite->extractTerms( &termSet );
assertEqualsMsg( _T( "wrong number of terms" ), 2, termSet.size() );
for( TermSet::iterator itTerms = termSet.begin(); itTerms != termSet.end(); itTerms++ )
{
Term * pTerm = *itTerms;
assertTrueMsg( _T( "wrong term" ), ( 0 == t1->compareTo( pTerm ) || 0 == t2->compareTo( pTerm )));
}
clearTermSet( termSet );
_CLLDECDELETE( t1 );
_CLLDECDELETE( t2 );
_CLLDECDELETE( t3 );
if( rewrite != bq )
_CLDELETE( rewrite );
_CLDELETE( bq );
pReader->close();
_CLDELETE( pReader );
closeIndex( pIndex );
pIndex = NULL;
}
示例15: testBooleanMemLeaks
/////////////////////////////////////////////////////////////////////////////
// CLucene specific
// Visual Studio 2005 shows memory leaks for this test, but some other
// tools do not detect any memory leaks. So what is right?
// IN VC80 shows memory leaks ONLY if both sub-queries are added as
// MUST BooleanClauses.
void testBooleanMemLeaks()
{
IndexReader * pReader = IndexReader::open( m_pSmall );
IndexSearcher * pSearch = _CLNEW IndexSearcher( pReader );
Query * q1 = csrq( _T( "data" ), _T( "A" ), _T( "A" ), true, true ); // matches document #0
Query * q2 = csrq( _T( "data" ), _T( "Z" ), _T( "Z" ), true, true ); // matches document #1
BooleanQuery * bq = _CLNEW BooleanQuery( true );
bq->add( q1, true, BooleanClause::MUST );
bq->add( q2, true, BooleanClause::MUST );
Hits * pResult = pSearch->search( bq );
_CLDELETE( pResult );
_CLDELETE( bq );
pSearch->close();
_CLDELETE( pSearch );
pReader->close();
_CLDELETE( pReader );
}