Java Code Examples for org.apache.lucene.util.TestUtil

The following examples show how to use org.apache.lucene.util.TestUtil. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: FieldTermStackTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testTermInfoComparisonConsistency() {
  TermInfo a = new TermInfo( TestUtil.randomUnicodeString(random()), 0, 0, 0, 1 );
  TermInfo b = new TermInfo( TestUtil.randomUnicodeString(random()), 0, 0, 1, 1 );
  TermInfo c = new TermInfo( TestUtil.randomUnicodeString(random()), 0, 0, 2, 1 );
  TermInfo d = new TermInfo( TestUtil.randomUnicodeString(random()), 0, 0, 0, 1 );

  assertConsistentEquals( a, a );
  assertConsistentEquals( b, b );
  assertConsistentEquals( c, c );
  assertConsistentEquals( d, d );
  assertConsistentEquals( a, d );
  assertConsistentLessThan( a, b );
  assertConsistentLessThan( b, c );
  assertConsistentLessThan( a, c );
  assertConsistentLessThan( d, b );
  assertConsistentLessThan( d, c );
}
 
Example 2
Source Project: lucene-solr   Source File: BaseNormsFormatTestCase.java    License: Apache License 2.0 6 votes vote down vote up
public void testNCommon() throws Exception {
  final Random r = random();
  final int N = TestUtil.nextInt(r, 2, 15);
  final long[] commonValues = new long[N];
  for (int j = 0; j < N; ++j) {
    commonValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);
  }
  final int numOtherValues = TestUtil.nextInt(r, 2, 256 - N);
  final long[] otherValues = new long[numOtherValues];
  for (int j = 0; j < numOtherValues; ++j) {
    otherValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);
  }
  doTestNormsVersusDocValues(1, new LongSupplier() {
    @Override
    public long getAsLong() {
      return r.nextInt(100) == 0 ? otherValues[r.nextInt(numOtherValues - 1)] : commonValues[r.nextInt(N - 1)];
    }
  });
}
 
Example 3
Source Project: lucene-solr   Source File: FullSolrCloudDistribCmdsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testConcurrentIndexing() throws Exception {
  final CloudSolrClient cloudClient = cluster.getSolrClient();
  final String collectionName = createAndSetNewDefaultCollection();

  final int numDocs = atLeast(50);
  final JettySolrRunner nodeToUpdate = cluster.getRandomJetty(random());
  try (ConcurrentUpdateSolrClient indexClient
       = getConcurrentUpdateSolrClient(nodeToUpdate.getProxyBaseUrl() + "/" + collectionName, 10, 2)) {
    
    for (int i = 0; i < numDocs; i++) {
      indexClient.add(sdoc("id", i, "text_t",
                           TestUtil.randomRealisticUnicodeString(random(), 200)));
    }
    indexClient.blockUntilFinished();
    
    assertEquals(0, indexClient.commit().getStatus());
    assertEquals(numDocs, cloudClient.query(params("q","*:*")).getResults().getNumFound());

    checkShardConsistency(params("q","*:*", "rows", ""+(1 + numDocs),"_trace","addAll"));
  }
}
 
Example 4
Source Project: lucene-solr   Source File: TestSegmentTermDocs.java    License: Apache License 2.0 6 votes vote down vote up
public void testTermDocs() throws IOException {
  //After adding the document, we should be able to read it back in
  SegmentReader reader = new SegmentReader(info, Version.LATEST.major, newIOContext(random()));
  assertTrue(reader != null);

  TermsEnum terms = reader.terms(DocHelper.TEXT_FIELD_2_KEY).iterator();
  terms.seekCeil(new BytesRef("field"));
  PostingsEnum termDocs = TestUtil.docs(random(), terms, null, PostingsEnum.FREQS);
  if (termDocs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS)    {
    int docId = termDocs.docID();
    assertTrue(docId == 0);
    int freq = termDocs.freq();
    assertTrue(freq == 3);  
  }
  reader.close();
}
 
