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

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

Introduction

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

Prototype

public long getNumFound() 

Source Link

Usage

From source file:com.googlecode.solrgeonames.server.JsonSearchResponse.java

License:Open Source License

/**
 * Create a response header string for the result set.
 *
 * @param results: The Solr results//from  w w  w  .  j a va  2s .c  om
 * @return String: Header block
 */
private String headResponse(QueryResponse results) {
    // Start index
    String start = request.getParameter("start");
    if (start == null || start.equals("")) {
        start = GeoServlet.DEFAULT_START;
    }

    // Rows
    String rows = request.getParameter("rows");
    if (rows == null || rows.equals("")) {
        rows = GeoServlet.DEFAULT_ROWS;
    }

    String inner = escape("title", "Generic Search") + ",\n";
    if (results == null) {
        inner += escape("totalResults", "0") + ",\n";
    } else {
        SolrDocumentList list = results.getResults();
        inner += escape("totalResults", list.getNumFound()) + ",\n";
    }
    inner += escape("startIndex", start) + ",\n";
    inner += escape("itemsPerPage", rows) + "\n";
    return "\"OpenSearchResponse\": {\n" + inner + "\n}";
}

From source file:com.hurence.logisland.service.solr.SolrTokenizationTest.java

License:Apache License

@Test
public void testTokenizerInSolr() throws SolrServerException, IOException {
    SolrClient server = rule.getClient();
    ModifiableSolrParams params = new ModifiableSolrParams();

    // ** Let's index a document into our embedded server

    SolrInputDocument newDoc = new SolrInputDocument();
    newDoc.addField("host", "Test Document 1");
    newDoc.addField("name", "doc-1");
    newDoc.addField("type", "Hello world!");
    newDoc.addField("start", new Date().getTime());
    newDoc.addField("end", new Date().getTime() + 1000);
    server.add(newDoc);//from www  . j av a  2  s  .  c  om
    server.commit();

    // ** And now let's query for it

    params.set("q", "name:doc-1");
    QueryResponse qResp = server.query(params);

    SolrDocumentList docList = qResp.getResults();
    assertTrue(docList.getNumFound() == 1);
    SolrDocument doc = docList.get(0);
    assertTrue(doc.getFirstValue("host").equals("Test Document 1"));
}

From source file:com.liferay.portal.search.solr.internal.SolrIndexSearcher.java

License:Open Source License

protected long doSearchCount(SearchContext searchContext, Query query) throws Exception {

    QueryResponse queryResponse = doSearch(searchContext, query, searchContext.getStart(),
            searchContext.getEnd(), true);

    SolrDocumentList solrDocumentList = queryResponse.getResults();

    return solrDocumentList.getNumFound();
}

From source file:com.liferay.portal.search.solr.internal.SolrIndexSearcher.java

License:Open Source License

protected Hits processResponse(QueryResponse queryResponse, SearchContext searchContext, Query query) {

    long startTime = System.currentTimeMillis();

    SolrDocumentList solrDocumentList = queryResponse.getResults();

    updateFacetCollectors(queryResponse, searchContext);

    Hits hits = new HitsImpl();

    List<Document> documents = new ArrayList<>();
    Set<String> queryTerms = new HashSet<>();
    List<Float> scores = new ArrayList<>();

    QueryConfig queryConfig = query.getQueryConfig();
    Map<String, Map<String, List<String>>> highlights = queryResponse.getHighlighting();

    for (SolrDocument solrDocument : solrDocumentList) {
        Document document = processSolrDocument(solrDocument, queryConfig);

        documents.add(document);//from w  w w.java 2s. co m

        addSnippets(solrDocument, document, queryConfig, queryTerms, highlights);

        float score = GetterUtil.getFloat(String.valueOf(solrDocument.getFieldValue("score")));

        scores.add(score);
    }

    hits.setDocs(documents.toArray(new Document[documents.size()]));
    hits.setLength((int) solrDocumentList.getNumFound());
    hits.setQuery(query);
    hits.setQueryTerms(queryTerms.toArray(new String[queryTerms.size()]));
    hits.setScores(ArrayUtil.toFloatArray(scores));
    hits.setSearchTime(queryResponse.getQTime());
    hits.setStart(startTime);

    return hits;
}

