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;
    }
  }
Ejemplo n.º 2
0
 /**
  * Perform searching for word by word content.
  *
  * @param content the content used to search.
  * @return a list of found documents.
  * @throws java.io.IOException if the path to the lucene index is incorrect.
  */
 public static List<Document> performSearchByContent(String content) throws IOException {
   List<Document> foundDocs =
       performSearch(QueryUtils.buildPhraseQuery(content), LuceneUtils.getLuceneSearcher());
   if (CollectionUtils.isEmpty(foundDocs)) {
     foundDocs =
         performSearch(QueryUtils.buildFuzzyQuery(content), LuceneUtils.getLuceneSearcher());
   }
   return foundDocs;
 }
Ejemplo n.º 3
0
  @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);
  }
Ejemplo n.º 4
0
 protected static void logPoliciesUsingRemovedTopologyObjs(
     String operation,
     String topologyType,
     Collection<Integer> deletedNodes,
     final String constraintName,
     Connection c)
     throws SQLException {
   String query =
       "select sets.customer_id, p.policy_name, pc.policy_id, sets.user_set_id, sets.constraint_value "
           + " from policies p, policy_criteria pc, dat_saved_user_sets sets "
           + " where sets.constraint_name = "
           + QueryUtils.literal(constraintName)
           + " and "
           + QueryUtils.dbCast("sets.constraint_value", QueryUtils.CastType.INTEGER)
           + " in "
           + QueryUtils.literal(deletedNodes)
           + " and sets.user_set_id = pc.userset_id and pc.policy_id = p.policy_id";
   s_logger.debug(query);
   Statement s = null;
   ResultSet rs = null;
   try {
     s = c.createStatement();
     rs = s.executeQuery(query);
     while (rs.next()) {
       int custID = rs.getInt(1);
       String policyName = rs.getString(2);
       int policyID = rs.getInt(3);
       int userSetID = rs.getInt(4);
       int nodeID = rs.getInt(5);
       StringBuilder bld =
           new StringBuilder(operation)
               .append(" ")
               .append(topologyType)
               .append(" ")
               .append(nodeID)
               .append(" which is refered to by userset ")
               .append(userSetID)
               .append(" in policy ")
               .append(policyName)
               .append(" with id ")
               .append(policyID)
               .append(" for customer ")
               .append(custID);
       s_logger.warn(bld.toString());
     }
   } finally {
     if (s != null) s.close();
     if (rs != null) rs.close();
   }
 }
 private Region getRegionFromPath(String imports, String fromClause)
     throws RegionNotFoundException {
   QCompiler compiler = new QCompiler();
   if (imports != null) {
     compiler.compileImports(imports);
   }
   List list = compiler.compileFromClause(fromClause);
   CompiledValue cv =
       QueryUtils.obtainTheBottomMostCompiledValue(
           ((CompiledIteratorDef) list.get(0)).getCollectionExpr());
   String regionPath = null;
   if (cv.getType() == OQLLexerTokenTypes.RegionPath) {
     regionPath = ((CompiledRegion) cv).getRegionPath();
   } else {
     throw new RegionNotFoundException(
         LocalizedStrings
             .DefaultQueryService_DEFAULTQUERYSERVICECREATEINDEXFIRST_ITERATOR_OF_INDEX_FROM_CLAUSE_DOES_NOT_EVALUATE_TO_A_REGION_PATH_THE_FROM_CLAUSE_USED_FOR_INDEX_CREATION_IS_0
             .toLocalizedString(fromClause));
   }
   Region region = cache.getRegion(regionPath);
   if (region == null) {
     throw new RegionNotFoundException(
         LocalizedStrings.DefaultQueryService_REGION_0_NOT_FOUND_FROM_1.toLocalizedString(
             new Object[] {regionPath, fromClause}));
   }
   return region;
 }
  @Test
  public void testParsingWhereColumnsCountQuery() throws ParseException {
    System.out.println("testGetWhereColumns");
    /*
     * ZQuery has error parsing columns with quote charcters arounds
     * columns. e.g Select COUNT() FROM votes_train WHERE
     * Class='democrat' AND handicapped-infants='n'; AND
     * `handicapped-infants`='n';"; Removing the back quote is causing
     * theno. of columns clculation wrong
     */

    String input =
        "Select COUNT(*) FROM votes_train WHERE Class='democrat'   AND handicapped-infants='n';";

    ZqlParser parser = new ZqlParser();
    ByteArrayInputStream inpStream = new ByteArrayInputStream(input.getBytes());
    parser.initParser(inpStream);
    ZQuery query = (ZQuery) parser.readStatements().get(0);

    Vector<String> whereColumns = QueryUtils.getWhereColumns(query);

    System.out.println("no of columns=" + whereColumns.size());
    System.out.println("******** Where Columns ***********");
    for (int i = 0; i < whereColumns.size(); i++) {
      System.out.println(whereColumns.get(i));
    }
    System.out.println("**********************************");
  }
  @Test
  public void testGetWhereColumns() throws ParseException {
    System.out.println("testGetWhereColumns");
    String input =
        "select COUNT(*) from EMPLOYEETABLE where ((EMPLOYEETABLE.key2 = 's4') OR (EMPLOYEETABLE.key2 = 's1') OR (EMPLOYEETABLE.key2 = 's2') OR (EMPLOYEETABLE.key2 = 's3'));";
    input =
        "select COUNT(*) from EMPLOYEETABLE where (EMPLOYEETABLE.key2 IN ('s1','s2','s3','s4'));";

    // String input = "select firstname, position from EMPLOYEETABLE where (position='manager' AND
    // timehere > 1) OR (firstname='Steve' AND key > 45);";
    // String input = "Select COUNT(*) FROM votes_train WHERE Class='democrat'   AND
    // handicapped-infants='n';";

    // String input = "select COUNT(*) from EMPLOYEETABLE where position2 > 'redshirt2' AND
    // neeraj='koul';";
    ZqlParser parser = new ZqlParser();
    ByteArrayInputStream inpStream = new ByteArrayInputStream(input.getBytes());
    parser.initParser(inpStream);
    ZQuery query = (ZQuery) parser.readStatements().get(0);

    Vector<String> whereColumns = QueryUtils.getWhereColumns(query);

    System.out.println("******** Where Columns ***********");
    for (int i = 0; i < whereColumns.size(); i++) {
      System.out.println(whereColumns.get(i));
    }
    System.out.println("**********************************");
  }
  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;
    }
  }
