com.carrotsearch.randomizedtesting.generators.RandomPicks Java Examples

The following examples show how to use com.carrotsearch.randomizedtesting.generators.RandomPicks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestGeo3DDocValues.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void checkPointEncoding(final double latitude, final double longitude) {
  PlanetModel planetModel = RandomPicks.randomFrom(random(), new PlanetModel[] {PlanetModel.WGS84, PlanetModel.CLARKE_1866});
  final GeoPoint point = new GeoPoint(planetModel, Geo3DUtil.fromDegrees(latitude), Geo3DUtil.fromDegrees(longitude));
  long pointValue = planetModel.getDocValueEncoder().encodePoint(point);
  final double x = planetModel.getDocValueEncoder().decodeXValue(pointValue);
  final double y = planetModel.getDocValueEncoder().decodeYValue(pointValue);
  final double z = planetModel.getDocValueEncoder().decodeZValue(pointValue);
  final GeoPoint pointR = new GeoPoint(x,y,z);
  // Check whether stable
  pointValue = planetModel.getDocValueEncoder().encodePoint(x, y, z);
  assertEquals(x, planetModel.getDocValueEncoder().decodeXValue(pointValue), 0.0);
  assertEquals(y, planetModel.getDocValueEncoder().decodeYValue(pointValue), 0.0);
  assertEquals(z, planetModel.getDocValueEncoder().decodeZValue(pointValue), 0.0);
  // Check whether has some relationship with original point
  assertEquals(0.0, point.arcDistance(pointR), 0.02);
}
 
Example #2
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testProhibitedLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  // The MUST_NOT clause is called with the same lead cost as the MUST clause
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(80, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect
}
 
Example #3
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDisjunctionCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42));
  ScorerSupplier s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42, s.cost());
  assertEquals(42, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42 + 12, s.cost());
  assertEquals(42 + 12, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(20));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42 + 12 + 20, s.cost());
  assertEquals(42 + 12 + 20, s.get(random().nextInt(100)).iterator().cost());
}
 
Example #4
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Settings.Builder setRandomIndexTranslogSettings(Random random, Settings.Builder builder) {
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
                new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 300), ByteSizeUnit.MB));
    }
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
                new ByteSizeValue(1, ByteSizeUnit.PB)); // just don't flush
    }
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(),
                RandomPicks.randomFrom(random, Translog.Durability.values()));
    }

    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(),
                RandomNumbers.randomIntBetween(random, 100, 5000), TimeUnit.MILLISECONDS);
    }

    return builder;
}
 
Example #5
Source File: TestReaderPool.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDrop() throws IOException {
  Directory directory = newDirectory();
  FieldInfos.FieldNumbers fieldNumbers = buildIndex(directory);
  StandardDirectoryReader reader = (StandardDirectoryReader) DirectoryReader.open(directory);
  SegmentInfos segmentInfos = reader.segmentInfos.clone();

  ReaderPool pool = new ReaderPool(directory, directory, segmentInfos, fieldNumbers, () -> 0l, null, null, null);
  SegmentCommitInfo commitInfo = RandomPicks.randomFrom(random(), segmentInfos.asList());
  ReadersAndUpdates readersAndUpdates = pool.get(commitInfo, true);
  assertSame(readersAndUpdates, pool.get(commitInfo, false));
  assertTrue(pool.drop(commitInfo));
  if (random().nextBoolean()) {
    assertFalse(pool.drop(commitInfo));
  }
  assertNull(pool.get(commitInfo, false));
  pool.release(readersAndUpdates, random().nextBoolean());
  IOUtils.close(pool, reader, directory);
}
 
