Example usage for org.apache.solr.core SolrCore getSearcher

List of usage examples for org.apache.solr.core SolrCore getSearcher

Introduction

In this page you can find the example usage for org.apache.solr.core SolrCore getSearcher.

Prototype

public RefCounted<SolrIndexSearcher> getSearcher() 

Source Link

Document

Return a registered RefCounted < SolrIndexSearcher > with the reference count incremented.

Usage

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testBasics() throws Exception {
    System.err.println(cc.getAllCoreNames());
    populate();//w  ww .  j a v a2 s  .c  o  m
    SolrCore target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 101);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            String[] vals = doc.getValues("id");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            String id = vals[0];
            vals = doc.getValues("text");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            if (!id.equals("id100")) {
                // should have also the sidecar fields
                vals = doc.getValues("side1_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
                vals = doc.getValues("side2_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
            } else {
                // should not have the sidecar fields
                vals = doc.getValues("side1_t");
                assertTrue(vals == null || vals.length == 0);
                vals = doc.getValues("side2_t");
                assertTrue(vals == null || vals.length == 0);
            }
        }
        // verify the inverted parts
        q = new TermQuery(new Term("side1_t", "foo"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
        q = new TermQuery(new Term("side2_t", "blah"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testChanges() throws Exception {
    populate();/*from   w w  w .  jav a 2  s.co  m*/
    // add some docs, overwriting some of the existing ones
    SolrCore target = cc.getCore("target");
    try {
        for (int i = 50; i < 150; i++) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(target));
            cmd.overwrite = true;
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("text", "new document " + i);
            cmd.solrDoc = doc;
            target.getUpdateHandler().addDoc(cmd);
        }
        target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
    } finally {
        target.close();
    }
    target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 151);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            System.err.println(doc);
        }
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:org.alfresco.solr.SolrInformationServer.java

License:Open Source License

private boolean isInIndex(long id, LRU cache, String fieldName, boolean populateCache, SolrCore core)
        throws IOException {
    if (cache.containsKey(id)) {
        return true;
    } else {//w  w  w  . j av  a  2 s  .  c  om
        RefCounted<SolrIndexSearcher> refCounted = null;
        try {
            if (populateCache) {
                cache.put(id, null); // Safe to add this here because we reset this on rollback.
            }
            refCounted = core.getSearcher();
            SolrIndexSearcher searcher = refCounted.get();
            FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
            TermQuery q = new TermQuery(new Term(fieldName, fieldType.readableToIndexed(Long.toString(id))));
            TopDocs topDocs = searcher.search(q, 1);
            return topDocs.totalHits > 0;
        } finally {
            ofNullable(refCounted).ifPresent(RefCounted::decref);
        }
    }
}

From source file:org.vootoo.schema.CrossCoreField.java

License:Apache License

@Override
public ValueSource getValueSource(SchemaField field, QParser parser) {
    SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
    if (info == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Cross-core field [" + field.getName() + "] must have SolrRequestInfo");
    }//from   w  w  w .j  a  va2 s  .  c o m

    String[] crossCore = dumpCrossSchemaField(field);

    SchemaField uniqueKeyField = schema.getUniqueKeyField();

    final SolrCore targetCore = info.getReq().getCore().getCoreDescriptor().getCoreContainer()
            .getCore(crossCore[0]);
    if (targetCore == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Cross-core field [" + field.getName() + "] miss target core [" + crossCore[0] + "]");
    }

    SchemaField targetField = targetCore.getLatestSchema().getFieldOrNull(crossCore[1]);
    if (targetField == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core field [" + field.getName()
                + "] core [" + crossCore[0] + "] miss field [" + crossCore[1] + "]");
    }

    final RefCounted<SolrIndexSearcher> targetCoreSearcher = targetCore.getSearcher();
    SolrIndexSearcher targetSolrIndexSearcher = targetCoreSearcher.get();

    CrossCoreFieldValueSource ccfvs = new CrossCoreFieldValueSource(uniqueKeyField, targetField, parser,
            targetSolrIndexSearcher);

    info.addCloseHook(new Closeable() {
        @Override
        public void close() throws IOException {
            targetCoreSearcher.decref();
            targetCore.close();
        }
    });

    return ccfvs;
}

From source file:uk.co.flax.biosolr.FacetTreeGenerator.java

License:Apache License

/**
 * Get a reference to the searcher for the required collection. If the collection is
 * not the same as the search collection, we assume it is under the same Solr instance.
 * @param rb the response builder holding the facets.
 * @return a counted reference to the searcher.
 * @throws SolrException if the collection cannot be found.
 *//*  ww w  .ja v  a 2s .com*/
private RefCounted<SolrIndexSearcher> getSearcherReference(ResponseBuilder rb) throws SolrException {
    RefCounted<SolrIndexSearcher> searcherRef;

    SolrCore currentCore = rb.req.getCore();
    if (StringUtils.isBlank(collection)) {
        searcherRef = currentCore.getSearcher();
    } else {
        // Using an alternative core - find it
        SolrCore reqCore = currentCore.getCoreDescriptor().getCoreContainer().getCore(collection);
        if (reqCore == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Collection \"" + collection + "\" cannot be found");
        }
        searcherRef = reqCore.getSearcher();
    }

    return searcherRef;
}