Java Code Examples for org.apache.solr.util.RefCounted#get()

The following examples show how to use org.apache.solr.util.RefCounted#get() . 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: SolrCore.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void initSearcher(SolrCore prev) throws IOException {
  // use the (old) writer to open the first searcher
  RefCounted<IndexWriter> iwRef = null;
  if (prev != null) {
    iwRef = prev.getUpdateHandler().getSolrCoreState().getIndexWriter(null);
    if (iwRef != null) {
      final IndexWriter iw = iwRef.get();
      final SolrCore core = this;
      newReaderCreator = () -> indexReaderFactory.newReader(iw, core);
    }
  }

  try {
    getSearcher(false, false, null, true);
  } finally {
    newReaderCreator = null;
    if (iwRef != null) {
      iwRef.decref();
    }
  }
}
 
Example 2
Source File: SegmentsInfoRequestHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SimpleOrderedMap<Object> getMergeInformation(SolrQueryRequest req, SegmentInfos infos, List<String> mergeCandidates) throws IOException {
  SimpleOrderedMap<Object> result = new SimpleOrderedMap<>();
  RefCounted<IndexWriter> refCounted = req.getCore().getSolrCoreState().getIndexWriter(req.getCore());
  try {
    IndexWriter indexWriter = refCounted.get();
    if (indexWriter instanceof SolrIndexWriter) {
      result.addAll(((SolrIndexWriter)indexWriter).getRunningMerges());
    }
    //get chosen merge policy
    MergePolicy mp = indexWriter.getConfig().getMergePolicy();
    //Find merges
    MergeSpecification findMerges = mp.findMerges(MergeTrigger.EXPLICIT, infos, indexWriter);
    if (findMerges != null && findMerges.merges != null && findMerges.merges.size() > 0) {
      for (OneMerge merge : findMerges.merges) {
        //TODO: add merge grouping
        for (SegmentCommitInfo mergeSegmentInfo : merge.segments) {
          mergeCandidates.add(mergeSegmentInfo.info.name);
        }
      }
    }

    return result;
  } finally {
    refCounted.decref();
  }
}
 
Example 3
Source File: RecoveryStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
final private void cloudDebugLog(SolrCore core, String op) {
  if (!log.isDebugEnabled()) {
    return;
  }
  try {
    RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
    SolrIndexSearcher searcher = searchHolder.get();
    try {
      final int totalHits = searcher.count(new MatchAllDocsQuery());
      final String nodeName = core.getCoreContainer().getZkController().getNodeName();
      log.debug("[{}] {} [{} total hits]", nodeName, op, totalHits);
    } finally {
      searchHolder.decref();
    }
  } catch (Exception e) {
    log.debug("Error in solrcloud_debug block", e);
  }
}
 
Example 4
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void allowDuplicateUpdate(AddUpdateCommand cmd) throws IOException {
  RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
  try {
    IndexWriter writer = iw.get();
    Iterable<Document> nestedDocs = cmd.getLuceneDocsIfNested();
    if (nestedDocs != null) {
      writer.addDocuments(nestedDocs);
    } else {
      writer.addDocument(cmd.getLuceneDocument());
    }
    if (ulog != null) ulog.add(cmd);

  } finally {
    iw.decref();
  }

}
 
Example 5
Source File: UninvertDocValuesMergePolicyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void withNewRawReader(TestHarness h, DirectoryReaderConsumer consumer) {
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      try {
        consumer.accept(searcher.getRawReader());
      } catch (Exception e) {
        fail(e.toString());
      }
    } finally {
      searcherRef.decref();
    }
  }
}
 
Example 6
Source File: AuthQueryIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Queries the index and asserts if the count matches documents returned.
 * @param queryString
 * @param count
 * @throws IOException
 * @throws org.apache.lucene.queryparser.classic.ParseException
 */
