*** QueryParser.jj.org Tue Jan 20 13:30:23 2004 --- QueryParser.jj Thu Jan 22 08:59:09 2004 *************** *** 236,258 **** protected void addClause(Vector clauses, int conj, int mods, Query q) { boolean required, prohibited; ! ! // If this term is introduced by AND, make the preceding term required, // unless it's already prohibited if (conj == CONJ_AND) { ! BooleanClause c = (BooleanClause) clauses.elementAt(clauses.size()-1); ! if (!c.prohibited) ! c.required = true; ! } ! ! if (operator == DEFAULT_OPERATOR_AND && conj == CONJ_OR) { ! // If this term is introduced by OR, make the preceding term optional, ! // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b) ! // notice if the input is a OR b, first term is parsed as required; without ! // this modification a OR b would parsed as +a OR b ! BooleanClause c = (BooleanClause) clauses.elementAt(clauses.size()-1); ! if (!c.prohibited) ! c.required = false; } // We might have been passed a null query; the term might have been --- 236,252 ---- protected void addClause(Vector clauses, int conj, int mods, Query q) { boolean required, prohibited; ! // System.out.println(conj+ " " + mods + " " + q.toString("text")); ! // If this term is introduced by AND, check if the previous term is the ! // first term in this or-group and make that term required, // unless it's already prohibited if (conj == CONJ_AND) { ! Vector clauses2 = (Vector)clauses.elementAt(clauses.size()-1); ! if ( clauses2.size() > 0 ) { ! BooleanClause c = (BooleanClause) clauses2.elementAt(clauses2.size()-1); ! if (!c.prohibited) ! c.required = true; ! } } // We might have been passed a null query; the term might have been *************** *** 260,280 **** if (q == null) return; if (operator == DEFAULT_OPERATOR_OR) { - // We set REQUIRED if we're introduced by AND or +; PROHIBITED if // introduced by NOT or -; make sure not to set both. prohibited = (mods == MOD_NOT); ! required = (mods == MOD_REQ); ! if (conj == CONJ_AND && !prohibited) { ! required = true; } ! } else { ! // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED ! // if not PROHIBITED and not introduced by OR prohibited = (mods == MOD_NOT); ! required = (!prohibited && conj != CONJ_OR); } ! clauses.addElement(new BooleanClause(q, required, prohibited)); } /** --- 254,282 ---- if (q == null) return; + // start new or-group if there's an explit or + if ( conj == CONJ_OR ) { + clauses.addElement(new Vector()); + } + if (operator == DEFAULT_OPERATOR_OR) { // introduced by NOT or -; make sure not to set both. prohibited = (mods == MOD_NOT); ! // for explizit conjunctions: set required to true ! if ( conj == CONJ_AND ) { ! required = true; } ! else { ! // default OR -> required only when requested ! required = (mods == MOD_REQ); ! } ! } else { // operator == DEFAULT_OPERATOR_AND ! // We set PROHIBITED if we're introduced by NOT or - prohibited = (mods == MOD_NOT); ! // always REQUIRED unless PROHIBITED ! required = (!prohibited); } ! ((Vector)clauses.elementAt(clauses.size()-1)).addElement(new BooleanClause(q, required, prohibited)); } /** *************** *** 362,372 **** */ protected Query getBooleanQuery(Vector clauses) throws ParseException { ! BooleanQuery query = new BooleanQuery(); ! for (int i = 0; i < clauses.size(); i++) { ! query.add((BooleanClause)clauses.elementAt(i)); ! } ! return query; } /** --- 364,405 ---- */ protected Query getBooleanQuery(Vector clauses) throws ParseException { ! BooleanQuery query = new BooleanQuery(); ! boolean empty = true; ! if ( clauses.size() == 1 ) { ! clauses = (Vector)clauses.elementAt(0); ! for (int i = 0; i < clauses.size(); i++) { ! BooleanClause clause = (BooleanClause)clauses.elementAt(i); ! if ( !clause.prohibited ) ! empty = false; // a query is empty only if it contains ! // nonprohibited terms... ! query.add(clause); ! } ! } ! else { ! for ( int i = 0; i < clauses.size(); i++ ) { ! Vector clauses2 = (Vector)clauses.elementAt(i); ! if ( clauses2.size() == 1 && ((BooleanClause)clauses2.elementAt(0)).prohibited == false ) { ! query.add(new BooleanClause(((BooleanClause)clauses2.elementAt(0)).query, false, false)); ! empty = false; ! } ! else if ( clauses2.size() >= 1 ) { ! BooleanQuery query2 = new BooleanQuery(); ! boolean empty2 = true; ! for ( int j = 0; j < clauses2.size(); j++ ) { ! BooleanClause clause = (BooleanClause)clauses2.elementAt(j); ! if ( !clause.prohibited ) ! empty2 = false; ! query2.add(clause); ! } ! if ( !empty2 ) { ! query.add(new BooleanClause(query2, false, false)); ! empty = false; ! } ! } ! } ! } ! return !empty ? query : null; } /** *************** *** 554,559 **** --- 587,593 ---- Query Query(String field) : { Vector clauses = new Vector(); + clauses.addElement(new Vector()); Query q, firstQuery=null; int conj, mods; } *************** *** 569,575 **** { addClause(clauses, conj, mods, q); } )* { ! if (clauses.size() == 1 && firstQuery != null) return firstQuery; else { return getBooleanQuery(clauses); --- 603,609 ---- { addClause(clauses, conj, mods, q); } )* { ! if (clauses.size() == 1 && ((Vector)clauses.elementAt(0)).size() == 1 && firstQuery != null) return firstQuery; else { return getBooleanQuery(clauses);