Example 5
public void testFullEviction() throws IOException {
    int pass = TestUtil.nextInt(random(), 10, 100);
    CachedFeatureStore cachedFeatureStore = new CachedFeatureStore(memStore, caches);
    while (pass-- > 0) {
        StoredFeature feat = LtrTestUtils.randomFeature();
        memStore.add(feat);
        cachedFeatureStore.load(feat.name());

        StoredFeatureSet set = LtrTestUtils.randomFeatureSet();
        memStore.add(set);
        cachedFeatureStore.loadSet(set.name());

        CompiledLtrModel model = LtrTestUtils.buildRandomModel();
        memStore.add(model);
        cachedFeatureStore.loadModel(model.name());
    }
    caches.evict(memStore.getStoreName());
    assertEquals(0, cachedFeatureStore.totalWeight());
    assertTrue(caches.getCachedStoreNames().isEmpty());
    assertEquals(0, caches.getPerStoreStats(memStore.getStoreName()).modelRam());
    assertEquals(0, caches.getPerStoreStats(memStore.getStoreName()).totalCount());
}
 
Example 6
Source Project: lucene-solr   Source File: TestDuelingAnalyzers.java    License: Apache License 2.0 6 votes vote down vote up
public void testLetterHtmlish() throws Exception {
  Random random = random();
  Analyzer left = new MockAnalyzer(random, jvmLetter, false);
  Analyzer right = new Analyzer() {
    @Override
    protected TokenStreamComponents createComponents(String fieldName) {
      Tokenizer tokenizer = new LetterTokenizer(newAttributeFactory());
      return new TokenStreamComponents(tokenizer, tokenizer);
    }
  };
  for (int i = 0; i < 200; i++) {
    String s = TestUtil.randomHtmlishString(random, 20);
    assertEquals(s, left.tokenStream("foo", newStringReader(s)), 
                 right.tokenStream("foo", newStringReader(s)));
  }
  IOUtils.close(left, right);
}
 
Example 7
Source Project: lucene-solr   Source File: BaseDocValuesFormatTestCase.java    License: Apache License 2.0 6 votes vote down vote up
public void testRandomAdvanceNumeric() throws IOException {
  final long longRange;
  if (random().nextBoolean()) {
    longRange = TestUtil.nextInt(random(), 1, 1024);
  } else {
    longRange = TestUtil.nextLong(random(), 1, Long.MAX_VALUE);
  }
  doTestRandomAdvance(new FieldCreator() {
      @Override
      public Field next() {
        return new NumericDocValuesField("field", TestUtil.nextLong(random(), 0, longRange));
      }

      @Override
      public DocIdSetIterator iterator(IndexReader r) throws IOException {
        return MultiDocValues.getNumericValues(r, "field");
      }
    });
}
 
Example 8
Source Project: lucene-solr   Source File: SolrCoreMetricManagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRegisterMetricsWithReplacements() {
  Random random = random();

  Map<String, Counter> registered = new HashMap<>();
  String scope = SolrMetricTestUtils.getRandomScope(random, true);
  SolrInfoBean.Category category = SolrMetricTestUtils.getRandomCategory(random, true);

  int iterations = TestUtil.nextInt(random, 0, MAX_ITERATIONS);
  for (int i = 0; i < iterations; ++i) {
    Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetricsWithReplacements(random, registered);
    if (metrics.isEmpty()) {
      continue;
    }
    SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
    coreMetricManager.registerMetricProducer(scope, producer);
    registered.putAll(metrics);
    assertRegistered(scope, registered, coreMetricManager);
  }
}
 
Example 9
Source Project: lucene-solr   Source File: TestIDVersionPostingsFormat.java    License: Apache License 2.0 6 votes vote down vote up
public void testMoreThanOneDocPerIDOneSegment() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setCodec(TestUtil.alwaysPostingsFormat(new IDVersionPostingsFormat()));
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc, false);
  Document doc = new Document();
  doc.add(makeIDField("id", 17));
  w.addDocument(doc);

  Document duplicate = new Document();
  duplicate.add(makeIDField("id", 17));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(duplicate);
    w.commit(false);
  });

  w.close();
  dir.close();
}
 