Example #6
Source File: TestFieldUpdatesBuffer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSortAndDedupNumericUpdatesByTerms() throws IOException {
  List<DocValuesUpdate.NumericDocValuesUpdate> updates = new ArrayList<>();
  int numUpdates = 1 + random().nextInt(1000);
  Counter counter = Counter.newCounter();
  String termField = RandomPicks.randomFrom(random(), Arrays.asList("id", "_id", "some_other_field"));
  long docValue = 1 + random().nextInt(1000);
  DocValuesUpdate.NumericDocValuesUpdate randomUpdate = new DocValuesUpdate.NumericDocValuesUpdate(
      new Term(termField, Integer.toString(random().nextInt(1000))), "numeric", docValue);
  randomUpdate = randomUpdate.prepareForApply(randomDocUpTo());
  updates.add(randomUpdate);
  FieldUpdatesBuffer buffer = new FieldUpdatesBuffer(counter, randomUpdate, randomUpdate.docIDUpto);
  for (int i = 0; i < numUpdates; i++) {
    randomUpdate = new DocValuesUpdate.NumericDocValuesUpdate(
        new Term(termField, Integer.toString(random().nextInt(1000))), "numeric", docValue);
    randomUpdate = randomUpdate.prepareForApply(randomDocUpTo());
    updates.add(randomUpdate);
    buffer.addUpdate(randomUpdate.term, randomUpdate.getValue(), randomUpdate.docIDUpto);
  }
  buffer.finish();
  assertBufferUpdates(buffer, updates, true);
}
 
Example #7
Source File: BaseTermVectorsFormatTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected RandomDocument(int fieldCount, int maxTermCount, Options options, String[] fieldNames, String[] sampleTerms, BytesRef[] sampleTermBytes) {
  if (fieldCount > fieldNames.length) {
    throw new IllegalArgumentException();
  }
  this.fieldNames = new String[fieldCount];
  fieldTypes = new FieldType[fieldCount];
  tokenStreams = new RandomTokenStream[fieldCount];
  Arrays.fill(fieldTypes, fieldType(options));
  final Set<String> usedFileNames = new HashSet<>();
  for (int i = 0; i < fieldCount; ++i) {
    do {
      this.fieldNames[i] = RandomPicks.randomFrom(random(), fieldNames);
    } while (usedFileNames.contains(this.fieldNames[i]));
    usedFileNames.add(this.fieldNames[i]);
    tokenStreams[i] = new RandomTokenStream(TestUtil.nextInt(random(), 1, maxTermCount), sampleTerms, sampleTermBytes);
  }
}
 
Example #8
Source File: TestMathUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static long randomLong() {
  if (random().nextBoolean()) {
    long l = 1;
    if (random().nextBoolean()) {
      l *= -1;
    }
    for (long i : PRIMES) {
      final int m = random().nextInt(3);
      for (int j = 0; j < m; ++j) {
        l *= i;
      }
    }
    return l;
  } else if (random().nextBoolean()) {
    return random().nextLong();
  } else {
    return RandomPicks.randomFrom(random(), Arrays.asList(Long.MIN_VALUE, Long.MAX_VALUE, 0L, -1L, 1L));
  }
}
 
Example #9
Source File: TestSuggestField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static IndexWriterConfig iwcWithSuggestField(Analyzer analyzer, final Set<String> suggestFields) {
  IndexWriterConfig iwc = newIndexWriterConfig(random(), analyzer);
  iwc.setMergePolicy(newLogMergePolicy());
  Codec filterCodec = new Lucene86Codec() {
    CompletionPostingsFormat.FSTLoadMode fstLoadMode =
        RandomPicks.randomFrom(random(), CompletionPostingsFormat.FSTLoadMode.values());
    PostingsFormat postingsFormat = new Completion84PostingsFormat(fstLoadMode);

    @Override
    public PostingsFormat getPostingsFormatForField(String field) {
      if (suggestFields.contains(field)) {
        return postingsFormat;
      }
      return super.getPostingsFormatForField(field);
    }
  };
  iwc.setCodec(filterCodec);
  return iwc;
}
 
Example #10
Source File: TestFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Object randomValue(Class<?> clazz) {
  if (clazz.isEnum()) {
    return RandomPicks.randomFrom(random(), clazz.getEnumConstants());
  } else if (clazz == boolean.class) {
    return random().nextBoolean();
  } else if (clazz == int.class) {
    return 1 + random().nextInt(100);
  }
  throw new AssertionError("Don't know how to generate a " + clazz);
}
 