Ejemplo n.º 9
0
 @Test
 public void testBasicQuerySanities() {
   Query childQuery = new TermQuery(new Term("field", "value"));
   ScoreType scoreType = ScoreType.values()[random().nextInt(ScoreType.values().length)];
   ParentFieldMapper parentFieldMapper =
       SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
   ParentChildIndexFieldData parentChildIndexFieldData =
       SearchContext.current().fieldData().getForField(parentFieldMapper);
   BitDocIdSetFilter parentFilter =
       wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "parent")));
   int minChildren = random().nextInt(10);
   int maxChildren = scaledRandomIntBetween(minChildren, 10);
   Query query =
       new ChildrenQuery(
           parentChildIndexFieldData,
           "parent",
           "child",
           parentFilter,
           childQuery,
           scoreType,
           minChildren,
           maxChildren,
           12,
           wrapWithBitSetFilter(NonNestedDocsFilter.INSTANCE));
   QueryUtils.check(query);
 }
 public void verifyNrHits(Query q, int expected) throws Exception {
   ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
   if (expected != h.length) {
     printHits(getName(), h, s);
   }
   assertEquals("result count", expected, h.length);
   QueryUtils.check(q, s);
 }
Ejemplo n.º 11
0
  /**
   * @param num
   * @param cardType
   * @return
   */
  public static boolean isEligibleForVerification(String num, String cardType) {

    if (StringUtils.isNotBlank(num) && StringUtils.isNotBlank(cardType)) {
      if (!QueryUtils.isVerifiedRecently(num, cardType)) {
        return true;
      }
    }
    return false;
  }
  public MultigetSliceQueryImpl(
      Keyspace ks, ColumnFamily<K, T, N> cf, List<NamedColumn<K, T, N, ?>> columns) {
    N[] columnNames = QueryUtils.getColumnNamesUnresolved(cf, columns);

    wrappedQuery =
        HFactory.createMultigetSliceQuery(
                ks, cf.getKeySerializer(), cf.getColumnNameSerializer(), DummySerializer.get())
            .setColumnFamily(cf.getName())
            .setColumnNames(columnNames);
  }
 public LabelQueryBuilder(
     Context paramContext, SQLiteDatabase paramSQLiteDatabase, String[] paramArrayOfString) {
   mDb = paramSQLiteDatabase;
   mProjection = paramArrayOfString;
   mBuilder.setTables("labels");
   mBuilder.setProjectionMap(MailEngine.LABEL_PROJECTION_MAP);
   mArgs =
       Lists.newArrayList(
           QueryUtils.getQueryBindArgs(paramContext, paramArrayOfString, "name", new String[0]));
 }
  public void testSkipToFirsttimeHit() throws IOException {
    final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
    dq.add(tq("dek", "albino"));
    dq.add(tq("dek", "DOES_NOT_EXIST"));

    QueryUtils.check(dq, s);

    final Weight dw = dq.weight(s);
    final Scorer ds = dw.scorer(s.getIndexReader(), true, false);
    assertTrue("firsttime skipTo found no match", ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals("found wrong docid", "d4", r.document(ds.docID()).get("id"));
  }
  public void testSkipToFirsttimeMiss() throws IOException {
    final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
    dq.add(tq("id", "d1"));
    dq.add(tq("dek", "DOES_NOT_EXIST"));

    QueryUtils.check(dq, s);

    final Weight dw = dq.weight(s);
    final Scorer ds = dw.scorer(s.getIndexReader(), true, false);
    final boolean skipOk = ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS;
    if (skipOk) {
      fail("firsttime skipTo found a match? ... " + r.document(ds.docID()).get("id"));
    }
  }
  public void testBooleanOptionalWithTiebreaker() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
      q1.add(tq("hed", "albino"));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
      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 score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      float score3 = h[3].score;

      String doc0 = s.doc(h[0].doc).get("id");
      String doc1 = s.doc(h[1].doc).get("id");
      String doc2 = s.doc(h[2].doc).get("id");
      String doc3 = s.doc(h[3].doc).get("id");

      assertTrue("doc0 should be d2 or d4: " + doc0, doc0.equals("d2") || doc0.equals("d4"));
      assertTrue("doc1 should be d2 or d4: " + doc0, doc1.equals("d2") || doc1.equals("d4"));
      assertEquals("score0 and score1 should match", score0, score1, SCORE_COMP_THRESH);
      assertEquals("wrong third", "d3", doc2);
      assertTrue(
          "d3 does not have worse score then d2 and d4: " + score1 + " >? " + score2,
          score1 > score2);

      assertEquals("wrong fourth", "d1", doc3);
      assertTrue(
          "d1 does not have worse score then d3: " + score2 + " >? " + score3, score2 > score3);

    } catch (Error e) {
      printHits("testBooleanOptionalWithTiebreaker", h, s);
      throw e;
    }
  }
  public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception {

    BooleanQuery q = new BooleanQuery();
    {
      DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
      q1.add(tq("hed", "albino", 1.5f));
      q1.add(tq("dek", "albino"));
      q.add(q1, BooleanClause.Occur.SHOULD); // false,false);
    }
    {
      DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
      q2.add(tq("hed", "elephant", 1.5f));
      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 score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      float score3 = h[3].score;

      String doc0 = s.doc(h[0].doc).get("id");
      String doc1 = s.doc(h[1].doc).get("id");
      String doc2 = s.doc(h[2].doc).get("id");
      String doc3 = s.doc(h[3].doc).get("id");

      assertEquals("doc0 should be d4: ", "d4", doc0);
      assertEquals("doc1 should be d3: ", "d3", doc1);
      assertEquals("doc2 should be d2: ", "d2", doc2);
      assertEquals("doc3 should be d1: ", "d1", doc3);

      assertTrue(
          "d4 does not have a better score then d3: " + score0 + " >? " + score1, score0 > score1);
      assertTrue(
          "d3 does not have a better score then d2: " + score1 + " >? " + score2, score1 > score2);
      assertTrue(
          "d3 does not have a better score then d1: " + score2 + " >? " + score3, score2 > score3);

    } catch (Error e) {
      printHits("testBooleanOptionalWithTiebreakerAndBoost", h, s);
      throw e;
    }
  }
  public MultigetSliceQueryImpl(
      Keyspace ks,
      ColumnFamily<K, T, N> cf,
      ColumnRange<K, T, N, ?> columnRange,
      N start,
      N finish,
      boolean reversed,
      int count) {
    QueryUtils.checkColumnBelongsToColumnFamily(cf, columnRange);

    wrappedQuery =
        HFactory.createMultigetSliceQuery(
                ks, cf.getKeySerializer(), cf.getColumnNameSerializer(), DummySerializer.get())
            .setColumnFamily(cf.getName())
            .setRange(start, finish, reversed, count);
  }