private void assertFTSQuery(String queryString,
                          int count,
                          String... name) throws IOException, ParseException
{
    SolrServletRequest solrQueryRequest = null;
    RefCounted<SolrIndexSearcher>refCounted = null;
    try
    {
        solrQueryRequest = new SolrServletRequest(getCore(), null);
        refCounted = getCore().getSearcher(false, true, null);
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        
        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setQuery(queryString);
        Query query = dataModel.getFTSQuery(new Pair<SearchParameters, Boolean>(searchParameters, Boolean.FALSE),
                solrQueryRequest, FTSQueryParser.RerankPhase.SINGLE_PASS);
        TopDocs docs = solrIndexSearcher.search(query, count * 2 + 10);
    
        Assert.assertEquals(count, docs.totalHits);
    } 
    finally
    {
        refCounted.decref();
        solrQueryRequest.close();
    }
}
 
Example 7
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void waitForDocCount(Query query, long expectedNumFound, long waitMillis)
        throws Exception
{
    Date date = new Date();
    long timeout = date.getTime() + waitMillis;

    RefCounted<SolrIndexSearcher> ref = null;
    int totalHits = 0;
    while(new Date().getTime() < timeout)
    {
        try
        {
            ref = getCore().getSearcher();
            SolrIndexSearcher searcher = ref.get();
            TopDocs topDocs = searcher.search(query, 10);
            totalHits = topDocs.totalHits;
            if (topDocs.totalHits == expectedNumFound)
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + " as expected");
                return;
            }
            else
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + ", expected " + expectedNumFound);
                Thread.sleep(2000);
            }
        }
        finally
        {
            ref.decref();
        }
    }
    throw new Exception("Wait error expected "+expectedNumFound+" found "+totalHits+" : "+query.toString());
}
 
Example 8
Source File: TestSimpleTextCodec.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  SolrConfig config = h.getCore().getSolrConfig();
  String codecFactory =  config.get("codecFactory/@class");
  assertEquals("Unexpected solrconfig codec factory", "solr.SimpleTextCodecFactory", codecFactory);

  assertEquals("Unexpected core codec", "SimpleText", h.getCore().getCodec().getName());

  RefCounted<IndexWriter> writerRef = h.getCore().getSolrCoreState().getIndexWriter(h.getCore());
  try {
    IndexWriter writer = writerRef.get();
    assertEquals("Unexpected codec in IndexWriter config", 
        "SimpleText", writer.getConfig().getCodec().getName()); 
  } finally {
    writerRef.decref();
  }

  assertU(add(doc("id","1", "text","textual content goes here")));
  assertU(commit());

  h.getCore().withSearcher(searcher -> {
    SegmentInfos infos = SegmentInfos.readLatestCommit(searcher.getIndexReader().directory());
    SegmentInfo info = infos.info(infos.size() - 1).info;
    assertEquals("Unexpected segment codec", "SimpleText", info.getCodec().getName());
    return null;
  });

  assertQ(req("q", "id:1"),
      "*[count(//doc)=1]");
}
 
Example 9
Source File: DocValuesMultiTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocValues() throws IOException {

  final DocValuesType expectedNumericDvType = Boolean.getBoolean(NUMERIC_POINTS_SYSPROP) ?
    DocValuesType.SORTED_NUMERIC : DocValuesType.SORTED_SET;
  
  assertU(adoc("id", "1", "floatdv", "4.5", "intdv", "-1", "intdv", "3",
      "stringdv", "value1", "stringdv", "value2",
      "booldv", "false", "booldv", "true"));
  assertU(commit());
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      final LeafReader reader = searcher.getSlowAtomicReader();
      assertEquals(1, reader.numDocs());
      final FieldInfos infos = reader.getFieldInfos();
      assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("stringdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("booldv").getDocValuesType());
      assertEquals(expectedNumericDvType, infos.fieldInfo("floatdv").getDocValuesType());
      assertEquals(expectedNumericDvType, infos.fieldInfo("intdv").getDocValuesType());

      SortedSetDocValues dv = reader.getSortedSetDocValues("stringdv");
      assertEquals(0, dv.nextDoc());
      assertEquals(0, dv.nextOrd());
      assertEquals(1, dv.nextOrd());
      assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.nextOrd());

      dv = reader.getSortedSetDocValues("booldv");
      assertEquals(0, dv.nextDoc());
      assertEquals(0, dv.nextOrd());
      assertEquals(1, dv.nextOrd());
      assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.nextOrd());


    } finally {
      searcherRef.decref();
    }
  }
}
 