Example #11
Source File: TestFieldUpdatesBuffer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
DocValuesUpdate.NumericDocValuesUpdate getRandomNumericUpdate() {
  String termField = RandomPicks.randomFrom(random(), Arrays.asList("id", "_id", "some_other_field"));
  String docId = "" + random().nextInt(10);
  DocValuesUpdate.NumericDocValuesUpdate value = new DocValuesUpdate.NumericDocValuesUpdate(new Term(termField, docId), "numeric",
      rarely() ? null : Long.valueOf(random().nextInt(100)));
  return rarely() ? value.prepareForApply(randomDocUpTo()) : value;
}
 
Example #12
Source File: TestFieldUpdatesBuffer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
DocValuesUpdate.BinaryDocValuesUpdate getRandomBinaryUpdate() {
  String termField = RandomPicks.randomFrom(random(), Arrays.asList("id", "_id", "some_other_field"));
  String docId = "" + random().nextInt(10);
  DocValuesUpdate.BinaryDocValuesUpdate value = new DocValuesUpdate.BinaryDocValuesUpdate(new Term(termField, docId), "binary",
      rarely() ? null : new BytesRef(TestUtil.randomRealisticUnicodeString(random())));
  return rarely() ? value.prepareForApply(randomDocUpTo()) : value;
}
 
Example #13
Source File: BaseLatLonShapeTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPolygonQueryEqualsAndHashcode() {
  Polygon polygon = GeoTestUtil.nextPolygon();
  QueryRelation queryRelation = RandomPicks.randomFrom(random(), QueryRelation.values());
  String fieldName = "foo";
  Query q1 = newPolygonQuery(fieldName, queryRelation, polygon);
  Query q2 = newPolygonQuery(fieldName, queryRelation, polygon);
  QueryUtils.checkEqual(q1, q2);
  //different field name
  Query q3 = newPolygonQuery("bar", queryRelation, polygon);
  QueryUtils.checkUnequal(q1, q3);
  //different query relation
  QueryRelation newQueryRelation = RandomPicks.randomFrom(random(), QueryRelation.values());
  Query q4 = newPolygonQuery(fieldName, newQueryRelation, polygon);
  if (queryRelation == newQueryRelation) {
    QueryUtils.checkEqual(q1, q4);
  } else {
    QueryUtils.checkUnequal(q1, q4);
  }
  //different shape
  Polygon newPolygon = GeoTestUtil.nextPolygon();;
  Query q5 = newPolygonQuery(fieldName, queryRelation, newPolygon);
  if (polygon.equals(newPolygon)) {
    QueryUtils.checkEqual(q1, q5);
  } else {
    QueryUtils.checkUnequal(q1, q5);
  }
}
 
Example #14
Source File: BaseLatLonShapeTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLineQueryEqualsAndHashcode() {
  Line line = nextLine();
  QueryRelation queryRelation = RandomPicks.randomFrom(random(), POINT_LINE_RELATIONS);
  String fieldName = "foo";
  Query q1 = newLineQuery(fieldName, queryRelation, line);
  Query q2 = newLineQuery(fieldName, queryRelation, line);
  QueryUtils.checkEqual(q1, q2);
  //different field name
  Query q3 = newLineQuery("bar", queryRelation, line);
  QueryUtils.checkUnequal(q1, q3);
  //different query relation
  QueryRelation newQueryRelation = RandomPicks.randomFrom(random(), POINT_LINE_RELATIONS);
  Query q4 = newLineQuery(fieldName, newQueryRelation, line);
  if (queryRelation == newQueryRelation) {
    QueryUtils.checkEqual(q1, q4);
  } else {
    QueryUtils.checkUnequal(q1, q4);
  }
  //different shape
  Line newLine = nextLine();
  Query q5 = newLineQuery(fieldName, queryRelation, newLine);
  if (line.equals(newLine)) {
    QueryUtils.checkEqual(q1, q5);
  } else {
    QueryUtils.checkUnequal(q1, q5);
  }
}
 
