public void testMethod() {
    RAMDirectory directory = new RAMDirectory();

    String[] values = new String[] {"1", "2", "3", "4"};

    try {
      IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
      for (int i = 0; i < values.length; i++) {
        Document doc = new Document();
        doc.add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
        writer.addDocument(doc);
      }
      writer.close();

      BooleanQuery booleanQuery1 = new BooleanQuery();
      booleanQuery1.add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
      booleanQuery1.add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);

      BooleanQuery query = new BooleanQuery();
      query.add(booleanQuery1, BooleanClause.Occur.MUST);
      query.add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);

      IndexSearcher indexSearcher = new IndexSearcher(directory);
      Hits hits = indexSearcher.search(query);
      assertEquals("Number of matched documents", 2, hits.length());

    } catch (IOException e) {
      fail(e.getMessage());
    }
  }
  public void testBooleanRequiredEqualScores() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.MUST); // true,false);
      QueryUtils.check(q1, s);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
      q2.add(tq("hed", "elephant"));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.MUST); // true,false);
      QueryUtils.check(q2, s);
    }

    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("3 docs should match " + q.toString(), 3, h.length);
      float score = h[0].score;
      for (int i = 1; i < h.length; i++) {
        assertEquals("score #" + i + " is not the same", score, h[i].score, SCORE_COMP_THRESH);
      }
    } catch (Error e) {
      printHits("testBooleanRequiredEqualScores1", h, s);
      throw e;
    }
  }
  public void testMultiFieldBQ3() throws Exception {
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);

    qtest(query, new int[] {0, 1, 2, 3});
  }
  public void testBooleanOptionalNoTiebreaker() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
      q2.add(tq("hed", "elephant"));
      q2.add(tq("dek", "elephant"));
      q.add(q2, BooleanClause.Occur.SHOULD); // false,false);
    }
    QueryUtils.check(q, s);

    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;

    try {
      assertEquals("4 docs should match " + q.toString(), 4, h.length);
      float score = h[0].score;
      for (int i = 1; i < h.length - 1; i++) {
        /* note: -1 */
        assertEquals("score #" + i + " is not the same", score, h[i].score, SCORE_COMP_THRESH);
      }
      assertEquals("wrong last", "d1", s.doc(h[h.length - 1].doc).get("id"));
      float score1 = h[h.length - 1].score;
      assertTrue(
          "d1 does not have worse score then others: " + score + " >? " + score1, score > score1);
    } catch (Error e) {
      printHits("testBooleanOptionalNoTiebreaker", h, s);
      throw e;
    }
  }
  public void testBQ19() throws Exception {
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
    query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);

    qtest(query, new int[] {0, 1});
  }