Example 10
Source Project: lucene-solr   Source File: TestBKDRadixSelector.java    License: Apache License 2.0 6 votes vote down vote up
public void testRandomLastByteTwoValues() throws IOException {
  int values = random().nextInt(15000) + 1;
  Directory dir = getDirectory(values);
  int partitionPoint = random().nextInt(values);
  int sortedOnHeap = random().nextInt(5000);
  int indexDimensions =  TestUtil.nextInt(random(), 1, 8);
  int dataDimensions =  TestUtil.nextInt(random(), indexDimensions, 8);
  int bytesPerDimensions = TestUtil.nextInt(random(), 2, 30);
  int packedLength = dataDimensions * bytesPerDimensions;
  PointWriter points = getRandomPointWriter(dir, values, packedLength);
  byte[] value = new byte[packedLength];
  random().nextBytes(value);
  for (int i =0; i < values; i++) {
    if (random().nextBoolean()) {
      points.append(value, 1);
    } else {
      points.append(value, 2);
    }
  }
  points.close();
  verify(dir, points, dataDimensions, indexDimensions, 0, values, partitionPoint, packedLength, bytesPerDimensions, sortedOnHeap);
  dir.close();
}
 
Example 11
Source Project: lucene-solr   Source File: TestPackedInts.java    License: Apache License 2.0 6 votes vote down vote up
public void testPackedLongValuesOnZeros() {
  // Make sure that when all values are the same, they use 0 bits per value
  final int pageSize = 1 << TestUtil.nextInt(random(), 6, 20);
  final float acceptableOverheadRatio = random().nextFloat();

  assertEquals(
      PackedLongValues.packedBuilder(pageSize, acceptableOverheadRatio).add(0).build().ramBytesUsed(),
      PackedLongValues.packedBuilder(pageSize, acceptableOverheadRatio).add(0).add(0).build().ramBytesUsed());

  final long l = random().nextLong();
  assertEquals(
    PackedLongValues.deltaPackedBuilder(pageSize, acceptableOverheadRatio).add(l).build().ramBytesUsed(),
    PackedLongValues.deltaPackedBuilder(pageSize, acceptableOverheadRatio).add(l).add(l).build().ramBytesUsed());

  final long avg = random().nextInt(100);
  assertEquals(
    PackedLongValues.monotonicBuilder(pageSize, acceptableOverheadRatio).add(l).add(l + avg).build().ramBytesUsed(),
    PackedLongValues.monotonicBuilder(pageSize, acceptableOverheadRatio).add(l).add(l + avg).add(l + 2 * avg).build().ramBytesUsed());
}
 
Example 12
Source Project: lucene-solr   Source File: TestIDVersionPostingsFormat.java    License: Apache License 2.0 6 votes vote down vote up
public void testInvalidVersions2() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setCodec(TestUtil.alwaysPostingsFormat(new IDVersionPostingsFormat()));
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc, false);
  Document doc = new Document();
  // Long.MAX_VALUE:
  doc.add(new StringAndPayloadField("id", "id", new BytesRef(new byte[] {(byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff})));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc);
    w.commit(false);
  });
  expectThrows(AlreadyClosedException.class, () -> {
    w.addDocument(doc);
  });

  dir.close();
}
 
Example 13
Source Project: lucene-solr   Source File: TestDuelingAnalyzers.java    License: Apache License 2.0 6 votes vote down vote up
public void testLetterUnicode() throws Exception {
  Random random = random();
  Analyzer left = new MockAnalyzer(random(), jvmLetter, false);
  Analyzer right = new Analyzer() {
    @Override
    protected TokenStreamComponents createComponents(String fieldName) {
      Tokenizer tokenizer = new LetterTokenizer(newAttributeFactory());
      return new TokenStreamComponents(tokenizer, tokenizer);
    }
  };
  for (int i = 0; i < 200; i++) {
    String s = TestUtil.randomUnicodeString(random);
    assertEquals(s, left.tokenStream("foo", newStringReader(s)), 
                 right.tokenStream("foo", newStringReader(s)));
  }
  IOUtils.close(left, right);
}
 
Example 14
Source Project: lucene-solr   Source File: TestSwappedIndexFiles.java    License: Apache License 2.0 6 votes vote down vote up
private void indexOneDoc(long seed, Directory dir, Document doc, boolean useCFS) throws IOException {
  Random random = new Random(seed);
  IndexWriterConfig conf = newIndexWriterConfig(random, new MockAnalyzer(random));
  conf.setCodec(TestUtil.getDefaultCodec());

  if (useCFS == false) {
    conf.setUseCompoundFile(false);
    conf.getMergePolicy().setNoCFSRatio(0.0);
  } else {
    conf.setUseCompoundFile(true);
    conf.getMergePolicy().setNoCFSRatio(1.0);
  }

  RandomIndexWriter w = new RandomIndexWriter(random, dir, conf);
  w.addDocument(doc);
  w.close();
}
 