Example #15
Source File: BaseDataOutputTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected static List<IOConsumer<DataInput>> addRandomData(DataOutput dst, Random rnd, int maxAddCalls) throws IOException {
  try {
    List<IOConsumer<DataInput>> reply = new ArrayList<>();
    for (int i = 0; i < maxAddCalls; i++) {
      reply.add(RandomPicks.randomFrom(rnd, GENERATORS).apply(dst, rnd));
    }
    return reply;
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #16
Source File: TestQueryRescorer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testRescoreIsIdempotent() throws Exception {
  Directory dir = newDirectory();
  int numDocs = 100;
  String fieldName = "field";
  IndexReader reader = publishDocs(numDocs, fieldName, dir);

  // Construct a query that will get numDocs hits.
  String wordOne = dictionary.get(0);
  TermQuery termQuery = new TermQuery(new Term(fieldName, wordOne));
  IndexSearcher searcher = getSearcher(reader);
  searcher.setSimilarity(new BM25Similarity());
  TopDocs hits1 = searcher.search(termQuery, numDocs);
  TopDocs hits2 = searcher.search(termQuery, numDocs);

  // Next, use a more specific phrase query that will return different scores
  // from the above term query
  String wordTwo = RandomPicks.randomFrom(random(), dictionary);
  PhraseQuery phraseQuery = new PhraseQuery(1, fieldName, wordOne, wordTwo);

  // rescore, requesting the same hits as topN
  int topN = numDocs;
  TopDocs firstRescoreHits = QueryRescorer.rescore(searcher, hits1, phraseQuery, 2.0, topN);

  // now rescore again, where topN is less than numDocs
  topN = random().nextInt(numDocs-1);
  ScoreDoc[] secondRescoreHits = QueryRescorer.rescore(searcher, hits2, phraseQuery, 2.0, topN).scoreDocs;
  ScoreDoc[] expectedTopNScoreDocs = ArrayUtil.copyOfSubArray(firstRescoreHits.scoreDocs, 0, topN);
  CheckHits.checkEqual(phraseQuery, expectedTopNScoreDocs, secondRescoreHits);

  reader.close();
  dir.close();
}
 
Example #17
Source File: TestQueryRescorer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testRescoreOfASubsetOfHits() throws Exception {
  Directory dir = newDirectory();
  int numDocs = 100;
  String fieldName = "field";
  IndexReader reader = publishDocs(numDocs, fieldName, dir);

  // Construct a query that will get numDocs hits.
  String wordOne = dictionary.get(0);
  TermQuery termQuery = new TermQuery(new Term(fieldName, wordOne));
  IndexSearcher searcher = getSearcher(reader);
  searcher.setSimilarity(new BM25Similarity());
  TopDocs hits = searcher.search(termQuery, numDocs);

  // Next, use a more specific phrase query that will return different scores
  // from the above term query
  String wordTwo = RandomPicks.randomFrom(random(), dictionary);
  PhraseQuery phraseQuery = new PhraseQuery(1, fieldName, wordOne, wordTwo);

  // rescore, requesting a smaller topN
  int topN = random().nextInt(numDocs-1);
  TopDocs phraseQueryHits = QueryRescorer.rescore(searcher, hits, phraseQuery, 2.0, topN);
  assertEquals(topN, phraseQueryHits.scoreDocs.length);

  for (int i = 1; i < phraseQueryHits.scoreDocs.length; i++) {
    assertTrue(phraseQueryHits.scoreDocs[i].score <= phraseQueryHits.scoreDocs[i-1].score);
  }
  reader.close();
  dir.close();
}
 
Example #18
Source File: BaseLatLonShapeTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testBoxQueryEqualsAndHashcode() {
  Rectangle rectangle = GeoTestUtil.nextBox();
  QueryRelation queryRelation = RandomPicks.randomFrom(random(), QueryRelation.values());
  String fieldName = "foo";
  Query q1 = newRectQuery(fieldName, queryRelation, rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat);
  Query q2 = newRectQuery(fieldName, queryRelation, rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat);
  QueryUtils.checkEqual(q1, q2);
  //different field name
  Query q3 = newRectQuery("bar", queryRelation, rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat);
  QueryUtils.checkUnequal(q1, q3);
  //different query relation
  QueryRelation newQueryRelation = RandomPicks.randomFrom(random(), QueryRelation.values());
  Query q4 = newRectQuery(fieldName, newQueryRelation, rectangle.minLon, rectangle.maxLon, rectangle.minLat, rectangle.maxLat);
  if (queryRelation == newQueryRelation) {
    QueryUtils.checkEqual(q1, q4);
  } else {
    QueryUtils.checkUnequal(q1, q4);
  }
  //different shape
  Rectangle newRectangle = GeoTestUtil.nextBox();
  Query q5 = newRectQuery(fieldName, queryRelation, newRectangle.minLon, newRectangle.maxLon, newRectangle.minLat, newRectangle.maxLat);
  if (rectangle.equals(newRectangle)) {
    QueryUtils.checkEqual(q1, q5);
  } else {
    QueryUtils.checkUnequal(q1, q5);
  }
}
 
Example #19
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionWithMinShouldMatchCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12));
  ScorerSupplier s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12, s.cost());
  assertEquals(42 + 12, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(20));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12 + 20, s.cost());
  assertEquals(42 + 12 + 20, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 2);
  assertEquals(12 + 20, s.cost());
  assertEquals(12 + 20, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(30));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12 + 20 + 30, s.cost());
  assertEquals(42 + 12 + 20 + 30, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 2);
  assertEquals(12 + 20 + 30, s.cost());
  assertEquals(12 + 20 + 30, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 3);
  assertEquals(12 + 20, s.cost());
  assertEquals(12 + 20, s.get(random().nextInt(100)).iterator().cost());
}
 
