private Query toTwoHandSidedQuery(Operation<?> operation, Occur occur, QueryMetadata metadata) {
   Query lhs = toQuery(operation.getArg(0), metadata);
   Query rhs = toQuery(operation.getArg(1), metadata);
   BooleanQuery bq = new BooleanQuery();
   bq.add(createBooleanClause(lhs, occur));
   bq.add(createBooleanClause(rhs, occur));
   return bq;
 }
 @Override
 public Query apply(Function input, Context context) {
   assert input != null;
   BooleanQuery query = new BooleanQuery();
   for (Symbol symbol : input.arguments()) {
     query.add(process(symbol, context), BooleanClause.Occur.MUST);
   }
   return query;
 }
  public void XtestSetGenerationPerformance() throws Exception {
    createIndex(49999);
    doSetGen(10000, new TermQuery(new Term("foo1_s", t(0))));

    BooleanQuery bq = new BooleanQuery();
    bq.add(new TermQuery(new Term("foo2_s", t(0))), BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("foo2_s", t(1))), BooleanClause.Occur.SHOULD);
    doSetGen(5000, bq);
  }
 private BooleanQuery createQuery(String hashes) {
   StringTokenizer tok = new StringTokenizer(hashes);
   BooleanQuery q = new BooleanQuery();
   while (tok.hasMoreTokens())
     q.add(
         new BooleanClause(
             new TermQuery(new Term("hash", tok.nextToken())), BooleanClause.Occur.SHOULD));
   return q;
 }
 @Override
 public Query apply(Function input, Context context) {
   assert input != null;
   BooleanQuery query = new BooleanQuery();
   query.setMinimumNumberShouldMatch(1);
   for (Symbol symbol : input.arguments()) {
     query.add(process(symbol, context), BooleanClause.Occur.SHOULD);
   }
   return query;
 }
 private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
   Operator op = operation.getOperator();
   if (op == Ops.OR) {
     return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata);
   } else if (op == Ops.AND) {
     return toTwoHandSidedQuery(operation, Occur.MUST, metadata);
   } else if (op == Ops.NOT) {
     BooleanQuery bq = new BooleanQuery();
     bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT));
     bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
     return bq;
   } else if (op == Ops.LIKE) {
     return like(operation, metadata);
   } else if (op == Ops.LIKE_IC) {
     throw new IgnoreCaseUnsupportedException();
   } else if (op == Ops.EQ) {
     return eq(operation, metadata, false);
   } else if (op == Ops.EQ_IGNORE_CASE) {
     throw new IgnoreCaseUnsupportedException();
   } else if (op == Ops.NE) {
     return ne(operation, metadata, false);
   } else if (op == Ops.STARTS_WITH) {
     return startsWith(metadata, operation, false);
   } else if (op == Ops.STARTS_WITH_IC) {
     throw new IgnoreCaseUnsupportedException();
   } else if (op == Ops.ENDS_WITH) {
     return endsWith(operation, metadata, false);
   } else if (op == Ops.ENDS_WITH_IC) {
     throw new IgnoreCaseUnsupportedException();
   } else if (op == Ops.STRING_CONTAINS) {
     return stringContains(operation, metadata, false);
   } else if (op == Ops.STRING_CONTAINS_IC) {
     throw new IgnoreCaseUnsupportedException();
   } else if (op == Ops.BETWEEN) {
     return between(operation, metadata);
   } else if (op == Ops.IN) {
     return in(operation, metadata, false);
   } else if (op == Ops.NOT_IN) {
     return notIn(operation, metadata, false);
   } else if (op == Ops.LT) {
     return lt(operation, metadata);
   } else if (op == Ops.GT) {
     return gt(operation, metadata);
   } else if (op == Ops.LOE) {
     return le(operation, metadata);
   } else if (op == Ops.GOE) {
     return ge(operation, metadata);
   } else if (op == LuceneOps.LUCENE_QUERY) {
     @SuppressWarnings("unchecked") // This is the expected type
     Query rv = ((Constant<Query>) operation.getArg(0)).getConstant();
     return rv;
   }
   throw new UnsupportedOperationException("Illegal operation " + operation);
 }
      @Override
      public Query apply(Function input, Context context) {
        assert input != null;
        assert input.arguments().size() == 1;
        BooleanQuery query = new BooleanQuery();

        query.add(process(input.arguments().get(0), context), BooleanClause.Occur.MUST_NOT);
        query.add(Queries.newMatchAllQuery(), BooleanClause.Occur.MUST);

        return query;
      }
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col like ANY (['a', 'b']) --> or(like(col, 'a'), like(col, 'b'))
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         likeQuery.toQuery(reference, value, context), BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col < ANY ([1,2,3]) --> or(col<1, col<2, col<3)
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         inverseRangeQuery.toQuery(reference, reference.valueType(), value),
         BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
 @Override
 public Query rewrite(IndexReader reader) {
   if (termArrays.size() == 1) { // optimize one-term case
     Term[] terms = termArrays.get(0);
     BooleanQuery boq = new BooleanQuery(true);
     for (int i = 0; i < terms.length; i++) {
       boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
     }
     boq.setBoost(getBoost());
     return boq;
   } else {
     return this;
   }
 }
 protected Query like(Operation<?> operation, QueryMetadata metadata) {
   verifyArguments(operation);
   Path<?> path = getPath(operation.getArg(0));
   String field = toField(path);
   String[] terms = convert(path, operation.getArg(1));
   if (terms.length > 1) {
     BooleanQuery bq = new BooleanQuery();
     for (String s : terms) {
       bq.add(new WildcardQuery(new Term(field, "*" + s + "*")), Occur.MUST);
     }
     return bq;
   }
   return new WildcardQuery(new Term(field, terms[0]));
 }
 protected Query endsWith(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
   verifyArguments(operation);
   Path<?> path = getPath(operation.getArg(0));
   String field = toField(path);
   String[] terms = convertEscaped(path, operation.getArg(1), metadata);
   if (terms.length > 1) {
     BooleanQuery bq = new BooleanQuery();
     for (int i = 0; i < terms.length; ++i) {
       String s = i == terms.length - 1 ? "*" + terms[i] : "*" + terms[i] + "*";
       bq.add(new WildcardQuery(new Term(field, s)), Occur.MUST);
     }
     return bq;
   }
   return new WildcardQuery(new Term(field, "*" + terms[0]));
 }
      @Override
      protected Query applyArrayReference(
          Reference arrayReference, Literal literal, Context context) throws IOException {
        // 1 != any ( col ) -->  gt 1 or lt 1
        String columnName = arrayReference.info().ident().columnIdent().fqn();
        Object value = literal.value();

        QueryBuilderHelper builder = QueryBuilderHelper.forType(arrayReference.valueType());
        BooleanQuery query = new BooleanQuery();
        query.setMinimumNumberShouldMatch(1);
        query.add(
            builder.rangeQuery(columnName, value, null, false, false), BooleanClause.Occur.SHOULD);
        query.add(
            builder.rangeQuery(columnName, null, value, false, false), BooleanClause.Occur.SHOULD);
        return query;
      }
 @Override
 public Query apply(Function parent, Function inner, Context context) throws IOException {
   FunctionLiteralPair outerPair = new FunctionLiteralPair(parent);
   if (!outerPair.isValid()) {
     return null;
   }
   Query query = getQuery(inner, context);
   if (query == null) return null;
   Boolean negate = !(Boolean) outerPair.input().value();
   if (negate) {
     BooleanQuery booleanQuery = new BooleanQuery();
     booleanQuery.add(query, BooleanClause.Occur.MUST_NOT);
     return booleanQuery;
   } else {
     return query;
   }
 }
      @Override
      protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
          throws IOException {
        // col not like ANY (['a', 'b']) --> not(and(like(col, 'a'), like(col, 'b')))
        BooleanQuery query = new BooleanQuery();
        BooleanQuery notQuery = new BooleanQuery();

        String columnName = reference.ident().columnIdent().fqn();
        QueryBuilderHelper builder = QueryBuilderHelper.forType(reference.valueType());
        for (Object value : toIterable(arrayLiteral.value())) {
          notQuery.add(
              builder.like(columnName, value, context.indexCache.filter()),
              BooleanClause.Occur.MUST);
        }
        query.add(notQuery, BooleanClause.Occur.MUST_NOT);
        return query;
      }
 protected Query in(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
   Path<?> path = getPath(operation.getArg(0));
   String field = toField(path);
   @SuppressWarnings("unchecked") // This is the second argument type
   Constant<Collection<?>> collConstant = (Constant<Collection<?>>) operation.getArg(1);
   Collection<?> values = collConstant.getConstant();
   BooleanQuery bq = new BooleanQuery();
   if (Number.class.isAssignableFrom(path.getType())) {
     for (Object value : values) {
       TermQuery eq = new TermQuery(new Term(field, convertNumber((Number) value)));
       bq.add(eq, Occur.SHOULD);
     }
   } else {
     for (Object value : values) {
       String[] str = convert(path, value);
       bq.add(eq(field, str, ignoreCase), Occur.SHOULD);
     }
   }
   return bq;
 }
  /** Add to an existing boolean query the More Like This query from this PriorityQueue */
  private void addToQuery(PriorityQueue<ScoreTerm> q, BooleanQuery query) {
    ScoreTerm scoreTerm;
    float bestScore = -1;

    while ((scoreTerm = q.pop()) != null) {
      TermQuery tq = new TermQuery(new Term(scoreTerm.topField, scoreTerm.word));

      if (boost) {
        if (bestScore == -1) {
          bestScore = (scoreTerm.score);
        }
        float myScore = (scoreTerm.score);
        tq.setBoost(boostFactor * myScore / bestScore);
      }

      try {
        query.add(tq, BooleanClause.Occur.SHOULD);
      } catch (BooleanQuery.TooManyClauses ignore) {
        break;
      }
    }
  }
 protected Query ne(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
   BooleanQuery bq = new BooleanQuery();
   bq.add(new BooleanClause(eq(operation, metadata, ignoreCase), Occur.MUST_NOT));
   bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
   return bq;
 }