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:org.springframework.data.solr.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.count(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:org.springframework.data.solr.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.count(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:org.xwiki.query.solr.internal.SolrQueryExecutor.java

License:Open Source License

/**
 * Filter out results from the response that the current user does not have access to view.
 * /*from w  w w.  j a  v  a 2 s  .  c o m*/
 * @param response the Solr response to filter
 */
protected void filterResponse(QueryResponse response) {
    SolrDocumentList results = response.getResults();
    long numFound = results.getNumFound();

    // Since we are modifying the results collection, we need to iterate over its copy.
    for (SolrDocument result : new ArrayList<SolrDocument>(results)) {
        try {
            DocumentReference resultDocumentReference = new DocumentReference(
                    (String) result.get(FieldUtils.WIKI), (String) result.get(FieldUtils.SPACE),
                    (String) result.get(FieldUtils.NAME));

            if (!documentAccessBridge.exists(resultDocumentReference)
                    || !documentAccessBridge.isDocumentViewable(resultDocumentReference)) {

                // Remove the current incompatible result.
                results.remove(result);

                // Decrement the number of results.
                numFound--;

                // FIXME: We should update maxScore as well when removing the top scored item. How do we do that?
                // Sorting based on score might be a not so expensive option.

                // FIXME: What about highlighting, facets and all the other data inside the QueryResponse?
            }
        } catch (Exception e) {
            this.logger.warn("Skipping bad result: {}", result, e);
        }
    }

    // Update the new number of results, excluding the filtered ones.
    if (numFound < 0) {
        // Lower bound guard for the total number of results.
        numFound = 0;
    }
    results.setNumFound(numFound);
}

From source file:uk.co.flax.ukmp.search.solr.SolrSearchEngine.java

License:Apache License

/**
 * Run the given query, removing the given list of filters from the query itself.
 * @param query the query to be run.//  w  w w . jav a2  s  .co  m
 * @param filters the list of filters to remove.
 * @return a list of documents returned from the query.
 * @throws SolrServerException if a problem occurs accessing Solr.
 */
private SolrDocumentList runQueryWithoutFilters(SolrQuery query, List<String> filters)
        throws SolrServerException {
    for (String fq : filters) {
        // Re-query without the filter queries set
        query.removeFilterQuery(fq);
    }
    QueryResponse response = server.query(query);
    SolrDocumentList docs = response.getResults();

    // Set numFound to the results list size, avoid reading every tweet in
    // the search engine
    if (docs.getNumFound() > 0) {
        docs.setNumFound(docs.size());
    }

    return docs;
}