Example #20
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testConjunctionCost() {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(42));
  assertEquals(42, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(12));
  assertEquals(12, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(20));
  assertEquals(12, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());
}
 
Example #21
Source File: TestTopFieldCollectorEarlyTermination.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Document randomDocument() {
  final Document doc = new Document();
  doc.add(new NumericDocValuesField("ndv1", random().nextInt(10)));
  doc.add(new NumericDocValuesField("ndv2", random().nextInt(10)));
  doc.add(new StringField("s", RandomPicks.randomFrom(random(), terms), Store.YES));
  return doc;
}
 
Example #22
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testToChildInitialAdvanceParentButNoKids() throws Exception {

    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

    // degenerate case: first doc has no children
    w.addDocument(makeResume("first", "nokids"));
    w.addDocuments(Arrays.asList(makeJob("job", 42), makeResume("second", "haskid")));

    // single segment
    w.forceMerge(1);

    final IndexReader r = w.getReader();
    final IndexSearcher s = newSearcher(r, false);
    w.close();

    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
    Query parentQuery = new TermQuery(new Term("docType", "resume"));

    ToChildBlockJoinQuery parentJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentFilter);

    Weight weight = s.createWeight(s.rewrite(parentJoinQuery), RandomPicks.randomFrom(random(), org.apache.lucene.search.ScoreMode.values()), 1);
    Scorer advancingScorer = weight.scorer(s.getIndexReader().leaves().get(0));
    Scorer nextDocScorer = weight.scorer(s.getIndexReader().leaves().get(0));

    final int firstKid = nextDocScorer.iterator().nextDoc();
    assertTrue("firstKid not found", DocIdSetIterator.NO_MORE_DOCS != firstKid);
    assertEquals(firstKid, advancingScorer.iterator().advance(0));

    r.close();
    dir.close();
  }
 
