* Limit number of query terms and extra query terms
* @param input
* @param output
*/
public Query limitTerms(Query input) {
Query output=new Query(input.getConf());
Clause[] clauses = input.getClauses();
int termsCounter=0;
int termsExtraCounter=0;
for (int i=0; i<clauses.length; i++) {
Clause c = clauses[i];
if (c.getField().equals(Clause.DEFAULT_FIELD) && !c.isProhibited() && termsCounter>=maxQueryTerms) { // is it is a term and reached the limiti
continue;
}
if ((!c.getField().equals(Clause.DEFAULT_FIELD) || c.isProhibited()) && termsExtraCounter>=maxQueryExtraTerms) // it is an exstra term or a not
continue;
if (c.isPhrase()) {
Term[] terms = c.getPhrase().getTerms();
int newLength=terms.length;
if (c.getField().equals(Clause.DEFAULT_FIELD) && !c.isProhibited()) {
if (terms.length+termsCounter>maxQueryTerms) {
newLength=maxQueryTerms-termsCounter;
termsCounter+=newLength;
}
else {
termsCounter+=terms.length;
}
}
else {
if (terms.length+termsExtraCounter>maxQueryExtraTerms) {
newLength=maxQueryExtraTerms-termsExtraCounter;
termsExtraCounter+=newLength;
}
else {
termsExtraCounter+=terms.length;
}
}
if (newLength!=terms.length) {
if (newLength==1) {
output.addClause(new Clause(terms[0], c.isRequired(), c.isProhibited(), c.getConf()));
}
else {
Term[] newTerms=new Term[newLength];
System.arraycopy(terms, 0, newTerms, 0, newLength);
output.addClause(new Clause(new Phrase(newTerms), c.isRequired(), c.isProhibited(), c.getConf()));
}
}
else {
output.addClause(c);
}
}
else {
output.addClause(c);
if (c.getField().equals(Clause.DEFAULT_FIELD) && !c.isProhibited()) {
termsCounter++;
}
else {
termsExtraCounter++;