Example usage for org.apache.solr.common SolrDocumentList setNumFound

List of usage examples for org.apache.solr.common SolrDocumentList setNumFound

Introduction

In this page you can find the example usage for org.apache.solr.common SolrDocumentList setNumFound.

Prototype

public void setNumFound(long numFound) 

Source Link

Usage

From source file:at.pagu.soldockr.core.SolrTemplateTest.java

License:Apache License

@Test
public void testCount() throws SolrServerException {
    ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
    QueryResponse responseMock = Mockito.mock(QueryResponse.class);
    SolrDocumentList resultList = new SolrDocumentList();
    resultList.setNumFound(10);
    Mockito.when(responseMock.getResults()).thenReturn(resultList);
    Mockito.when(solrServerMock.query(Mockito.any(SolrQuery.class))).thenReturn(responseMock);

    long result = solrTemplate.executeCount(new SimpleQuery(new Criteria("field_1").is("value1")));
    Assert.assertEquals(resultList.getNumFound(), result);

    Mockito.verify(solrServerMock, Mockito.times(1)).query(captor.capture());

    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getStart());
    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getRows());
}

From source file:at.pagu.soldockr.core.SolrTemplateTest.java

License:Apache License

@Test
public void testCountWhenPagingSet() throws SolrServerException {
    ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
    QueryResponse responseMock = Mockito.mock(QueryResponse.class);
    SolrDocumentList resultList = new SolrDocumentList();
    resultList.setNumFound(10);
    Mockito.when(responseMock.getResults()).thenReturn(resultList);
    Mockito.when(solrServerMock.query(Mockito.any(SolrQuery.class))).thenReturn(responseMock);

    Query query = new SimpleQuery(new Criteria("field_1").is("value1"));
    query.setPageRequest(new PageRequest(0, 5));
    long result = solrTemplate.executeCount(query);
    Assert.assertEquals(resultList.getNumFound(), result);

    Mockito.verify(solrServerMock, Mockito.times(1)).query(captor.capture());

    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getStart());
    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getRows());
}

From source file:com.basho.yokozuna.handler.EntropyData.java

