Example usage for org.apache.solr.client.solrj StreamingResponseCallback StreamingResponseCallback

List of usage examples for org.apache.solr.client.solrj StreamingResponseCallback StreamingResponseCallback

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj StreamingResponseCallback StreamingResponseCallback.

Prototype

StreamingResponseCallback

Source Link

Usage

From source file:mecha.db.Bucket.java

License:Apache License

public boolean foreach(final MDB.ForEachFunction forEachFunction) throws Exception {
    rates.add("mecha.db.bucket.global.foreach");

    final Semaphore streamSemaphore = new Semaphore(1, true);
    streamSemaphore.acquire();// w ww.  j  a v  a 2 s . co  m

    final String q = "partition:" + partition + " AND bucket:" + bucketStr;

    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    solrParams.set("q", "*:*");
    solrParams.set("fq", q);

    final AtomicBoolean fe_b = new AtomicBoolean(true);

    QueryResponse res = solrServer.queryAndStreamResponse(solrParams, new StreamingResponseCallback() {
        long numFound = -1;
        long count = 0;

        public void streamDocListInfo(long numFound, long start, Float maxScore) {
            //log.info("streamDocListInfo: numFound = " + numFound + ", start = " + start + ", maxScore = " + maxScore);
            this.numFound = numFound;
            if (numFound == 0) {
                streamSemaphore.release();
            }
        }

        public void streamSolrDocument(SolrDocument doc) {
            if (!fe_b.get())
                return;
            count++;
            try {
                final JSONObject solrObj = jsonizeSolrDoc(doc);
                final byte[] key = solrObj.getString("key").getBytes();
                final JSONObject riakObj = makeRiakObject(solrObj);
                final byte[] value = riakObj.toString().getBytes();

                if (!forEachFunction.each(bucket, key, value)) {
                    fe_b.set(false);
                    //log.info("releasing stream semaphore");
                    streamSemaphore.release();
                    return;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            if (count == numFound) {
                //log.info("end of line; automatically releasing stream semaphore");
                streamSemaphore.release();
            }
        }
    });

    streamSemaphore.acquire();
    return fe_b.get();
}

From source file:org.opensextant.extraction.SolrMatcherSupport.java

License:Apache License

/**
 * Solr call: tag input buffer, returning all candiate reference data that
 * matched during tagging./*from   ww  w.ja v a2s . com*/
 *
 * @param buffer text to tag
 * @param docid  id for text, only for tracking purposes
 * @param refDataMap
 *            - a map of reference data in solr, It will store caller's
 *            domain objects. e.g., rec.id => domain(rec)
 * @return solr response
 * @throws ExtractionException tagger error
 */
protected QueryResponse tagTextCallSolrTagger(String buffer, String docid,
        final Map<Integer, Object> refDataMap) throws ExtractionException {
    SolrTaggerRequest tagRequest = new SolrTaggerRequest(getMatcherParameters(), buffer);
    tagRequest.setPath(requestHandler);
    // Stream the response to avoid serialization and to save memory by
    // only keeping one SolrDocument materialized at a time
    tagRequest.setStreamingResponseCallback(new StreamingResponseCallback() {
        @Override
        public void streamDocListInfo(long numFound, long start, Float maxScore) {
        }

        // Future optimization: it would be nice if Solr could give us the
        // doc id without giving us a SolrDocument, allowing us to
        // conditionally get it. It would save disk IO & speed, at the
        // expense of putting ids into memory.
        @Override
        public void streamSolrDocument(final SolrDocument solrDoc) {
            Integer id = (Integer) solrDoc.getFirstValue("id");
            // create a domain object for the given tag;
            // this callback handler caches such domain obj in simple k/v
            // map.
            Object domainObj = createTag(solrDoc);
            if (domainObj != null) {
                refDataMap.put(id, domainObj);
            }
        }
    });

    QueryResponse response;
    try {
        response = tagRequest.process(solr.getInternalSolrServer());
    } catch (Exception err) {
        throw new ExtractionException("Failed to tag document=" + docid, err);
    }

    // see https://issues.apache.org/jira/browse/SOLR-5154
    SolrDocumentList docList = response.getResults();
    if (docList != null) {
        // log.debug("Not streaming docs from Solr (not supported)");
        StreamingResponseCallback callback = tagRequest.getStreamingResponseCallback();
        callback.streamDocListInfo(docList.getNumFound(), docList.getStart(), docList.getMaxScore());
        for (SolrDocument solrDoc : docList) {
            /**
             * This appears to be an empty list; what is this explicit
             * callback loop for?
             */
            callback.streamSolrDocument(solrDoc);
        }
    }

    return response;
}

From source file:org.opensextant.solrtexttagger.EmbeddedSolrNoSerializeTest.java

License:Open Source License

@Test
public void testTagStreaming() throws IOException, SolrServerException {
    ModifiableSolrParams params = params();
    String input = "foo boston bar";//just one tag;
    QueryRequest req = new SolrTaggerRequest(params, input);
    req.setPath("/tag");

    final AtomicReference<SolrDocument> refDoc = new AtomicReference<>();
    req.setStreamingResponseCallback(new StreamingResponseCallback() {
        @Override//from w ww. j  av a  2 s.  c o  m
        public void streamSolrDocument(SolrDocument doc) {
            refDoc.set(doc);
        }

        @Override
        public void streamDocListInfo(long numFound, long start, Float maxScore) {

        }
    });
    QueryResponse rsp = req.process(solrServer);
    assertNotNull(rsp.getResponse().get("tags"));
    assertNotNull(refDoc.get());
    assertEquals("Boston", ((Field) refDoc.get().getFieldValue("name")).stringValue());
}

From source file:org.zenoss.zep.index.impl.solr.SolrEventIndexBackend.java

License:Open Source License

@Override
protected void searchEventTagSeverities(EventFilter filter, final EventTagSeverityCounter counter)
        throws ZepException {
    assertReady();//from  w ww . ja va  2 s  .  c o  m
    if (filter.getTagFilterCount() == 0) {
        SolrQuery solrQuery = buildSolrQuery(filter, null, null, null, null, SolrFieldFilter.DEFAULTS);
        solrQuery.setRows(0);
        solrQuery.setFields();
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        solrQuery.setFacetLimit(-1);
        solrQuery.addFacetPivotField(IndexConstants.FIELD_ELEMENT_IDENTIFIER, IndexConstants.FIELD_SEVERITY,
                IndexConstants.FIELD_STATUS, IndexConstants.FIELD_COUNT);
        try {
            QueryResponse response = queryServer.query(solrQuery);
            for (PivotField pivotElementId : response.getFacetPivot().getVal(0)) {
                final String uuid = (String) pivotElementId.getValue();
                for (PivotField pivotSeverity : pivotElementId.getPivot()) {
                    final EventSeverity severity = EventSeverity
                            .valueOf(Integer.parseInt((String) pivotSeverity.getValue()));
                    for (PivotField pivotStatus : pivotSeverity.getPivot()) {
                        final EventStatus status = EventStatus
                                .valueOf(Integer.parseInt((String) pivotStatus.getValue()));
                        final boolean acknowledged = EventStatus.STATUS_ACKNOWLEDGED.equals(status);
                        for (PivotField pivotCount : pivotStatus.getPivot()) {
                            final Integer count = pivotCount.getCount() * (Integer) pivotCount.getValue();
                            counter.update(uuid, severity, count, acknowledged);
                        }
                    }
                }
            }
        } catch (SolrServerException e) {
            throw new ZepException(e);
        }
    } else {
        SolrQuery solrQuery = buildSolrQuery(filter, null, null, null, null,
                SolrFieldFilter.SEARCH_EVENT_TAG_SEVERITIES);
        try {
            queryServer.queryAndStreamResponse(solrQuery, new StreamingResponseCallback() {
                @Override
                public void streamSolrDocument(SolrDocument doc) {
                    final EventSeverity severity = EventSeverity
                            .valueOf(Integer.parseInt((String) doc.getFieldValue(FIELD_SEVERITY)));
                    final EventStatus status = EventStatus
                            .valueOf(Integer.parseInt((String) doc.getFieldValue(FIELD_STATUS)));
                    final boolean acknowledged = EventStatus.STATUS_ACKNOWLEDGED.equals(status);
                    final int count = Integer.parseInt((String) doc.getFieldValue(FIELD_COUNT));
                    for (String fieldName : new String[] { FIELD_ELEMENT_IDENTIFIER,
                            FIELD_ELEMENT_SUB_IDENTIFIER }) {
                        final String uuid = (String) doc.getFieldValue(fieldName);
                        counter.update(uuid, severity, count, acknowledged);
                    }
                    for (Object tag : doc.getFieldValues(FIELD_TAGS)) {
                        counter.update((String) tag, severity, count, acknowledged);
                    }
                }

                @Override
                public void streamDocListInfo(long numFound, long start, Float maxScore) {
                    // ignored
                }
            });
        } catch (SolrServerException e) {
            throw new ZepException(e);
        } catch (IOException e) {
            throw new ZepException(e);
        }
    }
}