Java Code Examples for org.apache.solr.search.SolrIndexSearcher#getRawReader()

The following examples show how to use org.apache.solr.search.SolrIndexSearcher#getRawReader() . 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: IndexSizeEstimatorTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testEstimator() throws Exception {
  JettySolrRunner jetty = cluster.getRandomJetty(random());
  String randomCoreName = jetty.getCoreContainer().getAllCoreNames().iterator().next();
  SolrCore core = jetty.getCoreContainer().getCore(randomCoreName);
  RefCounted<SolrIndexSearcher> searcherRef = core.getSearcher();
  try {
    SolrIndexSearcher searcher = searcherRef.get();
    // limit the max length
    IndexSizeEstimator estimator = new IndexSizeEstimator(searcher.getRawReader(), 20, 50, true, true);
    IndexSizeEstimator.Estimate estimate = estimator.estimate();
    Map<String, Long> fieldsBySize = estimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", fieldsBySize.isEmpty());
    assertEquals(fieldsBySize.toString(), fields.size(), fieldsBySize.size());
    fieldsBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Long> typesBySize = estimate.getTypesBySize();
    assertFalse("empty typesBySize", typesBySize.isEmpty());
    assertTrue("expected at least 8 types: " + typesBySize.toString(), typesBySize.size() >= 8);
    typesBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Object> summary = estimate.getSummary();
    assertNotNull("summary", summary);
    assertFalse("empty summary", summary.isEmpty());
    assertEquals(summary.keySet().toString(), fields.size(), summary.keySet().size());
    Map<String, Object> details = estimate.getDetails();
    assertNotNull("details", details);
    assertFalse("empty details", details.isEmpty());
    // by type
    assertEquals(details.keySet().toString(), 6, details.keySet().size());

    // check sampling
    estimator.setSamplingThreshold(searcher.getRawReader().maxDoc() / 2);
    IndexSizeEstimator.Estimate sampledEstimate = estimator.estimate();
    Map<String, Long> sampledFieldsBySize = sampledEstimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", sampledFieldsBySize.isEmpty());
    // verify that the sampled values are within 50% of the original values
    fieldsBySize.forEach((field, size) -> {
      Long sampledSize = sampledFieldsBySize.get(field);
      assertNotNull("sampled size for " + field + " is missing in " + sampledFieldsBySize, sampledSize);
      double delta = (double) size * 0.5;
      assertEquals("sampled size of " + field + " is wildly off", (double)size, (double)sampledSize, delta);
    });
    // verify the reader is still usable - SOLR-13694
    IndexReader reader = searcher.getRawReader();
    for (LeafReaderContext context : reader.leaves()) {
      LeafReader leafReader = context.reader();
      assertTrue("unexpected LeafReader class: " + leafReader.getClass().getName(), leafReader instanceof CodecReader);
      Bits liveDocs = leafReader.getLiveDocs();
      CodecReader codecReader = (CodecReader) leafReader;
      StoredFieldsReader storedFieldsReader = codecReader.getFieldsReader();
      StoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
      assertNotNull(storedFieldsReader);
      for (int docId = 0; docId < leafReader.maxDoc(); docId++) {
        if (liveDocs != null && !liveDocs.get(docId)) {
          continue;
        }
        storedFieldsReader.visitDocument(docId, visitor);
      }
    }
  } finally {
    searcherRef.decref();
    core.close();
  }
}
 
Example 2
Source File: TestHalfAndHalfDocValues.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testHalfAndHalfDocValues() throws Exception {
  // Insert two docs without docvalues
  String fieldname = "string_add_dv_later";
  assertU(adoc("id", "3", fieldname, "c"));
  assertU(commit());
  assertU(adoc("id", "1", fieldname, "a"));
  assertU(commit());


  try (SolrCore core = h.getCoreInc()) {
    assertFalse(core.getLatestSchema().getField(fieldname).hasDocValues());
    // Add docvalues to the field type
    IndexSchema schema = core.getLatestSchema();
    SchemaField oldField = schema.getField(fieldname);
    int newProperties = oldField.getProperties() | SchemaField.DOC_VALUES;

    SchemaField sf = new SchemaField(fieldname, oldField.getType(), newProperties, null);
    schema.getFields().put(fieldname, sf);

    // Insert a new doc with docvalues
    assertU(adoc("id", "2", fieldname, "b"));
    assertU(commit());


    // Check there are a mix of segments with and without docvalues
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      final DirectoryReader topReader = searcher.getRawReader();

      //Assert no merges

      assertEquals(3, topReader.numDocs());
      assertEquals(3, topReader.leaves().size());

      final FieldInfos infos = FieldInfos.getMergedFieldInfos(topReader);
      //The global field type should have docValues because a document with dvs was added
      assertEquals(DocValuesType.SORTED, infos.fieldInfo(fieldname).getDocValuesType());

      for (LeafReaderContext ctx : topReader.leaves()) {
        LeafReader r = ctx.reader();
        //Make sure there were no merges
        assertEquals(1, r.numDocs());
        Document doc = r.document(0);
        String id = doc.getField("id").stringValue();

        if (id.equals("1") || id.equals("3")) {
          assertEquals(DocValuesType.NONE, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType());
        } else {
          assertEquals(DocValuesType.SORTED, r.getFieldInfos().fieldInfo(fieldname).getDocValuesType());
        }

      }
    } finally {
      searcherRef.decref();
    }
  }

  // Assert sort order is correct
  assertQ(req("q", "string_add_dv_later:*", "sort", "string_add_dv_later asc"),
      "//*[@numFound='3']",
      "//result/doc[1]/str[@name='id'][.=1]",
      "//result/doc[2]/str[@name='id'][.=2]",
      "//result/doc[3]/str[@name='id'][.=3]"
  );
}