Example 10
Source File: VersionInfo.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the latest version from the index, searched by the given id (bytes) as seen from the realtime searcher.
 * Returns null if no document can be found in the index for the given id.
 */
@SuppressWarnings({"unchecked"})
public Long getVersionFromIndex(BytesRef idBytes) {
  // TODO: we could cache much of this and invalidate during a commit.
  // TODO: most DocValues classes are threadsafe - expose which.

  RefCounted<SolrIndexSearcher> newestSearcher = ulog.uhandler.core.getRealtimeSearcher();
  try {
    SolrIndexSearcher searcher = newestSearcher.get();
    long lookup = searcher.lookupId(idBytes);
    if (lookup < 0) return null; // this means the doc doesn't exist in the index yet

    ValueSource vs = versionField.getType().getValueSource(versionField, null);
    @SuppressWarnings({"rawtypes"})
    Map context = ValueSource.newContext(searcher);
    vs.createWeight(context, searcher);
    FunctionValues fv = vs.getValues(context, searcher.getTopReaderContext().leaves().get((int) (lookup >> 32)));
    long ver = fv.longVal((int) lookup);
    return ver;

  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
  } finally {
    if (newestSearcher != null) {
      newestSearcher.decref();
    }
  }
}
 
Example 11
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private DocFoundAndOldUserAndSolrVersions getOldUserVersionsFromFieldCache(BytesRef indexedDocId) {
  SolrInputDocument oldDoc = RealTimeGetComponent.getInputDocumentFromTlog(core, indexedDocId, null, null, true);
  if (oldDoc == RealTimeGetComponent.DELETED) {
    return DocFoundAndOldUserAndSolrVersions.NOT_FOUND;
  }
  if (oldDoc == null) {
    // need to look up in index now...
    RefCounted<SolrIndexSearcher> newestSearcher = core.getRealtimeSearcher();
    try {
      SolrIndexSearcher searcher = newestSearcher.get();
      long lookup = searcher.lookupId(indexedDocId);
      if (lookup < 0) {
        // doc not in index either...
        return DocFoundAndOldUserAndSolrVersions.NOT_FOUND;
      }
      final LeafReaderContext segmentContext = searcher.getTopReaderContext().leaves().get((int)(lookup>>32));
      final int docIdInSegment = (int)lookup;

      long oldSolrVersion = getFunctionValues(segmentContext, solrVersionField, searcher).longVal(docIdInSegment);
      Object[] oldUserVersions = getObjectValues(segmentContext, userVersionFields, searcher, docIdInSegment);
      return new DocFoundAndOldUserAndSolrVersions(oldUserVersions, oldSolrVersion);
    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
    } finally {
      if (newestSearcher != null) { //TODO can this ever be null?
        newestSearcher.decref();
      }
    }
  } else {
    return getUserVersionAndSolrVersionFromDocument(oldDoc);
  }
}
 