Beispiel #6
0
  public int doNestedConjunctions(int iter, int maxOuterClauses, int maxClauses)
      throws IOException {
    int ret = 0;
    long nMatches = 0;

    for (int i = 0; i < iter; i++) {
      int oClauses = r.nextInt(maxOuterClauses - 1) + 2;
      BooleanQuery oq = new BooleanQuery();
      BitSet result = null;

      for (int o = 0; o < oClauses; o++) {

        int nClauses = r.nextInt(maxClauses - 1) + 2; // min 2 clauses
        BooleanQuery bq = new BooleanQuery();
        for (int j = 0; j < nClauses; j++) {
          result = addClause(bq, result);
        }

        oq.add(bq, BooleanClause.Occur.MUST);
      } // outer

      CountingHitCollector hc =
          validate ? new MatchingHitCollector(result) : new CountingHitCollector();
      s.search(oq, hc);
      nMatches += hc.getCount();
      ret += hc.getSum();
      if (validate) assertEquals(result.cardinality(), hc.getCount());
      // System.out.println(hc.getCount());
    }
    System.out.println("Average number of matches=" + (nMatches / iter));
    return ret;
  }
 @Override
 public boolean shouldCache(Query query, LeafReaderContext context) throws IOException {
   if (query instanceof MatchAllDocsQuery
       // MatchNoDocsQuery currently rewrites to a BooleanQuery,
       // but who knows, it might get its own Weight one day
       || query instanceof MatchNoDocsQuery) {
     return false;
   }
   if (query instanceof BooleanQuery) {
     BooleanQuery bq = (BooleanQuery) query;
     if (bq.clauses().isEmpty()) {
       return false;
     }
   }
   if (query instanceof DisjunctionMaxQuery) {
     DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) query;
     if (dmq.getDisjuncts().isEmpty()) {
       return false;
     }
   }
   if (segmentPolicy.shouldCache(query, context) == false) {
     return false;
   }
   final int frequency = frequency(query);
   final int minFrequency = minFrequencyToCache(query);
   return frequency >= minFrequency;
 }
  @Override
  public Weight createWeight(IndexSearcher searcher) throws IOException {
    IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader());
    indexSearcher.setSimilarity(new SimpleSimilarity());

    BooleanQuery booleanQuery = new BooleanQuery();
    for (int h : hashes) {
      booleanQuery.add(
          new BooleanClause(
              new TermQuery(new Term(hashFieldName, Integer.toString(h))),
              BooleanClause.Occur.SHOULD));
    }
    TopDocs topDocs = indexSearcher.search(booleanQuery, maxResult);

    if (topDocs.scoreDocs.length == 0) { // no result find
      return Queries.newMatchNoDocsQuery().createWeight(searcher);
    }

    BitSet bitSet = new BitSet(topDocs.scoreDocs.length);
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
      bitSet.set(scoreDoc.doc);
    }

    return new ImageHashLimitWeight(searcher, bitSet);
  }
Beispiel #9
0
  public int doNestedTermConjunctions(
      IndexSearcher s, int termsInIndex, int maxOuterClauses, int maxClauses, int iter)
      throws IOException {
    int ret = 0;
    long nMatches = 0;
    for (int i = 0; i < iter; i++) {
      int oClauses = r.nextInt(maxOuterClauses - 1) + 2;
      BooleanQuery oq = new BooleanQuery();
      for (int o = 0; o < oClauses; o++) {

        int nClauses = r.nextInt(maxClauses - 1) + 2; // min 2 clauses
        BooleanQuery bq = new BooleanQuery();
        BitSet termflag = new BitSet(termsInIndex);
        for (int j = 0; j < nClauses; j++) {
          int tnum;
          // don't pick same clause twice
          tnum = r.nextInt(termsInIndex);
          if (termflag.get(tnum)) tnum = termflag.nextClearBit(tnum);
          if (tnum < 0 || tnum >= 25) tnum = termflag.nextClearBit(0);
          termflag.set(tnum);
          Query tq = new TermQuery(terms[tnum]);
          bq.add(tq, BooleanClause.Occur.MUST);
        } // inner

        oq.add(bq, BooleanClause.Occur.MUST);
      } // outer

      CountingHitCollector hc = new CountingHitCollector();
      s.search(oq, hc);
      nMatches += hc.getCount();
      ret += hc.getSum();
    }
    System.out.println("Average number of matches=" + (nMatches / iter));
    return ret;
  }
  public void testMoreThan32ProhibitedClauses() throws Exception {
    final Directory d = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), d);
    Document doc = new Document();
    doc.add(
        new TextField(
            "field",
            "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33",
            Field.Store.NO));
    w.addDocument(doc);
    doc = new Document();
    doc.add(new TextField("field", "33", Field.Store.NO));
    w.addDocument(doc);
    final IndexReader r = w.getReader();
    w.close();
    final IndexSearcher s = newSearcher(r);

    final BooleanQuery q = new BooleanQuery();
    for (int term = 0; term < 33; term++) {
      q.add(
          new BooleanClause(
              new TermQuery(new Term("field", "" + term)), BooleanClause.Occur.MUST_NOT));
    }
    q.add(new BooleanClause(new TermQuery(new Term("field", "33")), BooleanClause.Occur.SHOULD));

    final int[] count = new int[1];
    s.search(
        q,
        new Collector() {
          private Scorer scorer;

          @Override
          public void setScorer(Scorer scorer) {
            // Make sure we got BooleanScorer:
            this.scorer = scorer;
            assertEquals(
                "Scorer is implemented by wrong class",
                BooleanScorer.class.getName() + "$BucketScorer",
                scorer.getClass().getName());
          }

          @Override
          public void collect(int doc) {
            count[0]++;
          }

          @Override
          public void setNextReader(AtomicReaderContext context) {}

          @Override
          public boolean acceptsDocsOutOfOrder() {
            return true;
          }
        });

    assertEquals(1, count[0]);

    r.close();
    d.close();
  }