Example 15
Source Project: lucene-solr   Source File: TestAddIndexes.java    License: Apache License 2.0 6 votes vote down vote up
public void testAddIndexesWithCloseNoWait() throws Throwable {

    final int NUM_COPY = 50;
    CommitAndAddIndexes3 c = new CommitAndAddIndexes3(NUM_COPY);
    c.launchThreads(-1);

    Thread.sleep(TestUtil.nextInt(random(), 10, 500));

    // Close w/o first stopping/joining the threads
    if (VERBOSE) {
      System.out.println("TEST: now close(false)");
    }
    c.close(false);

    c.joinThreads();

    if (VERBOSE) {
      System.out.println("TEST: done join threads");
    }
    c.closeDir();

    assertTrue(c.failures.size() == 0);
  }
 
Example 16
public void testBulkMergeWithDeletes() throws IOException {
  final int numDocs = atLeast(200);
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(NoMergePolicy.INSTANCE));
  for (int i = 0; i < numDocs; ++i) {
    Document doc = new Document();
    doc.add(new StringField("id", Integer.toString(i), Store.YES));
    doc.add(new StoredField("f", TestUtil.randomSimpleString(random())));
    w.addDocument(doc);
  }
  final int deleteCount = TestUtil.nextInt(random(), 5, numDocs);
  for (int i = 0; i < deleteCount; ++i) {
    final int id = random().nextInt(numDocs);
    w.deleteDocuments(new Term("id", Integer.toString(id)));
  }
  w.commit();
  w.close();
  w = new RandomIndexWriter(random(), dir);
  w.forceMerge(TestUtil.nextInt(random(), 1, 3));
  w.commit();
  w.close();
  TestUtil.checkIndex(dir);
  dir.close();
}
 
Example 17
Source Project: lucene-solr   Source File: SolrMetricManagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRegisterAll() throws Exception {
  Random r = random();

  SolrMetricManager metricManager = new SolrMetricManager();

  Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(r, true);
  MetricRegistry mr = new MetricRegistry();
  for (Map.Entry<String, Counter> entry : metrics.entrySet()) {
    mr.register(entry.getKey(), entry.getValue());
  }

  String registryName = TestUtil.randomSimpleString(r, 1, 10);
  assertEquals(0, metricManager.registry(registryName).getMetrics().size());
  // There is nothing registered so we should be error-free on the first pass
  metricManager.registerAll(registryName, mr, SolrMetricManager.ResolutionStrategy.ERROR);
  // this should simply skip existing names
  metricManager.registerAll(registryName, mr, SolrMetricManager.ResolutionStrategy.IGNORE);
  // this should re-register everything, and no errors
  metricManager.registerAll(registryName, mr, SolrMetricManager.ResolutionStrategy.REPLACE);
  // this should produce error
  expectThrows(IllegalArgumentException.class, () -> metricManager.registerAll(registryName, mr, SolrMetricManager.ResolutionStrategy.ERROR));
}
 
Example 18
Source Project: lucene-solr   Source File: TestCheckJoinIndex.java    License: Apache License 2.0 6 votes vote down vote up
public void testNoParent() throws IOException {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  final int numDocs = TestUtil.nextInt(random(), 1, 3);
  for (int i = 0; i < numDocs; ++i) {
    w.addDocument(new Document());
  }
  final IndexReader reader = w.getReader();
  w.close();
  BitSetProducer parentsFilter = new QueryBitSetProducer(new MatchNoDocsQuery());
  try {
    expectThrows(IllegalStateException.class, () -> CheckJoinIndex.check(reader, parentsFilter));
  } finally {
    reader.close();
    dir.close();
  }
}
 
Example 19
Source Project: lucene-solr   Source File: TestIDVersionPostingsFormat.java    License: Apache License 2.0 6 votes vote down vote up
public void testInvalidVersions() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setCodec(TestUtil.alwaysPostingsFormat(new IDVersionPostingsFormat()));
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc, false);
  Document doc = new Document();
  // -1
  doc.add(new StringAndPayloadField("id", "id", new BytesRef(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff})));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc);
    w.commit(false);
  });
  expectThrows(AlreadyClosedException.class, () -> {
    w.addDocument(doc);
  });
  dir.close();
}
 