From source file:com.liferay.portal.search.solr.SolrIndexSearcher.java

License:Open Source License

public long searchCount(SearchContext searchContext, Query query) throws SearchException {

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();/*from   ww  w .jav  a2s .com*/

    try {
        QueryResponse queryResponse = search(searchContext, query, searchContext.getStart(),
                searchContext.getEnd(), true);

        SolrDocumentList solrDocumentList = queryResponse.getResults();

        return solrDocumentList.getNumFound();
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(e, e);
        }

        if (!_swallowException) {
            throw new SearchException(e.getMessage());
        }

        return 0;
    } finally {
        if (_log.isInfoEnabled()) {
            stopWatch.stop();

            _log.info("Searching " + query.toString() + " took " + stopWatch.getTime() + " ms");
        }
    }
}

From source file:com.liferay.portal.search.solr.SolrIndexSearcher.java

License:Open Source License

protected Hits processQueryResponse(QueryResponse queryResponse, SearchContext searchContext, Query query)
        throws Exception {

    long startTime = System.currentTimeMillis();

    SolrDocumentList solrDocumentList = queryResponse.getResults();

    updateFacetCollectors(queryResponse, searchContext);

    Hits hits = new HitsImpl();

    List<Document> documents = new ArrayList<Document>();
    Set<String> queryTerms = new HashSet<String>();
    List<Float> scores = new ArrayList<Float>();

    QueryConfig queryConfig = query.getQueryConfig();
    Map<String, Map<String, List<String>>> highlights = queryResponse.getHighlighting();

    for (SolrDocument solrDocument : solrDocumentList) {
        Document document = processSolrDocument(solrDocument);

        documents.add(document);//from  w  ww  .j a  v a2  s  . c o m

        addSnippets(solrDocument, document, queryConfig, queryTerms, highlights);

        float score = GetterUtil.getFloat(String.valueOf(solrDocument.getFieldValue("score")));

        scores.add(score);
    }

    hits.setDocs(documents.toArray(new Document[documents.size()]));
    hits.setLength((int) solrDocumentList.getNumFound());
    hits.setQuery(query);
    hits.setQueryTerms(queryTerms.toArray(new String[queryTerms.size()]));
    hits.setScores(ArrayUtil.toFloatArray(scores));
    hits.setSearchTime(queryResponse.getQTime());
    hits.setStart(startTime);

    return hits;
}

From source file:com.liferay.portal.search.solr.SolrIndexSearcherImpl.java

License:Open Source License

protected Hits subset(SolrQuery solrQuery, Query query, QueryConfig queryConfig, QueryResponse queryResponse,
        boolean allResults) throws Exception {

    long startTime = System.currentTimeMillis();

    Hits hits = new HitsImpl();

    SolrDocumentList solrDocumentList = queryResponse.getResults();

    long total = solrDocumentList.getNumFound();

    if (allResults && (total > 0)) {
        solrQuery.setRows((int) total);

        queryResponse = _solrServer.query(solrQuery);

        return subset(solrQuery, query, queryConfig, queryResponse, false);
    }/*from ww  w.  ja v a2 s . c  om*/

    float maxScore = 1;

    if (queryConfig.isScoreEnabled()) {
        maxScore = solrDocumentList.getMaxScore();
    }

    int subsetTotal = solrDocumentList.size();

    Document[] documents = new DocumentImpl[subsetTotal];
    String[] snippets = new String[subsetTotal];
    float[] scores = new float[subsetTotal];

    int j = 0;

    Set<String> queryTerms = new HashSet<String>();

    Map<String, Map<String, List<String>>> highlights = queryResponse.getHighlighting();

    for (SolrDocument solrDocument : solrDocumentList) {
        Document document = new DocumentImpl();

        Collection<String> names = solrDocument.getFieldNames();

        for (String name : names) {
            Collection<Object> fieldValues = solrDocument.getFieldValues(name);

            Field field = new Field(name,
                    ArrayUtil.toStringArray(fieldValues.toArray(new Object[fieldValues.size()])));

            document.add(field);
        }

        float score = 1;

        if (queryConfig.isScoreEnabled()) {
            score = GetterUtil.getFloat(String.valueOf(solrDocument.getFieldValue("score")));
        }

        documents[j] = document;

        if (queryConfig.isHighlightEnabled()) {
            snippets[j] = getSnippet(solrDocument, queryTerms, highlights);
        } else {
            snippets[j] = StringPool.BLANK;
        }

        scores[j] = score / maxScore;

        j++;
    }

    float searchTime = (float) (System.currentTimeMillis() - startTime) / Time.SECOND;

    hits.setDocs(documents);
    hits.setLength((int) total);
    hits.setQuery(query);
    hits.setQueryTerms(queryTerms.toArray(new String[queryTerms.size()]));
    hits.setScores(scores);
    hits.setSearchTime(searchTime);
    hits.setSnippets(snippets);
    hits.setStart(startTime);

    return hits;
}