Ejemplo n.º 19
0
  private static Set<Integer> purgeDeletedSites(Connection c) throws SQLException {
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // find server objects that need to be purged
      List<Integer> purgeSiteIds = new ArrayList<Integer>();
      Set<Integer> custIds = new HashSet<Integer>();
      String query =
          "select site.site_id, src.customer_id from dat_dirsync_sources src, dat_customer_sites site where site.is_deleted = true and current_timestamp > site.purge_time and src.source_id = site.source_id";
      stmt = c.createStatement();
      s_logger.debug(query);
      rs = stmt.executeQuery(query);
      while (rs.next()) {
        purgeSiteIds.add(rs.getInt(1));
        custIds.add(rs.getInt(2));
      }

      if (CollectionsUtils.isNullOrEmpty(purgeSiteIds)) {
        return null;
      }

      int totalDeletes = 0;
      for (List<Integer> ids : ChunkedListIterator.iterable(purgeSiteIds, BATCH_SIZE)) {
        logPoliciesUsingRemovedTopologyObjs(
            "Purging ", "site", ids, IUserManager.PROP_ROUTING_GROUP_ID, c);

        // purge servers
        String idList = QueryUtils.literal(ids);
        query = "delete from dat_customer_sites where site_id in " + idList;
        s_logger.debug(query);
        totalDeletes += stmt.executeUpdate(query);

        // don't purge constraints because the scope of the constraints will expand. Worst case,
        // when all constraints are deleted, a saved user set will have global scope and this
        // is definitely not what the customer wants.
      }
      s_logger.info("Purged " + totalDeletes + " sites for " + custIds.size() + " customers");
      return custIds;
    } finally {
      if (stmt != null) {
        stmt.close();
      }
      if (rs != null) {
        rs.close();
      }
    }
  }
  public SuperSliceQueryImpl(
      Keyspace ks,
      SupercolumnFamily<K, T, SN, N> scf,
      K key,
      List<NamedSupercolumn<K, T, SN, N>> supercolumns) {
    SN[] supercolumnNames = QueryUtils.getSupercolumnNamesUnresolved(scf, supercolumns);

    wrappedQuery =
        HFactory.createSuperSliceQuery(
                ks,
                scf.getKeySerializer(),
                scf.getSupercolumnNameSerializer(),
                scf.getSubcolumnNameSerializer(),
                DummySerializer.get())
            .setColumnFamily(scf.getName())
            .setColumnNames(supercolumnNames)
            .setKey(key);
  }
  public void testSimpleEqualScores2() throws Exception {

    DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
    q.add(tq("dek", "albino"));
    q.add(tq("dek", "elephant"));
    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("testSimpleEqualScores2", h, s);
      throw e;
    }
  }
  public SuperSliceQueryImpl(
      Keyspace ks,
      SupercolumnFamily<K, T, SN, N> scf,
      K key,
      SupercolumnRange<K, T, SN, N> supercolumnRange,
      SN start,
      SN finish,
      boolean reversed,
      int count) {
    QueryUtils.checkSupercolumnBelongsToFamily(scf, supercolumnRange);

    wrappedQuery =
        HFactory.createSuperSliceQuery(
                ks,
                scf.getKeySerializer(),
                scf.getSupercolumnNameSerializer(),
                scf.getSubcolumnNameSerializer(),
                DummySerializer.get())
            .setColumnFamily(scf.getName())
            .setRange(start, finish, reversed, count)
            .setKey(key);
  }
  public void testSimpleTiebreaker() throws Exception {

    DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.01f);
    q.add(tq("dek", "albino"));
    q.add(tq("dek", "elephant"));
    QueryUtils.check(q, s);

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

    try {
      assertEquals("3 docs should match " + q.toString(), 3, h.length);
      assertEquals("wrong first", "d2", s.doc(h[0].doc).get("id"));
      float score0 = h[0].score;
      float score1 = h[1].score;
      float score2 = h[2].score;
      assertTrue(
          "d2 does not have better score then others: " + score0 + " >? " + score1,
          score0 > score1);
      assertEquals("d4 and d1 don't have equal scores", score1, score2, SCORE_COMP_THRESH);
    } catch (Error e) {
      printHits("testSimpleTiebreaker", h, s);
      throw e;
    }
  }
  public void testRandomQueries() throws Exception {
    final Random rnd = newRandom();

    String field = "data";
    String[] vals = {"1", "2", "3", "4", "5", "6", "A", "Z", "B", "Y", "Z", "X", "foo"};
    int maxLev = 4;

    // callback object to set a random setMinimumNumberShouldMatch
    TestBoolean2.Callback minNrCB =
        new TestBoolean2.Callback() {
          public void postCreate(BooleanQuery q) {
            BooleanClause[] c = q.getClauses();
            int opt = 0;
            for (int i = 0; i < c.length; i++) {
              if (c[i].getOccur() == BooleanClause.Occur.SHOULD) opt++;
            }
            q.setMinimumNumberShouldMatch(rnd.nextInt(opt + 2));
          }
        };

    // increase number of iterations for more complete testing
    for (int i = 0; i < 50; i++) {
      int lev = rnd.nextInt(maxLev);
      final long seed = rnd.nextLong();
      BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
      // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB);
      BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);
      // only set minimumNumberShouldMatch on the top level query since setting
      // at a lower level can change the score.
      minNrCB.postCreate(q2);

      // Can't use Hits because normalized scores will mess things
      // up.  The non-sorting version of search() that returns TopDocs
      // will not normalize scores.
      TopDocs top1 = s.search(q1, null, 100);
      TopDocs top2 = s.search(q2, null, 100);
      if (i < 100) {
        QueryUtils.check(q1, s);
        QueryUtils.check(q2, s);
      }
      // The constrained query
      // should be a superset to the unconstrained query.
      if (top2.totalHits > top1.totalHits) {
        TestCase.fail(
            "Constrained results not a subset:\n"
                + CheckHits.topdocsString(top1, 0, 0)
                + CheckHits.topdocsString(top2, 0, 0)
                + "for query:"
                + q2.toString());
      }

      for (int hit = 0; hit < top2.totalHits; hit++) {
        int id = top2.scoreDocs[hit].doc;
        float score = top2.scoreDocs[hit].score;
        boolean found = false;
        // find this doc in other hits
        for (int other = 0; other < top1.totalHits; other++) {
          if (top1.scoreDocs[other].doc == id) {
            found = true;
            float otherScore = top1.scoreDocs[other].score;
            // check if scores match
            if (Math.abs(otherScore - score) > 1.0e-6f) {
              TestCase.fail(
                  "Doc "
                      + id
                      + " scores don't match\n"
                      + CheckHits.topdocsString(top1, 0, 0)
                      + CheckHits.topdocsString(top2, 0, 0)
                      + "for query:"
                      + q2.toString());
            }
          }
        }

        // check if subset
        if (!found)
          TestCase.fail(
              "Doc "
                  + id
                  + " not found\n"
                  + CheckHits.topdocsString(top1, 0, 0)
                  + CheckHits.topdocsString(top2, 0, 0)
                  + "for query:"
                  + q2.toString());
      }
    }
    // System.out.println("Total hits:"+tot);
  }
  private List getNextSequenceImpl(TypeDescriptor typeDescriptor, Session session) {
    Field[] pkFields = typeDescriptor.getPkFields();

    Assert.condition(
        1 == pkFields.length,
        "Automatic PK values are only supported for types with a single PK field.");

    String createPKStmt =
        dbDescriptor.getCreatePKStatement(
            sqlUtils.getSchemaName(), typeDescriptor.getPkSequence(), this.sequenceBatchSize);

    Field field = pkFields[0];
    if (session.isUsingPreparedStatements(typeDescriptor.getType())) {
      PreparedStatement pkStatement = null;
      try {
        Connection connection = session.getConnection();
        PreparedStatement result;
        try {
          result = connection.prepareStatement(createPKStmt);
        } catch (SQLException x) {
          throw new InternalException(x);
        }
        pkStatement = result;
        ResultSet pkQuery = pkStatement.executeQuery();
        List newIds = new LinkedList();
        while (pkQuery.next()) {
          newIds.add(
              DmlManager.getJavaValue(
                  field.getType(),
                  typeDescriptor.getPersistentField(field).getLength(),
                  pkQuery,
                  1,
                  true,
                  false));
        }
        return newIds;
      } catch (SQLException e) {
        throw new InternalException(e);
      } finally {
        QueryUtils.closeStatement(pkStatement);
      }
    } else {
      Statement pkStmt = null;
      try {
        Connection connection = session.getConnection();
        pkStmt = connection.createStatement();
        ResultSet pkQuery = pkStmt.executeQuery(createPKStmt);
        List newIds = new LinkedList();
        while (pkQuery.next()) {
          newIds.add(
              DmlManager.getJavaValue(
                  field.getType(),
                  typeDescriptor.getPersistentField(field).getLength(),
                  pkQuery,
                  1,
                  true,
                  false));
        }
        return newIds;
      } catch (SQLException e) {
        throw new InternalException(e);
      } finally {
        QueryUtils.closeStatement(pkStmt);
      }
    }
  }