Example 20
Source Project: lucene-solr   Source File: TestExportWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void createLargeIndex() throws Exception {
  int BATCH_SIZE = 1000;
  int NUM_BATCHES = 100;
  SolrInputDocument[] docs = new SolrInputDocument[BATCH_SIZE];
  for (int i = 0; i < NUM_BATCHES; i++) {
    for (int j = 0; j < BATCH_SIZE; j++) {
      docs[j] = new SolrInputDocument(
          "id", String.valueOf(i * BATCH_SIZE + j),
          "batch_i_p", String.valueOf(i),
          "random_i_p", String.valueOf(random().nextInt(BATCH_SIZE)),
          "sortabledv", TestUtil.randomSimpleString(random(), 2, 3),
          "sortabledv_udvas", String.valueOf(random().nextInt(100)),
          "small_i_p", String.valueOf((i + j) % 7)
          );
    }
    updateJ(jsonAdd(docs), null);
  }
  assertU(commit());
}
 
Example 21
Source Project: lucene-solr   Source File: EdgeNGramTokenizerTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testFewTokenChars() throws IOException {
  final char[] chrs = new char[TestUtil.nextInt(random(), 4000, 5000)];
  Arrays.fill(chrs, ' ');
  for (int i = 0; i < chrs.length; ++i) {
    if (random().nextFloat() < 0.1) {
      chrs[i] = 'a';
    }
  }
  final int minGram = TestUtil.nextInt(random(), 1, 2);
  final int maxGram = TestUtil.nextInt(random(), minGram, 2);
  testNGrams(minGram, maxGram, new String(chrs), " ");
}
 
Example 22
Source Project: lucene-solr   Source File: CursorPagingTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * test faceting with deep paging
 */
public void testFacetingWithRandomSorts() throws Exception {
  final int numDocs = TestUtil.nextInt(random(), 1000, 3000);
  String[] fieldsToFacetOn = { "int", "long", "str" };
  String[] facetMethods = { "enum", "fc", "fcs" };

  for (int i = 1; i <= numDocs; i++) {
    SolrInputDocument doc = buildRandomDocument(i);
    assertU(adoc(doc));
  }
  assertU(commit());

  Collection<String> allFieldNames = getAllSortFieldNames();
  String[] fieldNames = new String[allFieldNames.size()];
  allFieldNames.toArray(fieldNames);
  String f = fieldNames[TestUtil.nextInt(random(), 0, fieldNames.length - 1)];
  String order = 0 == TestUtil.nextInt(random(), 0, 1) ? " asc" : " desc";
  String sort = f + order + (f.equals("id") ? "" : ", id" + order);
  String rows = "" + TestUtil.nextInt(random(), 13, 50);
  String facetField = fieldsToFacetOn
      [TestUtil.nextInt(random(), 0, fieldsToFacetOn.length - 1)];
  String facetMethod = facetMethods
      [TestUtil.nextInt(random(), 0, facetMethods.length - 1)];
  SentinelIntSet ids = assertFullWalkNoDupsWithFacets
      (numDocs, params("q", "*:*",
          "fl", "id," + facetField,
          "facet", "true",
          "facet.field", facetField,
          "facet.method", facetMethod,
          "facet.missing", "true",
          "facet.limit", "-1", // unlimited
          "rows", rows,
          "sort", sort));
  assertEquals(numDocs, ids.size());
}
 