Example #23
Source File: BaseCompressingDocValuesFormatTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testUniqueValuesCompression() throws IOException {
  try (final Directory dir = new ByteBuffersDirectory()) {
    final IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    final IndexWriter iwriter = new IndexWriter(dir, iwc);

    final int uniqueValueCount = TestUtil.nextInt(random(), 1, 256);
    final List<Long> values = new ArrayList<>();

    final Document doc = new Document();
    final NumericDocValuesField dvf = new NumericDocValuesField("dv", 0);
    doc.add(dvf);
    for (int i = 0; i < 300; ++i) {
      final long value;
      if (values.size() < uniqueValueCount) {
        value = random().nextLong();
        values.add(value);
      } else {
        value = RandomPicks.randomFrom(random(), values);
      }
      dvf.setLongValue(value);
      iwriter.addDocument(doc);
    }
    iwriter.forceMerge(1);
    final long size1 = dirSize(dir);
    for (int i = 0; i < 20; ++i) {
      dvf.setLongValue(RandomPicks.randomFrom(random(), values));
      iwriter.addDocument(doc);
    }
    iwriter.forceMerge(1);
    final long size2 = dirSize(dir);
    // make sure the new longs did not cost 8 bytes each
    assertTrue(size2 < size1 + 8 * 20);
  }
}
 
Example #24
Source File: MockFSDirectoryService.java    From crate with Apache License 2.0 5 votes vote down vote up
private FsDirectoryService randomDirectoryService(IndexSettings indexSettings, ShardPath path) {
    final IndexMetaData build = IndexMetaData.builder(indexSettings.getIndexMetaData())
        .settings(Settings.builder()
            // don't use the settings from indexSettings#getSettings() they are merged with node settings and might contain
            // secure settings that should not be copied in here since the new IndexSettings ctor below will barf if we do
            .put(indexSettings.getIndexMetaData().getSettings())
            .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(),
                RandomPicks.randomFrom(random, IndexModule.Type.values()).getSettingsKey()))
        .build();
    final IndexSettings newIndexSettings = new IndexSettings(build, indexSettings.getNodeSettings());
    return new FsDirectoryService(newIndexSettings, path);
}
 
Example #25
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42, 54));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12, 54));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.SHOULD).clear();
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect
}
 
Example #26
Source File: TestUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a String thats "regexpish" (contains lots of operators typically found in regular expressions)
 * If you call this enough times, you might get a valid regex!
 * 
 * <P>Note: to avoid practically endless backtracking patterns we replace asterisk and plus
 * operators with bounded repetitions. See LUCENE-4111 for more info.
 * 
 * @param maxLength A hint about maximum length of the regexpish string. It may be exceeded by a few characters.
 */
public static String randomRegexpishString(Random r, int maxLength) {
  final StringBuilder regexp = new StringBuilder(maxLength);
  for (int i = nextInt(r, 0, maxLength); i > 0; i--) {
    if (r.nextBoolean()) {
      regexp.append((char) RandomNumbers.randomIntBetween(r, 'a', 'z'));
    } else {
      regexp.append(RandomPicks.randomFrom(r, ops));
    }
  }
  return regexp.toString();
}
 
Example #27
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
/** Pick a random object from the given list. */
public static <T> T randomFrom(List<T> list) {
    return RandomPicks.randomFrom(random(), list);
}
 