Ejemplo n.º 26
0
  private static void deleteTopologyData(
      Connection c, int sourceID, Collection groupIDs, Collection serverIDs, Collection storeIDs)
      throws SQLException {
    PreparedStatement ps = null;
    int purgeInterval =
        ManagementContainer.getInstance()
            .getConfiguration()
            .getIntProperty(PURGE_DAY_INTERVAL_PROP, DEFAULT_PURGE_DAY_INTERVAL);
    List<Integer> deletedStores = null;
    List<Integer> deletedServers = null;
    List<Integer> deletedSites = null;

    try {
      // First determine what stores to delete
      String strQuery =
          "select store_id from dat_customer_stores where exists ( "
              + "  select * from dat_customer_servers svr "
              + "    where svr.server_id = dat_customer_stores.server_id and "
              + "      exists ( "
              + "        select * from dat_customer_sites s "
              + "        where s.site_id = svr.admin_group_id and source_id = ? "
              + "      ) "
              + "  ) and "
              + "  store_id not in "
              + QueryUtils.literal(storeIDs)
              + " and is_deleted = false";
      ps = c.prepareStatement(strQuery);

      ps.setInt(1, sourceID);
      ResultSet rs = ps.executeQuery();
      // Convert the result set to a list of store id's to be deleted
      while (rs.next()) {
        if (deletedStores == null) deletedStores = new ArrayList<Integer>();

        deletedStores.add(rs.getInt(1));
      }

      ps.close();

      if (deletedStores != null) // Check to see if we have anything to delete
      {
        strQuery =
            "update dat_customer_stores set is_deleted = true, purge_time = current_timestamp + '"
                + purgeInterval
                + " days'::interval"
                + "    where store_id in "
                + QueryUtils.literal(deletedStores);
        ps = c.prepareStatement(strQuery);
        ps.executeUpdate();
        ps.close();

        // Log what we marked for deletion
        logPoliciesUsingRemovedTopologyObjs(
            "Marking as deleted", "store", deletedStores, IUserManager.PROP_STORE_ID, c);
      }

      ps = null;

      // delete the servers
      // First determine what servers to delete
      strQuery =
          "select server_id from dat_customer_servers "
              + "where "
              + "  exists ( "
              + "    select * from dat_customer_sites s "
              + "    where s.site_id = dat_customer_servers.admin_group_id and source_id = ? "
              + "  ) and "
              + "  server_id not in "
              + QueryUtils.literal(serverIDs)
              + " and is_deleted = false";
      ps = c.prepareStatement(strQuery);

      ps.setInt(1, sourceID);
      rs = ps.executeQuery();
      // Convert the result set to a list of server id's to be deleted
      while (rs.next()) {
        if (deletedServers == null) deletedServers = new ArrayList<Integer>();

        deletedServers.add(rs.getInt(1));
      }

      ps.close();

      if (deletedServers != null) // Check to see if we have anything to delete
      {
        strQuery =
            "update dat_customer_servers set is_deleted = true, purge_time = current_timestamp + '"
                + purgeInterval
                + " days'::interval"
                + "    where server_id in "
                + QueryUtils.literal(deletedServers);
        ps = c.prepareStatement(strQuery);
        ps.executeUpdate();
        ps.close();

        // Log what we marked for deletion
        logPoliciesUsingRemovedTopologyObjs(
            "Marking as deleted", "server", deletedServers, IUserManager.PROP_SERVER_ID, c);
      }

      ps = null;

      // delete the sites
      // First determine what sites to delete
      strQuery =
          "select site_id from dat_customer_sites "
              + "where "
              + "  source_id = ? and is_deleted = false and "
              + "  site_id not in "
              + QueryUtils.literal(groupIDs);
      ps = c.prepareStatement(strQuery);

      ps.setInt(1, sourceID);
      rs = ps.executeQuery();
      // Convert the result set to a list of site id's to be deleted
      while (rs.next()) {
        if (deletedSites == null) deletedSites = new ArrayList<Integer>();

        deletedSites.add(rs.getInt(1));
      }

      ps.close();

      if (deletedSites != null) // Check to see if we have anything to delete
      {
        strQuery =
            "update dat_customer_sites set is_deleted = true, purge_time = current_timestamp + '"
                + purgeInterval
                + " days'::interval"
                + "    where site_id in "
                + QueryUtils.literal(deletedSites);
        ps = c.prepareStatement(strQuery);
        ps.executeUpdate();
        ps.close();

        // Log what we marked for deletion
        logPoliciesUsingRemovedTopologyObjs(
            "Marking as deleted", "site", deletedSites, IUserManager.PROP_ROUTING_GROUP_ID, c);
      }

      ps = null;
    } finally {
      if (ps != null) {
        ps.close();
      }
    }
  }