Example 12
Source File: DocValuesTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocValues() throws IOException {
  assertU(adoc("id", "1"));
  assertU(commit());
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      final LeafReader reader = searcher.getSlowAtomicReader();
      assertEquals(1, reader.numDocs());
      final FieldInfos infos = reader.getFieldInfos();
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("floatdv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("intdv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("doubledv").getDocValuesType());
      assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("longdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED, infos.fieldInfo("stringdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED, infos.fieldInfo("booldv").getDocValuesType());

      NumericDocValues dvs = reader.getNumericDocValues("floatdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals((long) Float.floatToIntBits(1), dvs.longValue());
      dvs = reader.getNumericDocValues("intdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(2L, dvs.longValue());
      dvs = reader.getNumericDocValues("doubledv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(Double.doubleToLongBits(3), dvs.longValue());
      dvs = reader.getNumericDocValues("longdv");
      assertEquals(0, dvs.nextDoc());
      assertEquals(4L, dvs.longValue());
      SortedDocValues sdv = reader.getSortedDocValues("stringdv");
      assertEquals(0, sdv.nextDoc());
      assertEquals("solr", sdv.binaryValue().utf8ToString());
      sdv = reader.getSortedDocValues("booldv");
      assertEquals(0, sdv.nextDoc());
      assertEquals("T", sdv.binaryValue().utf8ToString());

      final IndexSchema schema = core.getLatestSchema();
      final SchemaField floatDv = schema.getField("floatdv");
      final SchemaField intDv = schema.getField("intdv");
      final SchemaField doubleDv = schema.getField("doubledv");
      final SchemaField longDv = schema.getField("longdv");
      final SchemaField boolDv = schema.getField("booldv");

      FunctionValues values = floatDv.getType().getValueSource(floatDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(1f, values.floatVal(0), 0f);
      assertEquals(1f, values.objectVal(0));
      values = intDv.getType().getValueSource(intDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(2, values.intVal(0));
      assertEquals(2, values.objectVal(0));
      values = doubleDv.getType().getValueSource(doubleDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(3d, values.doubleVal(0), 0d);
      assertEquals(3d, values.objectVal(0));
      values = longDv.getType().getValueSource(longDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals(4L, values.longVal(0));
      assertEquals(4L, values.objectVal(0));
      
      values = boolDv.getType().getValueSource(boolDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
      assertEquals("true", values.strVal(0));
      assertEquals(true, values.objectVal(0));

      // check reversibility of created fields
      tstToObj(schema.getField("floatdv"), -1.5f);
      tstToObj(schema.getField("floatdvs"), -1.5f);
      tstToObj(schema.getField("doubledv"), -1.5d);
      tstToObj(schema.getField("doubledvs"), -1.5d);
      tstToObj(schema.getField("intdv"), -7);
      tstToObj(schema.getField("intdvs"), -7);
      tstToObj(schema.getField("longdv"), -11L);
      tstToObj(schema.getField("longdvs"), -11L);
      tstToObj(schema.getField("datedv"), new Date(1000));
      tstToObj(schema.getField("datedvs"), new Date(1000));
      tstToObj(schema.getField("stringdv"), "foo");
      tstToObj(schema.getField("stringdvs"), "foo");
      tstToObj(schema.getField("booldv"), true);
      tstToObj(schema.getField("booldvs"), true);

    } finally {
      searcherRef.decref();
    }
  }
}
 
Example 13
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean commit(boolean openSearcher) throws IOException
{
    canUpdate();

    UpdateRequestProcessor processor = null;
    boolean searcherOpened = false;
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, newSolrQueryResponse());
        CommitUpdateCommand command = new CommitUpdateCommand(request, false);
        if (openSearcher)
        {
            RefCounted<SolrIndexSearcher> active = null;
            RefCounted<SolrIndexSearcher> newest = null;
            try
            {
                active = core.getSearcher();
                newest = core.getNewestSearcher(false);
                if (active.get() == newest.get())
                {
                    searcherOpened = command.openSearcher = true;
                    command.waitSearcher = false;
                }
                else
                {
                    searcherOpened = command.openSearcher = false;
                }
            }
            finally
            {
                ofNullable(active).ifPresent(RefCounted::decref);
                ofNullable(newest).ifPresent(RefCounted::decref);
            }
        }
        processor.processCommit(command);
    }
    finally
    {
        if (processor != null)
        {
            processor.finish();
        }
    }

    return searcherOpened;
}
 
Example 14
Source File: SplitOp.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 *   This is called when splitByPrefix is used.
 *   The overseer called us to get recommended splits taking into
 *   account actual document distribution over the hash space.
 */
private void handleGetRanges(CoreAdminHandler.CallInfo it, String coreName) throws Exception {

  SolrCore parentCore = it.handler.coreContainer.getCore(coreName);
  if (parentCore == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown core " + coreName);
  }

  RefCounted<SolrIndexSearcher> searcherHolder = parentCore.getRealtimeSearcher();

  try {
    if (!it.handler.coreContainer.isZooKeeperAware()) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Shard splitByPrefix requires SolrCloud mode.");
    } else {
      SolrIndexSearcher searcher = searcherHolder.get();

      String routeFieldName = null;
      String prefixField = "id_prefix";

      ClusterState clusterState = it.handler.coreContainer.getZkController().getClusterState();
      String collectionName = parentCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
      DocCollection collection = clusterState.getCollection(collectionName);
      String sliceName = parentCore.getCoreDescriptor().getCloudDescriptor().getShardId();
      Slice slice = collection.getSlice(sliceName);
      DocRouter router = collection.getRouter() != null ? collection.getRouter() : DocRouter.DEFAULT;
      DocRouter.Range currentRange = slice.getRange();

      Object routerObj = collection.get(DOC_ROUTER); // for back-compat with Solr 4.4
      if (routerObj instanceof Map) {
        @SuppressWarnings({"rawtypes"})
        Map routerProps = (Map) routerObj;
        routeFieldName = (String) routerProps.get("field");
      }
      if (routeFieldName == null) {
        routeFieldName = searcher.getSchema().getUniqueKeyField().getName();
      }

      Collection<RangeCount> counts = getHashHistogram(searcher, prefixField, router, collection);

      if (counts.size() == 0) {
        // How to determine if we should look at the id field to figure out the prefix buckets?
        // There may legitimately be no indexed terms in id_prefix if no ids have a prefix yet.
        // For now, avoid using splitByPrefix unless you are actually using prefixes.
        counts = getHashHistogramFromId(searcher, searcher.getSchema().getUniqueKeyField().getName(), router, collection);
      }

      Collection<DocRouter.Range> splits = getSplits(counts, currentRange);
      String splitString = toSplitString(splits);

      if (splitString == null) {
        return;
      }

      it.rsp.add(CoreAdminParams.RANGES, splitString);
    }
  } finally {
    if (searcherHolder != null) searcherHolder.decref();
    if (parentCore != null) parentCore.close();
  }
}
 
Example 15
Source File: RequestSyncShardOp.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CallInfo it) throws Exception {
  final SolrParams params = it.req.getParams();

  log.info("I have been requested to sync up my shard");

  String cname = params.required().get(CoreAdminParams.CORE);

  ZkController zkController = it.handler.coreContainer.getZkController();
  if (zkController == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Only valid for SolrCloud");
  }

  SyncStrategy syncStrategy = null;
  try (SolrCore core = it.handler.coreContainer.getCore(cname)) {

    if (core != null) {
      syncStrategy = new SyncStrategy(core.getCoreContainer());

      Map<String, Object> props = new HashMap<>();
      props.put(ZkStateReader.BASE_URL_PROP, zkController.getBaseUrl());
      props.put(ZkStateReader.CORE_NAME_PROP, cname);
      props.put(ZkStateReader.NODE_NAME_PROP, zkController.getNodeName());

      boolean success = syncStrategy.sync(zkController, core, new ZkNodeProps(props), true).isSuccess();
      // solrcloud_debug
      if (log.isDebugEnabled()) {
        try {
          RefCounted<SolrIndexSearcher> searchHolder = core
              .getNewestSearcher(false);
          SolrIndexSearcher searcher = searchHolder.get();
          try {
            if (log.isDebugEnabled()) {
              log.debug("{} synched {}", core.getCoreContainer().getZkController().getNodeName()
                  , searcher.count(new MatchAllDocsQuery()));
            }
          } finally {
            searchHolder.decref();
          }
        } catch (Exception e) {
          log.debug("Error in solrcloud_debug block", e);
        }
      }
      if (!success) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Sync Failed");
      }
    } else {
      SolrException.log(log, "Could not find core to call sync:" + cname);
    }
  } finally {
    // no recoveryStrat close for now
    if (syncStrategy != null) {
      syncStrategy.close();
    }
  }
}
 
Example 16
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void assertAQuery(
        String queryString,
        int count,
        Locale locale,
        String[] textAttributes,
        String[] allAttributes,
        String... name)
{
    RefCounted<SolrIndexSearcher>refCounted = null;
    try (SolrServletRequest solrQueryRequest = new SolrServletRequest(getCore(), null))
    {
        refCounted = getCore().getSearcher();
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setQuery(queryString);
        if (locale != null)
        {
            searchParameters.addLocale(locale);
        }

        if (textAttributes != null)
        {
            for (String textAttribute : textAttributes)
            {
                searchParameters.addTextAttribute(textAttribute);
        }
    }
    if (allAttributes != null)
    {
        for (String allAttribute : allAttributes)
        {
            searchParameters.addAllAttribute(allAttribute);
        }
    }

    Query query = dataModel.getLuceneQueryParser(searchParameters, solrQueryRequest, FTSQueryParser.RerankPhase.SINGLE_PASS).parse(queryString);
    LOG.debug("####### Query ######:"+query);
    TopDocs docs = solrIndexSearcher.search(query, count * 2 + 10);

        assertEquals(fixQueryString(queryString, name), count, docs.totalHits);
    }
    catch(Exception exception)
    {
        exception.printStackTrace();
        throw new RuntimeException(exception);
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
}
 
Example 17
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 18
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<Transaction> getCascades(int num) throws IOException
{
    RefCounted<SolrIndexSearcher> refCounted = null;
    try
    {
        refCounted = this.core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();

        Collector collector;

        TopFieldCollector topFieldCollector = TopFieldCollector.create(new Sort(new SortField(FIELD_TXID, SortField.Type.LONG)),
                                                                        num,
                                                                        null,
                                                                        false,
                                                                        false,
                                                                        false);

        collector = topFieldCollector;

        LegacyNumericRangeQuery q = LegacyNumericRangeQuery.newIntRange(FIELD_CASCADE_FLAG, 1, 1, true, true);
        DelegatingCollector delegatingCollector = new TxnCacheFilter(cleanCascadeCache);

        delegatingCollector.setLastDelegate(collector);
        collector = delegatingCollector;

        searcher.search(q, collector);
        ScoreDoc[] scoreDocs = topFieldCollector.topDocs().scoreDocs;

        Set<String> fields = new HashSet<>();
        fields.add(FIELD_S_TXID);
        fields.add(FIELD_S_TXCOMMITTIME);

        List<Transaction> transactions = new ArrayList<>(scoreDocs.length);

        for(ScoreDoc scoreDoc : scoreDocs)
        {
            Transaction transaction = new Transaction();
            Document doc = searcher.doc(scoreDoc.doc, fields);

            IndexableField txID = doc.getField(FIELD_S_TXID);
            long txnID = txID.numericValue().longValue();
            cleanCascadeCache.put(txnID, null);
            transaction.setId(txnID);

            IndexableField txnCommitTime = doc.getField(FIELD_S_TXCOMMITTIME);
            transaction.setCommitTimeMs(txnCommitTime.numericValue().longValue());

            transactions.add(transaction);
        }

        return transactions;
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
}
 
Example 19
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<NodeMetaData> getCascadeNodes(List<Long> txnIds) throws IOException, JSONException
{
    List<FieldInstance> list = dataModel.getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
    FieldInstance fieldInstance = list.get(0);

    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList;
    Set<Long> parentNodesId = new HashSet<>();

    try
    {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        String field = fieldInstance.getField();
        SchemaField schemaField = searcher.getSchema().getField(field);
        FieldType fieldType = schemaField.getType();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery booleanQuery;

        for(Long l : txnIds)
        {
            BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
            fieldType.readableToIndexed(l.toString(), bytesRefBuilder);
            TermQuery termQuery = new TermQuery(new Term(field, bytesRefBuilder.toBytesRef()));
            BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.SHOULD);
            builder.add(booleanClause);
        }

        booleanQuery = builder.build();

        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        int size = docList.size();
        for(int i=0; i<size; i++)
        {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, REQUEST_ONLY_ID_FIELD);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            parentNodesId.add(ids.dbId);
        }
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }

    List<NodeMetaData> allNodeMetaDatas = new ArrayList<>();

    for (Long parentNodeId : parentNodesId)
    {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(parentNodeId);
        nmdp.setToNodeId(parentNodeId);
        nmdp.setIncludeAclId(true);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeTxnId(true);
        nmdp.setMaxResults(1);
        // Gets only one
        Optional<Collection<NodeMetaData>> nodeMetaDatas = getNodesMetaDataFromRepository(nmdp);
        allNodeMetaDatas.addAll(nodeMetaDatas.orElse(Collections.emptyList()));
    }

    return allNodeMetaDatas;
}
 
Example 20
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]"
  );
}