Example #28
Source File: BaseShapeTestCase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** test random generated circles */
protected void verifyRandomDistanceQueries(IndexReader reader, Object... shapes) throws Exception {
  IndexSearcher s = newSearcher(reader);

  final int iters = scaledIterationCount(shapes.length);

  Bits liveDocs = MultiBits.getLiveDocs(s.getIndexReader());
  int maxDoc = s.getIndexReader().maxDoc();

  for (int iter = 0; iter < iters; ++iter) {
    if (VERBOSE) {
      System.out.println("\nTEST: iter=" + (iter + 1) + " of " + iters + " s=" + s);
    }

    // Polygon
    Object queryCircle = randomQueryCircle();
    Component2D queryCircle2D = toCircle2D(queryCircle);
    QueryRelation queryRelation = RandomPicks.randomFrom(random(), QueryRelation.values());
    Query query = newDistanceQuery(FIELD_NAME, queryRelation, queryCircle);

    if (VERBOSE) {
      System.out.println("  query=" + query + ", relation=" + queryRelation);
    }

    final FixedBitSet hits = new FixedBitSet(maxDoc);
    s.search(query, new SimpleCollector() {

      private int docBase;

      @Override
      public ScoreMode scoreMode() {
        return ScoreMode.COMPLETE_NO_SCORES;
      }

      @Override
      protected void doSetNextReader(LeafReaderContext context) throws IOException {
        docBase = context.docBase;
      }

      @Override
      public void collect(int doc) throws IOException {
        hits.set(docBase+doc);
      }
    });

    boolean fail = false;
    NumericDocValues docIDToID = MultiDocValues.getNumericValues(reader, "id");
    for (int docID = 0; docID < maxDoc; ++docID) {
      assertEquals(docID, docIDToID.nextDoc());
      int id = (int) docIDToID.longValue();
      boolean expected;
      if (liveDocs != null && liveDocs.get(docID) == false) {
        // document is deleted
        expected = false;
      } else if (shapes[id] == null) {
        expected = false;
      } else {
        expected = VALIDATOR.setRelation(queryRelation).testComponentQuery(queryCircle2D, shapes[id]);
      }

      if (hits.get(docID) != expected) {
        StringBuilder b = new StringBuilder();

        if (expected) {
          b.append("FAIL: id=" + id + " should match but did not\n");
        } else {
          b.append("FAIL: id=" + id + " should not match but did\n");
        }
        b.append("  relation=" + queryRelation + "\n");
        b.append("  query=" + query + " docID=" + docID + "\n");
        if (shapes[id] instanceof Object[]) {
          b.append("  shape=" + Arrays.toString((Object[]) shapes[id]) + "\n");
        } else {
          b.append("  shape=" + shapes[id] + "\n");
        }
        b.append("  deleted?=" + (liveDocs != null && liveDocs.get(docID) == false));
        b.append("  distanceQuery=" + queryCircle.toString());
        if (true) {
          fail("wrong hit (first of possibly more):\n\n" + b);
        } else {
          System.out.println(b.toString());
          fail = true;
        }
      }
    }
    if (fail) {
      fail("some hits were wrong");
    }
  }
}
 
Example #29
Source File: TestCustomSeparatorBreakIterator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static char randomSeparator() {
  return RandomPicks.randomFrom(random(), SEPARATORS);
}
 
Example #30
Source File: InternalTestCluster.java    From crate with Apache License 2.0 4 votes vote down vote up
private static Settings getRandomNodeSettings(long seed) {
    Random random = new Random(seed);
    Builder builder = Settings.builder();
    builder.put(TransportSettings.TRANSPORT_COMPRESS.getKey(), rarely(random));
    if (random.nextBoolean()) {
        builder.put("cache.recycler.page.type", RandomPicks.randomFrom(random, PageCacheRecycler.Type.values()));
    }

    builder.put(EsExecutors.PROCESSORS_SETTING.getKey(), 1 + random.nextInt(3));

    // randomize tcp settings
    if (random.nextBoolean()) {
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_RECOVERY.getKey(), random.nextInt(2) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_BULK.getKey(), random.nextInt(3) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_REG.getKey(), random.nextInt(6) + 1);
    }

    if (random.nextBoolean()) {
        builder.put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(),
                timeValueSeconds(RandomNumbers.randomIntBetween(random, 10, 30)).getStringRep());
    }

    if (random.nextInt(10) == 0) {
        builder.put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
        builder.put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
    }

    if (random.nextBoolean()) {
        if (random.nextInt(10) == 0) { // do something crazy slow here
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
                    new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 10), ByteSizeUnit.MB));
        } else {
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
                    new ByteSizeValue(RandomNumbers.randomIntBetween(random, 10, 200), ByteSizeUnit.MB));
        }
    }

    if (random.nextBoolean()) {
        builder.put(TransportSettings.PING_SCHEDULE.getKey(), RandomNumbers.randomIntBetween(random, 100, 2000) + "ms");
    }

    return builder.build();
}