Example 23
Source Project: lucene-solr   Source File: TestConcurrentMergeScheduler.java    License: Apache License 2.0 5 votes vote down vote up
public void testTotalBytesSize() throws Exception {
  Directory d = newDirectory();
  if (d instanceof MockDirectoryWrapper) {
    ((MockDirectoryWrapper)d).setThrottling(MockDirectoryWrapper.Throttling.NEVER);
  }
  IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
  iwc.setMaxBufferedDocs(5);
  CountDownLatch atLeastOneMerge = new CountDownLatch(1);
  iwc.setMergeScheduler(new TrackingCMS(atLeastOneMerge));
  if (TestUtil.getPostingsFormat("id").equals("SimpleText")) {
    // no
    iwc.setCodec(TestUtil.alwaysPostingsFormat(TestUtil.getDefaultPostingsFormat()));
  }
  IndexWriter w = new IndexWriter(d, iwc);
  for(int i=0;i<1000;i++) {
    Document doc = new Document();
    doc.add(new StringField("id", ""+i, Field.Store.NO));
    w.addDocument(doc);

    if (random().nextBoolean()) {
      w.deleteDocuments(new Term("id", ""+random().nextInt(i+1)));
    }
  }
  atLeastOneMerge.await();
  assertTrue(((TrackingCMS) w.getConfig().getMergeScheduler()).totMergedBytes != 0);
  w.close();
  d.close();
}
 
Example 24
Source Project: lucene-solr   Source File: TestMutablePointsReaderUtils.java    License: Apache License 2.0 5 votes vote down vote up
private void doTestSortByDim() {
  final int numIndexDims = TestUtil.nextInt(random(), 1, 8);
  final int numDataDims = TestUtil.nextInt(random(), numIndexDims, 8);
  final int bytesPerDim = TestUtil.nextInt(random(), 1, 16);
  final int maxDoc = TestUtil.nextInt(random(), 1, 1 << random().nextInt(30));
  int[] commonPrefixLengths = new int[numDataDims];
  Point[] points = createRandomPoints(numDataDims, numIndexDims, bytesPerDim, maxDoc, commonPrefixLengths);
  DummyPointsReader reader = new DummyPointsReader(points);
  final int sortedDim = random().nextInt(numIndexDims);
  MutablePointsReaderUtils.sortByDim(numDataDims, numIndexDims, sortedDim, bytesPerDim, commonPrefixLengths, reader, 0, points.length,
      new BytesRef(), new BytesRef());
  for (int i = 1; i < points.length; ++i) {
    final int offset = sortedDim * bytesPerDim;
    BytesRef previousValue = reader.points[i-1].packedValue;
    BytesRef currentValue = reader.points[i].packedValue;
    int cmp = Arrays.compareUnsigned(previousValue.bytes, previousValue.offset + offset, previousValue.offset + offset + bytesPerDim, currentValue.bytes, currentValue.offset + offset, currentValue.offset + offset + bytesPerDim);
    if (cmp == 0) {
      int dataDimOffset = numIndexDims * bytesPerDim;
      int dataDimsLength = (numDataDims - numIndexDims) * bytesPerDim;
      cmp = Arrays.compareUnsigned(previousValue.bytes, previousValue.offset + dataDimOffset, previousValue.offset + dataDimOffset + dataDimsLength,
          currentValue.bytes, currentValue.offset + dataDimOffset, currentValue.offset + dataDimOffset + dataDimsLength);
      if (cmp == 0) {
        cmp = reader.points[i - 1].doc - reader.points[i].doc;
      }
    }
    assertTrue(cmp <= 0);
  }
}
 
Example 25
Source Project: lucene-solr   Source File: TestFieldCacheSort.java    License: Apache License 2.0 5 votes vote down vote up
public void testMaxScore() throws Exception {
  Directory d = newDirectory();
  // Not RIW because we need exactly 2 segs:
  IndexWriter w = new IndexWriter(d, new IndexWriterConfig(new MockAnalyzer(random())));
  int id = 0;
  for(int seg=0;seg<2;seg++) {
    for(int docIDX=0;docIDX<10;docIDX++) {
      Document doc = new Document();
      doc.add(new LegacyIntField("id", docIDX, Field.Store.YES));
      StringBuilder sb = new StringBuilder();
      for(int i=0;i<id;i++) {
        sb.append(' ');
        sb.append("text");
      }
      doc.add(newTextField("body", sb.toString(), Field.Store.NO));
      w.addDocument(doc);
      id++;
    }
    w.commit();
  }

  IndexReader r = UninvertingReader.wrap(DirectoryReader.open(w),
                  Collections.singletonMap("id", Type.LEGACY_INTEGER));
  w.close();
  Query q = new TermQuery(new Term("body", "text"));
  TestUtil.checkReader(r);
  r.close();
  d.close();
}
 