Beispiel #11
0
 /**
  * Expert: called when re-writing queries under MultiSearcher.
  *
  * <p>Create a single query suitable for use by all subsearchers (in 1-1 correspondence with
  * queries). This is an optimization of the OR of all queries. We handle the common optimization
  * cases of equal queries and overlapping clauses of boolean OR queries (as generated by
  * MultiTermQuery.rewrite()). Be careful overriding this method as queries[0] determines which
  * method will be called and is not necessarily of the same type as the other queries.
  */
 public Query combine(Query[] queries) {
   HashSet<Query> uniques = new HashSet<Query>();
   for (int i = 0; i < queries.length; i++) {
     Query query = queries[i];
     BooleanClause[] clauses = null;
     // check if we can split the query into clauses
     boolean splittable = (query instanceof BooleanQuery);
     if (splittable) {
       BooleanQuery bq = (BooleanQuery) query;
       splittable = bq.isCoordDisabled();
       clauses = bq.getClauses();
       for (int j = 0; splittable && j < clauses.length; j++) {
         splittable = (clauses[j].getOccur() == BooleanClause.Occur.SHOULD);
       }
     }
     if (splittable) {
       for (int j = 0; j < clauses.length; j++) {
         uniques.add(clauses[j].getQuery());
       }
     } else {
       uniques.add(query);
     }
   }
   // optimization: if we have just one query, just return it
   if (uniques.size() == 1) {
     return uniques.iterator().next();
   }
   BooleanQuery result = new BooleanQuery(true);
   for (final Query query : uniques) result.add(query, BooleanClause.Occur.SHOULD);
   return result;
 }
  public void testMethod() throws Exception {
    Directory directory = newDirectory();

    String[] values = new String[] {"1", "2", "3", "4"};

    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
    for (int i = 0; i < values.length; i++) {
      Document doc = new Document();
      doc.add(newStringField(FIELD, values[i], Field.Store.YES));
      writer.addDocument(doc);
    }
    IndexReader ir = writer.getReader();
    writer.close();

    BooleanQuery booleanQuery1 = new BooleanQuery();
    booleanQuery1.add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
    booleanQuery1.add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);

    BooleanQuery query = new BooleanQuery();
    query.add(booleanQuery1, BooleanClause.Occur.MUST);
    query.add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);

    IndexSearcher indexSearcher = newSearcher(ir);
    ScoreDoc[] hits = indexSearcher.search(query, null, 1000).scoreDocs;
    assertEquals("Number of matched documents", 2, hits.length);
    ir.close();
    directory.close();
  }
 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;
 }
 /** test iterator returns NO_MORE_DOCS */
 public void testEmpty2() throws Exception {
   BooleanQuery query = new BooleanQuery();
   query.add(new TermQuery(new Term("id", "0")), BooleanClause.Occur.MUST);
   query.add(new TermQuery(new Term("id", "0")), BooleanClause.Occur.MUST_NOT);
   Filter expected = new QueryWrapperFilter(query);
   Filter actual = new CachingWrapperFilter(expected);
   assertFilterEquals(expected, actual);
 }
  public void testBQ11() throws Exception {
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
    TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
    boostedQuery.setBoost(1000);
    query.add(boostedQuery, BooleanClause.Occur.SHOULD);

    qtest(query, new int[] {0, 1, 2, 3});
  }
  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);
  }
 @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;
 }
 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;
 }
  public void testAllOptional() throws Exception {

    BooleanQuery q = new BooleanQuery();
    for (int i = 1; i <= 4; i++) {
      q.add(new TermQuery(new Term("data", "" + i)), BooleanClause.Occur.SHOULD); // false, false);
    }
    q.setMinimumNumberShouldMatch(2); // match at least two of 4
    verifyNrHits(q, 2);
  }
  @Test
  public void testRandomQueries() throws Exception {
    String[] vals = {"w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz"};

    int tot = 0;

    BooleanQuery q1 = null;
    try {

      // increase number of iterations for more complete testing
      int num = atLeast(10);
      for (int i = 0; i < num; i++) {
        int level = random.nextInt(3);
        q1 =
            randBoolQuery(
                new Random(random.nextLong()), random.nextBoolean(), level, field, vals, null);

        // Can't sort by relevance since floating point numbers may not quite
        // match up.
        Sort sort = Sort.INDEXORDER;

        QueryUtils.check(random, q1, searcher);
        final Similarity oldSim = searcher.getSimilarity();
        try {
          searcher.setSimilarity(new FunkySimilarity());
          QueryUtils.check(random, q1, searcher);
        } finally {
          searcher.setSimilarity(oldSim);
        }

        TopFieldCollector collector = TopFieldCollector.create(sort, 1000, false, true, true, true);

        searcher.search(q1, null, collector);
        ScoreDoc[] hits1 = collector.topDocs().scoreDocs;

        collector = TopFieldCollector.create(sort, 1000, false, true, true, false);

        searcher.search(q1, null, collector);
        ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
        tot += hits2.length;
        CheckHits.checkEqual(q1, hits1, hits2);

        BooleanQuery q3 = new BooleanQuery();
        q3.add(q1, BooleanClause.Occur.SHOULD);
        q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD);
        TopDocs hits4 = bigSearcher.search(q3, 1);
        assertEquals(mulFactor * collector.totalHits + NUM_EXTRA_DOCS / 2, hits4.totalHits);
      }

    } catch (Exception e) {
      // For easier debugging
      System.out.println("failed query: " + q1);
      throw e;
    }

    // System.out.println("Total hits:"+tot);
  }
  public void testNoOptionalButMin2() throws Exception {

    /* one required, no optional */
    BooleanQuery q = new BooleanQuery();
    q.add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST); // true,  false);

    q.setMinimumNumberShouldMatch(1); // 1 of 0 optional

    verifyNrHits(q, 0);
  }
 /**
  * Merges <code>termClaimsDescriptionAbstractTitleQueries</code> into a single query. In the
  * future this method should probably be in <code>Query</code> class. This is akward way of doing
  * it; but only merge queries method that is available is mergeBooleanQueries; so actually have to
  * make a string termClaimsDescriptionAbstractTitle1^boost1,
  * termClaimsDescriptionAbstractTitle2^boost and then parse it into a query
  *
  * @param termQueries - to merge
  * @param maxTerms
  * @return query created from termClaimsDescriptionAbstractTitleQueries including boost parameters
  * @throws org.apache.lucene.queryparser.classic.ParseException
  */
 public Query mergeQueries(List<TermQuery> termQueries, int maxTerms) throws ParseException {
   BooleanQuery query = new BooleanQuery();
   // Select only the maxTerms number of terms
   int termCount = Math.min(termQueries.size(), maxTerms);
   for (int i = 0; i < termCount; i++) {
     TermQuery termQuery = termQueries.get(i);
     query.add(termQuery, BooleanClause.Occur.SHOULD);
   }
   return query;
 }
 @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 void doTest(Random random, PrintWriter out, boolean useCompoundFiles, int MAX_DOCS)
      throws Exception {
    Directory directory = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random);
    IndexWriterConfig conf = newIndexWriterConfig(analyzer);
    final MergePolicy mp = conf.getMergePolicy();
    mp.setNoCFSRatio(useCompoundFiles ? 1.0 : 0.0);
    IndexWriter writer = new IndexWriter(directory, conf);
    if (VERBOSE) {
      System.out.println("TEST: now build index MAX_DOCS=" + MAX_DOCS);
    }

    for (int j = 0; j < MAX_DOCS; j++) {
      Document d = new Document();
      d.add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES));
      d.add(newTextField(ID_FIELD, Integer.toString(j), Field.Store.YES));
      writer.addDocument(d);
    }
    writer.close();

    // try a search without OR
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = newSearcher(reader);

    Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));
    out.println("Query: " + query.toString(PRIORITY_FIELD));
    if (VERBOSE) {
      System.out.println("TEST: search query=" + query);
    }

    final Sort sort = new Sort(SortField.FIELD_SCORE, new SortField(ID_FIELD, SortField.Type.INT));

    ScoreDoc[] hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);

    // try a new search with OR
    searcher = newSearcher(reader);
    hits = null;

    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(
        new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur.SHOULD);
    booleanQuery.add(
        new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur.SHOULD);
    out.println("Query: " + booleanQuery.toString(PRIORITY_FIELD));

    hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);

    reader.close();
    directory.close();
  }
  public void testDMQ7() throws Exception {
    DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);

    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
    booleanQuery.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);

    q.add(booleanQuery);
    q.add(new TermQuery(new Term(FIELD, "w2")));
    qtest(q, new int[] {0, 1, 2, 3});
  }
 @Override
 public Query construct(IndexEnvironment env, Map<String, String[]> querySource)
     throws ParseException {
   if ("1".equals(StringTools.arrayToString(querySource.get("queryversion"), ""))) {
     // preserving old stuff:
     // 1. all lucene special chars to be quoted
     // 2. if "wholewords" is "on" or "true" -> don't add *_*, otherwise add *_*
     BooleanQuery result = new BooleanQuery();
     String wholeWords = StringTools.arrayToString(querySource.get("wholewords"), "");
     boolean useWildcards = !(null != wholeWords && StringTools.stringToBoolean(wholeWords));
     for (Map.Entry<String, String[]> queryItem : querySource.entrySet()) {
       String field = queryItem.getKey();
       if (env.fields.containsKey(field) && queryItem.getValue().length > 0) {
         for (String value : queryItem.getValue()) {
           if (null != value) {
             value = value.trim().toLowerCase();
             if (0 != value.length()) {
               if ("keywords".equals(field) && ACCESSION_REGEX.test(value)) {
                 result.add(new TermQuery(new Term("accession", value)), BooleanClause.Occur.MUST);
               } else if ("keywords".equals(field)
                   && '"' == value.charAt(0)
                   && '"' == value.charAt(value.length() - 1)) {
                 value = value.substring(1, value.length() - 1);
                 PhraseQuery q = new PhraseQuery();
                 String[] tokens = value.split("\\s+");
                 for (String token : tokens) {
                   q.add(new Term(field, token));
                 }
                 result.add(q, BooleanClause.Occur.MUST);
               } else {
                 String[] tokens = value.split("\\s+");
                 for (String token : tokens) {
                   // we use wildcards for keywords depending on "wholewords" switch,
                   // *ALWAYS* for other fields, *NEVER* for user id and accession or boolean
                   // fields
                   Query q =
                       !"boolean".equals(env.fields.get(field).type)
                               && !" userid  accession ".contains(" " + field + " ")
                               && (useWildcards || (!" keywords ".contains(" " + field + " ")))
                           ? new WildcardQuery(new Term(field, "*" + token + "*"))
                           : new TermQuery(new Term(field, token));
                   result.add(q, BooleanClause.Occur.MUST);
                 }
               }
             }
           }
         }
       }
     }
     return result;
   } else {
     return super.construct(env, querySource);
   }
 }
 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);
 }
  public void testBQ20() throws Exception {
    BooleanQuery q = new BooleanQuery();
    q.setMinimumNumberShouldMatch(2);
    q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
    q.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
    q.add(new TermQuery(new Term(FIELD, "zz")), BooleanClause.Occur.SHOULD);
    q.add(new TermQuery(new Term(FIELD, "w5")), BooleanClause.Occur.SHOULD);
    q.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.SHOULD);

    qtest(q, new int[] {0, 3});
  }
      @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;
 }