From source file:com.lyncode.oai.proxy.data.ProxyIdentify.java

License:Apache License

@Override
public Date getEarliestDate() {
    SolrServer server = SolrServerManager.getServer();
    SolrQuery query = new SolrQuery("*:*").addField(ProxyItem.DATE_FIELD).addSortField(ProxyItem.DATE_FIELD,
            ORDER.asc);//from   w w w  .  j  ava 2s . c o  m
    try {
        SolrDocumentList results = server.query(query).getResults();
        if (results.getNumFound() > 0) {
            return (Date) results.get(0).get(ProxyItem.DATE_FIELD);
        }
    } catch (SolrServerException e) {
        log.debug(e.getMessage(), e);
    }
    return new Date();
}

From source file:com.lyncode.oai.proxy.data.ProxyItemRepository.java

License:Apache License

@Override
public AbstractItem getItem(String identifier) throws IdDoesNotExistException {
    try {/*from   w w  w .  ja  v a2  s. com*/
        SolrQuery query = new SolrQuery(
                ProxyItem.IDENTIFIER_FIELD + ":" + ClientUtils.escapeQueryChars(identifier));
        SolrDocumentList list = SolrServerManager.getServer().query(query).getResults();
        if (list.getNumFound() > 0) {
            return new ProxyItem(list.get(0));
        } else
            throw new IdDoesNotExistException();
    } catch (SolrServerException e) {
        throw new IdDoesNotExistException(e);
    }
}

From source file:com.lyncode.oai.proxy.data.ProxyItemRepository.java

License:Apache License

@Override
public ListItemIdentifiersResult getItemIdentifiers(List<Filter> filters, int offset, int length) {
    try {/*from  w  ww. j  a va  2 s  . c  o  m*/
        List<String> whereCond = new ArrayList<String>();
        for (Filter filter : filters) {
            if (filter.getFilter() instanceof ProxyFilter) {
                ProxyFilter proxyFilter = (ProxyFilter) filter.getFilter();
                whereCond.add("(" + proxyFilter.query() + ")");
            }
        }
        if (whereCond.isEmpty())
            whereCond.add("*:*");
        SolrQuery query = new SolrQuery("(" + StringUtils.join(whereCond.iterator(), ") AND (") + ")");
        query.setStart(offset);
        query.setRows(length);
        SolrDocumentList list = SolrServerManager.getServer().query(query).getResults();
        List<AbstractItemIdentifier> results = new ArrayList<AbstractItemIdentifier>();
        for (SolrDocument doc : list)
            results.add(new ProxyItem(doc));
        return new ListItemIdentifiersResult(list.getNumFound() > offset + length, results,
                (int) list.getNumFound());
    } catch (SolrServerException e) {
        return new ListItemIdentifiersResult(false, new ArrayList<AbstractItemIdentifier>());
    }
}