Example 26
Source Project: lucene-solr   Source File: TestBackwardsCompatibility.java    License: Apache License 2.0 5 votes vote down vote up
public void testNextIntoWrongField() throws Exception {
  for (String name : oldNames) {
    Directory dir = oldIndexDirs.get(name);
    IndexReader r = DirectoryReader.open(dir);
    TermsEnum terms = MultiTerms.getTerms(r, "content").iterator();
    BytesRef t = terms.next();
    assertNotNull(t);

    // content field only has term aaa:
    assertEquals("aaa", t.utf8ToString());
    assertNull(terms.next());

    BytesRef aaaTerm = new BytesRef("aaa");

    // should be found exactly
    assertEquals(TermsEnum.SeekStatus.FOUND,
                 terms.seekCeil(aaaTerm));
    assertEquals(35, countDocs(TestUtil.docs(random(), terms, null, PostingsEnum.NONE)));
    assertNull(terms.next());

    // should hit end of field
    assertEquals(TermsEnum.SeekStatus.END,
                 terms.seekCeil(new BytesRef("bbb")));
    assertNull(terms.next());

    // should seek to aaa
    assertEquals(TermsEnum.SeekStatus.NOT_FOUND,
                 terms.seekCeil(new BytesRef("a")));
    assertTrue(terms.term().bytesEquals(aaaTerm));
    assertEquals(35, countDocs(TestUtil.docs(random(), terms, null, PostingsEnum.NONE)));
    assertNull(terms.next());

    assertEquals(TermsEnum.SeekStatus.FOUND,
                 terms.seekCeil(aaaTerm));
    assertEquals(35, countDocs(TestUtil.docs(random(), terms, null, PostingsEnum.NONE)));
    assertNull(terms.next());

    r.close();
  }
}
 
Example 27
Source Project: lucene-solr   Source File: TestFuzzyQuery.java    License: Apache License 2.0 5 votes vote down vote up
private String randomSimpleString(int digits) {
  int termLength = TestUtil.nextInt(random(), 1, 8);
  char[] chars = new char[termLength];
  for(int i=0;i<termLength;i++) {
    chars[i] = (char) ('a' + random().nextInt(digits));
  }
  return new String(chars);
}
 
Example 28
/**
 * half of the time, returns a random term from TEST_TERMS.
 * the other half of the time, returns a random unicode string.
 */
private String randomTerm() {
  if (random().nextBoolean()) {
    // return a random TEST_TERM
    return TEST_TERMS[random().nextInt(TEST_TERMS.length)];
  } else {
    // return a random unicode term
    return TestUtil.randomUnicodeString(random());
  }
}
 
Example 29
Source Project: lucene-solr   Source File: TestUTF32ToUTF8.java    License: Apache License 2.0 5 votes vote down vote up
private void assertAutomaton(Automaton automaton) throws Exception {
  CharacterRunAutomaton cra = new CharacterRunAutomaton(automaton);
  ByteRunAutomaton bra = new ByteRunAutomaton(automaton);
  final AutomatonTestUtil.RandomAcceptedStrings ras = new AutomatonTestUtil.RandomAcceptedStrings(automaton);
  
  int num = atLeast(1000);
  for (int i = 0; i < num; i++) {
    final String string;
    if (random().nextBoolean()) {
      // likely not accepted
      string = TestUtil.randomUnicodeString(random());
    } else {
      // will be accepted
      int[] codepoints = ras.getRandomAcceptedString(random());
      try {
        string = UnicodeUtil.newString(codepoints, 0, codepoints.length);
      } catch (Exception e) {
        System.out.println(codepoints.length + " codepoints:");
        for(int j=0;j<codepoints.length;j++) {
          System.out.println("  " + Integer.toHexString(codepoints[j]));
        }
        throw e;
      }
    }
    byte bytes[] = string.getBytes(StandardCharsets.UTF_8);
    assertEquals(cra.run(string), bra.run(bytes, 0, bytes.length));
  }
}
 
Example 30
Source Project: lucene-solr   Source File: TestSynonymMapFilter.java    License: Apache License 2.0 5 votes vote down vote up
private String randomNonEmptyString() {
  while(true) {
    final String s = TestUtil.randomUnicodeString(random()).trim();
    if (s.length() != 0 && s.indexOf('\u0000') == -1) {
      return s;
    }
  }
}