QueryPtr DisjunctionMaxQuery::rewrite(IndexReaderPtr reader) { int32_t numDisjunctions = disjuncts.size(); if (numDisjunctions == 1) { QueryPtr singleton(disjuncts[0]); QueryPtr result(singleton->rewrite(reader)); if (getBoost() != 1.0) { if (result == singleton) result = boost::dynamic_pointer_cast<Query>(result->clone()); result->setBoost(getBoost() * result->getBoost()); } return result; } DisjunctionMaxQueryPtr clone; for (int32_t i = 0; i < numDisjunctions; ++i) { QueryPtr clause(disjuncts[i]); QueryPtr rewrite(clause->rewrite(reader)); if (rewrite != clause) { if (!clone) clone = boost::dynamic_pointer_cast<DisjunctionMaxQuery>(this->clone()); clone->disjuncts[i] = rewrite; } } return clone ? clone : shared_from_this(); }
int32_t SpanNotQuery::hashCode() { int32_t result = include->hashCode(); result = (result << 1) | MiscUtils::unsignedShift(result, 31); // rotate left result ^= exclude->hashCode(); result = (result << 1) | MiscUtils::unsignedShift(result, 31); // rotate left result ^= MiscUtils::doubleToRawIntBits(getBoost()); return result; }
size_t FuzzyQuery::hashCode() const{ //todo: we should give the query a seeding value... but //need to do it for all hascode functions // TODO: does not conform with JL size_t val = Similarity::floatToByte(getBoost()) ^ getTerm()->hashCode(); val ^= Similarity::floatToByte(this->getMinSimilarity()); val ^= this->getPrefixLength(); return val; }
size_t BooleanQuery::hashCode() const { //todo: do cachedHashCode, and invalidate on add/remove clause size_t ret = 0; for (uint32_t i = 0 ; i < clauses.size(); i++) { BooleanClause* c = clauses[i]; ret = 31 * ret + c->hashCode(); } ret = ret ^ Similarity::floatToByte(getBoost()); return ret; }
QueryPtr WildcardQuery::rewrite(const IndexReaderPtr& reader) { if (termIsPrefix) { MultiTermQueryPtr rewritten(newLucene<PrefixQuery>(term->createTerm(term->text().substr(0, term->text().find('*'))))); rewritten->setBoost(getBoost()); rewritten->setRewriteMethod(getRewriteMethod()); return rewritten; } else { return MultiTermQuery::rewrite(reader); } }
String DisjunctionMaxQuery::toString(const String& field) { String buffer(L"("); for (Collection<QueryPtr>::iterator query = disjuncts.begin(); query != disjuncts.end(); ++query) { if (query != disjuncts.begin()) buffer += L" | "; if (boost::dynamic_pointer_cast<BooleanQuery>(*query)) // wrap sub-bools in parens buffer += L"(" + (*query)->toString(field) + L")"; else buffer += (*query)->toString(field); } buffer += L")"; if (tieBreakerMultiplier != 0.0) buffer += L"~" + StringUtils::toString(tieBreakerMultiplier); if (getBoost() != 1.0) buffer += L"^" + StringUtils::toString(getBoost()); return buffer; }
WeightPtr PhraseQuery::createWeight(SearcherPtr searcher) { if (terms.size() == 1) // optimize one-term case { QueryPtr termQuery(newLucene<TermQuery>(terms[0])); termQuery->setBoost(getBoost()); return termQuery->createWeight(searcher); } return newLucene<PhraseWeight>(shared_from_this(), searcher); }
TCHAR* ConstantScoreQuery::toString(const TCHAR* /*field*/) const { StringBuffer buf; buf.append(_T("ConstantScore(")); TCHAR* tmp = filter->toString(); buf.append(tmp); _CLLDELETE(tmp); buf.appendBoost(getBoost()); buf.appendChar(_T(')')); return buf.giveBuffer(); }
/** Prints a user-readable version of this query. */ TCHAR* RangeQuery::toString(const TCHAR* field) const { StringBuffer buffer; if ( field==NULL || _tcscmp(getField(),field)!=0 ) { buffer.append( getField() ); buffer.append( _T(":")); } buffer.append(inclusive ? _T("[") : _T("{")); buffer.append(lowerTerm != NULL ? lowerTerm->text() : _T("NULL")); buffer.append(_T(" TO ")); buffer.append(upperTerm != NULL ? upperTerm->text() : _T("NULL")); buffer.append(inclusive ? _T("]") : _T("}")); if (getBoost() != 1.0f) { buffer.append( _T("^")); buffer.appendFloat( getBoost(),1 ); } return buffer.toString(); }
bool PhraseQuery::equals(LuceneObjectPtr other) { if (LuceneObject::equals(other)) return true; PhraseQueryPtr otherPhraseQuery(boost::dynamic_pointer_cast<PhraseQuery>(other)); if (!otherPhraseQuery) return false; return (getBoost() == otherPhraseQuery->getBoost() && slop == otherPhraseQuery->slop && terms.equals(otherPhraseQuery->terms, luceneEquals<TermPtr>()) && positions.equals(otherPhraseQuery->positions)); }
LuceneObjectPtr SpanOrQuery::clone(const LuceneObjectPtr& other) { int32_t sz = clauses.size(); Collection<SpanQueryPtr> newClauses(Collection<SpanQueryPtr>::newInstance(sz)); for (int32_t i = 0; i < sz; ++i) { newClauses[i] = boost::dynamic_pointer_cast<SpanQuery>(clauses[i]->clone()); } SpanOrQueryPtr spanOrQuery(newLucene<SpanOrQuery>(newClauses)); spanOrQuery->setBoost(getBoost()); return spanOrQuery; }
TCHAR* FuzzyQuery::toString(const TCHAR* field) const{ StringBuffer buffer(100); // TODO: Have a better estimation for the initial buffer length Term* term = getTerm(false); // no need to increase ref count if ( field==NULL || _tcscmp(term->field(),field)!=0 ) { buffer.append(term->field()); buffer.appendChar( _T(':')); } buffer.append(term->text()); buffer.appendChar( _T('~') ); buffer.appendFloat(minimumSimilarity,1); buffer.appendBoost(getBoost()); return buffer.giveBuffer(); }
TCHAR* BooleanQuery::toString(const TCHAR* field) const{ StringBuffer buffer; if (getBoost() != 1.0) { buffer.append(_T("(")); } for (uint32_t i = 0 ; i < clauses.size(); i++) { BooleanClause* c = clauses[i]; if (c->prohibited) buffer.append(_T("-")); else if (c->required) buffer.append(_T("+")); if ( c->query->instanceOf(BooleanQuery::getClassName()) ) { // wrap sub-bools in parens buffer.append(_T("(")); TCHAR* buf = c->query->toString(field); buffer.append(buf); _CLDELETE_CARRAY( buf ); buffer.append(_T(")")); } else { TCHAR* buf = c->query->toString(field); buffer.append(buf); _CLDELETE_CARRAY( buf ); } if (i != clauses.size()-1) buffer.append(_T(" ")); if (getBoost() != 1.0) { buffer.append(_T(")^")); buffer.appendFloat(getBoost(),1); } } return buffer.toString(); }
/** * 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; }
QueryPtr FuzzyQuery::rewrite(const IndexReaderPtr& reader) { if (!termLongEnough) { // can only match if it's exact return newLucene<TermQuery>(term); } int32_t maxSize = BooleanQuery::getMaxClauseCount(); ScoreTermQueuePtr stQueue(newLucene<ScoreTermQueue>(maxSize + 1)); FilteredTermEnumPtr enumerator(getEnum(reader)); LuceneException finally; try { ScoreTermPtr st = newLucene<ScoreTerm>(); do { TermPtr t(enumerator->term()); if (!t) { break; } double score = enumerator->difference(); // ignore uncompetitive hits if (stQueue->size() >= maxSize && score <= stQueue->top()->score) { continue; } // add new entry in PQ st->term = t; st->score = score; stQueue->add(st); // possibly drop entries from queue st = (stQueue->size() > maxSize) ? stQueue->pop() : newLucene<ScoreTerm>(); } while (enumerator->next()); } catch (LuceneException& e) { finally = e; } enumerator->close(); finally.throwException(); BooleanQueryPtr query(newLucene<BooleanQuery>(true)); int32_t size = stQueue->size(); for (int32_t i = 0; i < size; ++i) { ScoreTermPtr st(stQueue->pop()); TermQueryPtr tq(newLucene<TermQuery>(st->term)); // found a match tq->setBoost(getBoost() * st->score); // set the boost query->add(tq, BooleanClause::SHOULD); // add to query } return query; }
bool SpanOrQuery::equals(const LuceneObjectPtr& other) { if (LuceneObject::equals(other)) { return true; } SpanOrQueryPtr otherQuery(boost::dynamic_pointer_cast<SpanOrQuery>(other)); if (!otherQuery) { return false; } if (!clauses.equals(otherQuery->clauses, luceneEquals<SpanQueryPtr>())) { return false; } if (!clauses.empty() && field != otherQuery->field) { return false; } return (getBoost() == otherQuery->getBoost()); }
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); );
int32_t SpanOrQuery::hashCode() { int32_t result = MiscUtils::hashCode(clauses.begin(), clauses.end(), MiscUtils::hashLucene<SpanQueryPtr>); result ^= (result << 10) | MiscUtils::unsignedShift(result, 23); result ^= MiscUtils::doubleToRawIntBits(getBoost()); return result; }
int32_t PhraseQuery::hashCode() { return MiscUtils::doubleToIntBits(getBoost()) ^ slop ^ MiscUtils::hashCode(terms.begin(), terms.end(), MiscUtils::hashLucene<TermPtr>) ^ MiscUtils::hashCode(positions.begin(), positions.end(), MiscUtils::hashNumeric<int32_t>); }
/** Returns a hash code value for this object.*/ size_t PrefixQuery::hashCode() const { return Similarity::floatToByte(getBoost()) ^ prefix->hashCode(); }
bool SpanNotQuery::equals(const LuceneObjectPtr& other) { if (LuceneObject::equals(other)) { return true; } SpanNotQueryPtr otherQuery(boost::dynamic_pointer_cast<SpanNotQuery>(other)); if (!otherQuery) { return false; } return (include->equals(otherQuery->include) && exclude->equals(otherQuery->exclude) && getBoost() == otherQuery->getBoost()); }
LuceneObjectPtr SpanNotQuery::clone(const LuceneObjectPtr& other) { SpanNotQueryPtr spanNotQuery(newLucene<SpanNotQuery>(boost::dynamic_pointer_cast<SpanQuery>(include->clone()), boost::dynamic_pointer_cast<SpanQuery>(exclude->clone()))); spanNotQuery->setBoost(getBoost()); return spanNotQuery; }
int32_t FilteredQuery::hashCode() { return query->hashCode() ^ filter->hashCode() + MiscUtils::doubleToIntBits(getBoost()); }
int32_t DisjunctionMaxQuery::hashCode() { return MiscUtils::doubleToIntBits(getBoost()) + MiscUtils::doubleToIntBits(tieBreakerMultiplier) + MiscUtils::hashCode(disjuncts.begin(), disjuncts.end(), MiscUtils::hashLucene<QueryPtr>); }
/** Returns a hash code value for this object.*/ size_t RangeQuery::hashCode() const { return Similarity::floatToByte(getBoost()) ^ (lowerTerm != NULL ? lowerTerm->hashCode() : 0) ^ (upperTerm != NULL ? upperTerm->hashCode() : 0) ^ (this->inclusive ? 1 : 0); }