Ejemplo n.º 27
0
 /**
  * Perform searching for word by word content.
  *
  * @param content the content used to search.
  * @param luceneDirPath the lucene index path.
  * @return a list of found documents.
  * @throws java.io.IOException if the path to the lucene index is incorrect.
  */
 public static List<Document> performSearchByContent(String content, String luceneDirPath)
     throws IOException {
   String indexDirPath = luceneDirPath + PACKAGE_PATH;
   return performSearch(
       QueryUtils.buildPhraseQuery(content), LuceneUtils.getLuceneSearcher(indexDirPath));
 }
 @Test
 public void testEqualsAndHash() throws Exception {
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test1", 4, 10L, 20L, true, true));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test2", 4, 10L, 20L, false, true));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test3", 4, 10L, 20L, true, false));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test4", 4, 10L, 20L, false, false));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test5", 4, 10L, null, true, true));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test6", 4, null, 20L, true, true));
   QueryUtils.checkHashEquals(
       LegacyNumericRangeQuery.newLongRange("test7", 4, null, null, true, true));
   QueryUtils.checkEqual(
       LegacyNumericRangeQuery.newLongRange("test8", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newLongRange("test8", 4, 10L, 20L, true, true));
   QueryUtils.checkUnequal(
       LegacyNumericRangeQuery.newLongRange("test9", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newLongRange("test9", 8, 10L, 20L, true, true));
   QueryUtils.checkUnequal(
       LegacyNumericRangeQuery.newLongRange("test10a", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newLongRange("test10b", 4, 10L, 20L, true, true));
   QueryUtils.checkUnequal(
       LegacyNumericRangeQuery.newLongRange("test11", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newLongRange("test11", 4, 20L, 10L, true, true));
   QueryUtils.checkUnequal(
       LegacyNumericRangeQuery.newLongRange("test12", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newLongRange("test12", 4, 10L, 20L, false, true));
   QueryUtils.checkUnequal(
       LegacyNumericRangeQuery.newLongRange("test13", 4, 10L, 20L, true, true),
       LegacyNumericRangeQuery.newFloatRange("test13", 4, 10f, 20f, true, true));
   // difference to int range is tested in TestNumericRangeQuery32
 }