License:Open Source License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
        throws Exception, InstantiationException, IllegalAccessException {

    String contParam = req.getParams().get("continue");
    BytesRef cont = contParam != null ? decodeCont(contParam) : DEFAULT_CONT;

    // TODO: Make before required in handler config
    String before = req.getParams().get("before");
    if (before == null) {
        throw new Exception("Parameter 'before' is required");
    }// w  w  w.ja v a 2s  .c o  m
    int n = req.getParams().getInt("n", DEFAULT_N);
    SolrDocumentList docs = new SolrDocumentList();

    // Add docs here and modify object inline in code
    rsp.add("response", docs);

    try {
        SolrIndexSearcher searcher = req.getSearcher();
        AtomicReader rdr = searcher.getAtomicReader();
        BytesRef tmp = null;
        Terms terms = rdr.terms(ENTROPY_DATA_FIELD);
        TermsEnum te = terms.iterator(null);

        if (isContinue(cont)) {
            log.debug("continue from " + cont);

            TermsEnum.SeekStatus status = te.seekCeil(cont, true);

            if (status == TermsEnum.SeekStatus.END) {
                rsp.add("more", false);
                return;
            } else if (status == TermsEnum.SeekStatus.FOUND) {
                // If this term has already been seen then skip it.
                tmp = te.next();

                if (endOfItr(tmp)) {
                    rsp.add("more", false);
                    return;
                }
            } else if (status == TermsEnum.SeekStatus.NOT_FOUND) {
                tmp = te.next();
            }
        } else {
            tmp = te.next();
        }

        String text = null;
        String[] vals = null;
        String ts = null;
        String docId = null;
        String vectorClock = null;
        int count = 0;
        BytesRef current = null;

        while (!endOfItr(tmp) && count < n) {
            current = BytesRef.deepCopyOf(tmp);
            text = tmp.utf8ToString();
            log.debug("text: " + text);
            vals = text.split(" ");
            ts = vals[0];

            // TODO: what if null?
            if (!(ts.compareTo(before) < 0)) {
                rsp.add("more", false);
                docs.setNumFound(count);
                return;
            }

            docId = vals[1];
            vectorClock = vals[2];
            SolrDocument tmpDoc = new SolrDocument();
            tmpDoc.addField("doc_id", docId);
            tmpDoc.addField("base64_vclock", Base64.encodeBase64String(sha(vectorClock)));
            docs.add(tmpDoc);
            count++;
            tmp = te.next();
        }

        if (count < n) {
            rsp.add("more", false);
        } else {
            rsp.add("more", true);
            String newCont = Base64.encodeBase64URLSafeString(current.bytes);
            // The continue context for next req to start where
            // this one finished.
            rsp.add("continuation", newCont);
        }

        docs.setNumFound(count);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

@SuppressWarnings("unchecked")
private static SolrDocumentList parse(Element rootElement) throws IOException {
    SolrDocumentList solrDocumentList = new SolrDocumentList();
    Element channelElement = rootElement.element("channel");
    String totalResultsStr = channelElement.elementText(new QName("totalResults", NS_OPENSEARCH));
    String startIndexStr = channelElement.elementText(new QName("startIndex", NS_OPENSEARCH));
    long numFound = Long.parseLong(totalResultsStr);
    long start = Long.parseLong(startIndexStr);
    solrDocumentList.setNumFound(numFound);
    solrDocumentList.setStart(start);/*from  w  w  w . ja v a 2 s.c o  m*/

    for (Iterator<Element> it = channelElement.elementIterator("item"); it.hasNext();) {
        Element itemElement = it.next();
        String title = itemElement.elementText("title");
        String description = itemElement.elementText("description");
        String link = itemElement.elementText("link");

        title = CharSetUtils.convert(title, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);
        description = CharSetUtils.convert(description, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);
        link = CharSetUtils.convert(link, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);

        SolrDocument solrDocument = new SolrDocument();
        solrDocument.addField("title", title);
        solrDocument.addField("description", description);
        solrDocument.addField("link", link);
        solrDocumentList.add(solrDocument);
    }
    return solrDocumentList;
}

From source file:com.doculibre.constellio.utils.NamedListUtilsTest.java

License:Open Source License

/**
 * The conversion of a SolrDocument and his NamedList form should be equal.
 * Servlets use the NamedList form// w w w  .  j a  v  a  2  s .  c o  m
 * 
 * @throws IOException
 */
@Test
public void testSolrDocumentListVSNamedList() throws IOException {
    NamedList<Object> l1 = new NamedList<Object>();
    SolrDocumentList l = new SolrDocumentList();
    SolrDocument d = new SolrDocument();
    d.setField("a", 123);
    d.setField("myArray", Arrays.asList(new String[] { "A", "B", "C" }));
    d.setField("title", "");
    l.add(d);
    l.setStart(22);
    l.setNumFound(1);
    l1.add(ServletsConstants.RESPONSE, l);

    NamedList<Object> l2 = new NamedList<Object>();
    NamedList<Object> nl = new NamedList<Object>();
    NamedList<Object> attr = new NamedList<Object>();
    attr.add("numFound", 1);
    attr.add("start", 22);
    nl.add("attr", attr);
    l2.add(ServletsConstants.RESPONSE, nl);
    NamedList<Object> nlDoc = new NamedList<Object>();
    nlDoc.add("a", 123);
    nlDoc.add("myArray", Arrays.asList(new String[] { "A", "B", "C" }));
    nlDoc.add("title", "");
    nl.add("doc", nlDoc);

    File tempFile1 = File.createTempFile("temp", ".xml");

    NamedListUtils.convertResponseNamedListToXML(l1, new FileOutputStream(tempFile1));
    String xml1 = readFileAsString(tempFile1);

    File tempFile2 = File.createTempFile("temp", ".xml");

    NamedListUtils.convertResponseNamedListToXML(l2, new FileOutputStream(tempFile2));
    String xml2 = readFileAsString(tempFile2);
    TestCase.assertEquals(xml1, xml2);
}

From source file:com.francelabs.datafari.statistics.StatsProcessor.java

License:Apache License

public static void processStatsResponse(final QueryResponse queryResponse) throws Exception {
    final NamedList responseHeader = queryResponse.getResponseHeader();
    final FacetField QFacet = queryResponse.getFacetField("q");

    final Long numTot = queryResponse.getResults().getNumFound();

    final SolrDocumentList solrDocumentList = new SolrDocumentList();
    solrDocumentList.setNumFound(QFacet.getValueCount());
    solrDocumentList.setStart(0);// w ww .j av  a 2s .  c  o  m

    if (numTot != 0) {
        final Map<String, FieldStatsInfo> stats = queryResponse.getFieldStatsInfo();
        final List<FieldStatsInfo> noHitsStats = stats.get("noHits").getFacets().get("q");
        final List<FieldStatsInfo> QTimeStats = stats.get("QTime").getFacets().get("q");
        List<FieldStatsInfo> positionClickTotStats = null;
        try {
            positionClickTotStats = stats.get("positionClickTot").getFacets().get("q");
        } catch (final Exception e) {

        }
        final List<FieldStatsInfo> clickStats = stats.get("click").getFacets().get("q");
        final List<FieldStatsInfo> numClicksStats = stats.get("numClicks").getFacets().get("q");
        final List<FieldStatsInfo> numFoundStats = stats.get("numFound").getFacets().get("q");

        final List<Count> QFacetValues = QFacet.getValues();

        final Map<String, SolrDocument> mapDocuments = new HashMap<String, SolrDocument>();

        for (int i = 0; i < QFacetValues.size(); i++) {
            final SolrDocument doc = new SolrDocument();
            final String query = QFacetValues.get(i).getName();

            final double count = QFacetValues.get(i).getCount();
            final double frequency = StatsUtils.round(count * 100 / numTot, 2, BigDecimal.ROUND_HALF_UP);

            doc.addField("query", query);

            doc.addField("count", count);
            doc.addField("frequency", frequency);
            mapDocuments.put(query, doc);
            solrDocumentList.add(doc);
        }

        for (int i = 0; i < QTimeStats.size(); i++) {
            final String query = QTimeStats.get(i).getName();
            final SolrDocument doc = mapDocuments.get(query);

            final int AVGHits = new Double((Double) numFoundStats.get(i).getMean()).intValue();
            final Double noHits = new Double((Double) noHitsStats.get(i).getSum());
            final int AVGQTime = new Double((Double) QTimeStats.get(i).getMean()).intValue();
            final int MAXQTime = new Double((Double) QTimeStats.get(i).getMax()).intValue();
            final double click = new Double((Double) clickStats.get(i).getSum());
            final double clickRatio = StatsUtils.round(click * 100 / (Double) doc.getFirstValue("count"), 2,
                    BigDecimal.ROUND_HALF_UP);
            if (click > 0) {
                final double AVGClickPosition = new Double((Double) positionClickTotStats.get(i).getSum()
                        / (Double) numClicksStats.get(i).getSum()).intValue();

                doc.addField("AVGClickPosition", AVGClickPosition);

            } else {
                doc.addField("AVGClickPosition", "-");
            }

            doc.addField("withClickRatio", clickRatio);
            doc.addField("AVGHits", AVGHits);
            doc.addField("numNoHits", noHits);
            doc.addField("withClick", click);
            doc.addField("AVGQTime", AVGQTime);
            doc.addField("MaxQTime", MAXQTime);
        }

    }

    final NamedList<Object> response = new SimpleOrderedMap<Object>();
    response.add("responseHeader", responseHeader);
    response.add("response", solrDocumentList);
    queryResponse.setResponse(response);
}

From source file:com.idealista.solrmeter.BaseTestCase.java

License:Apache License

/**
 * Creates a query response/*from ww w .ja v  a2 s .  c  o m*/
 * @param qTime
 * @return
 */
protected QueryResponse createQueryResponse(int qTime) {
    QueryResponse response = new QueryResponse();
    NamedList<Object> headerNamedList = new NamedList<Object>();
    headerNamedList.add("QTime", qTime);
    NamedList<Object> responseNamedList = new NamedList<Object>();
    responseNamedList.add("responseHeader", headerNamedList);

    SolrDocumentList resultsNamedList = new SolrDocumentList();
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.setNumFound(10);
    responseNamedList.add("response", resultsNamedList);
    response.setResponse(responseNamedList);
    return response;
}

From source file:com.sn.solr.plugin.common.SolrHelper.java

License:Apache License

/**
 * Constructs {@link SolrDocumentList} from the current {@link SolrQueryRequest}
 * and {@link SolrQueryResponse}.//  w w w .j  a  v  a2  s. c  o m
 * 
 * @param req {@link SolrQueryRequest}
 * @param res {@link SolrQueryResponse}
 * @return
 * @throws CorruptIndexException
 * @throws IOException
 */
public static SolrDocumentList getSolrDocList(SolrQueryRequest req, SolrQueryResponse res)
        throws CorruptIndexException, IOException {
    DocSlice slice = (DocSlice) res.getValues().get(RESP_EL_TAG);
    Set<String> returnFields = SolrHelper.getReturnFields(req);
    SolrDocumentList docList = new SolrDocumentList();
    for (DocIterator it = slice.iterator(); it.hasNext();) {
        int docId = it.nextDoc();
        Document doc = req.getSearcher().getReader().document(docId);
        SolrDocument sdoc = new SolrDocument();
        for (Fieldable f : doc.getFields()) {
            String fn = f.name();
            if (returnFields.contains(fn)) {
                sdoc.addField(fn, doc.get(fn));
            }
        }
        docList.add(sdoc);
    }
    docList.setMaxScore(slice.maxScore());
    docList.setNumFound(slice.matches());
    docList.setStart(slice.offset());
    return docList;
}

From source file:com.sn.solr.utils.common.SolrHelper.java

License:Apache License

/**
 * Constructs {@link SolrDocumentList} from the current {@link SolrQueryRequest}
 * and {@link SolrQueryResponse}./*from w w w  .j a v  a 2s .com*/
 * 
 * @param req {@link SolrQueryRequest}
 * @param res {@link SolrQueryResponse}
 * @return
 * @throws CorruptIndexException
 * @throws IOException
 */
public static SolrDocumentList getSolrDocList(SolrQueryRequest req, SolrQueryResponse res)
        throws CorruptIndexException, IOException {
    DocSlice slice = (DocSlice) res.getValues().get(RESP_EL_TAG);
    Set<String> returnFields = SolrHelper.getReturnFields(req);
    SolrDocumentList docList = new SolrDocumentList();
    for (DocIterator it = slice.iterator(); it.hasNext();) {
        int docId = it.nextDoc();
        Document doc = req.getSearcher().getReader().document(docId);
        SolrDocument sdoc = new SolrDocument();
        for (Fieldable f : doc.getFields()) {
            String fn = f.name();
            if (returnFields.contains(fn)) {
                sdoc.addField(fn, doc.get(fn));
            }
        }
        docList.add(sdoc);
    }
    docList.setMaxScore(slice.maxScore());
    docList.setNumFound(slice.matches());
    return docList;
}

From source file:de.qaware.chronix.solr.query.analysis.AnalysisHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    LOGGER.debug("Handling analysis request {}", req);
    //First check if the request should return documents => rows > 0
    SolrParams params = req.getParams();
    String rowsParam = params.get(CommonParams.ROWS, null);
    int rows = -1;
    if (rowsParam != null) {
        rows = Integer.parseInt(rowsParam);
    }//ww  w .j av a 2s  .  c  om

    SolrDocumentList results = new SolrDocumentList();
    String[] filterQueries = req.getParams().getParams(CommonParams.FQ);

    //Do a query and collect them on the join function
    Map<String, List<SolrDocument>> collectedDocs = findDocuments(req,
            JoinFunctionEvaluator.joinFunction(filterQueries));

    //If now rows should returned, we only return the num found
    if (rows == 0) {
        results.setNumFound(collectedDocs.keySet().size());
    } else {
        //Otherwise return the aggregated time series
        long queryStart = Long.parseLong(params.get(ChronixQueryParams.QUERY_START_LONG));
        long queryEnd = Long.parseLong(params.get(ChronixQueryParams.QUERY_END_LONG));

        //We have an analysis query
        List<SolrDocument> aggregatedDocs = analyze(collectedDocs,
                AnalysisQueryEvaluator.buildAnalysis(filterQueries), queryStart, queryEnd);

        results.addAll(aggregatedDocs);
        results.setNumFound(aggregatedDocs.size());
    }
    rsp.add("response", results);
    LOGGER.debug("Sending response {}",
            rsp.getToLogAsString(String.join("-", filterQueries == null ? "" : "")) + "/");

}