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:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.DefaultObjectPropertyFormGenerator.java

License:Open Source License

private boolean tooManyRangeOptions(VitroRequest vreq, HttpSession session) throws SolrServerException {
    List<String> types = getRangeTypes(vreq);
    SolrServer solrServer = SolrSetup.getSolrServer(session.getServletContext());

    //empty list means the range is not set to anything, force Thing
    if (types.size() == 0) {
        types = new ArrayList<String>();
        types.add(VitroVocabulary.OWL_THING);
    }/* ww w.  j  a v a 2s . c o m*/

    long count = 0;
    for (String type : types) {
        //solr query for type count.          
        SolrQuery query = new SolrQuery();
        if (VitroVocabulary.OWL_THING.equals(type)) {
            query.setQuery("*:*");
        } else {
            query.setQuery(VitroSearchTermNames.RDFTYPE + ":" + type);
        }
        query.setRows(0);

        QueryResponse rsp = solrServer.query(query);
        SolrDocumentList docs = rsp.getResults();
        long found = docs.getNumFound();
        count = count + found;
        if (count > maxNonACRangeIndividualCount)
            break;
    }

    return count > maxNonACRangeIndividualCount;
}

From source file:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.DefaultObjectPropertyFormGenerator.java

License:Open Source License

private Object rangeIndividualsExist(HttpSession session, List<String> types) throws SolrServerException {
    SolrServer solrServer = SolrSetup.getSolrServer(session.getServletContext());

    boolean rangeIndividualsFound = false;
    for (String type : types) {
        //solr for type count.
        SolrQuery query = new SolrQuery();
        query.setQuery(VitroSearchTermNames.RDFTYPE + ":" + type);
        query.setRows(0);//w  w w . j a  v a 2s  .  c  o  m

        QueryResponse rsp = solrServer.query(query);
        SolrDocumentList docs = rsp.getResults();
        if (docs.getNumFound() > 0) {
            rangeIndividualsFound = true;
            break;
        }
    }

    return rangeIndividualsFound;
}

From source file:edu.cornell.mannlib.vitro.webapp.search.controller.AutocompleteController.java

License:Open Source License

@Override
protected void doRequest(VitroRequest vreq, HttpServletResponse response) throws IOException, ServletException {

    try {/*from   w w w. j  a v  a  2s  . co m*/

        String qtxt = vreq.getParameter(PARAM_QUERY);

        SolrQuery query = getQuery(qtxt, vreq);
        if (query == null) {
            log.debug("query for '" + qtxt + "' is null.");
            doNoQuery(response);
            return;
        }
        log.debug("query for '" + qtxt + "' is " + query.toString());

        SolrServer solr = SolrSetup.getSolrServer(getServletContext());
        QueryResponse queryResponse = solr.query(query);

        if (queryResponse == null) {
            log.error("Query response for a search was null");
            doNoSearchResults(response);
            return;
        }

        SolrDocumentList docs = queryResponse.getResults();

        if (docs == null) {
            log.error("Docs for a search was null");
            doNoSearchResults(response);
            return;
        }

        long hitCount = docs.getNumFound();
        log.debug("Total number of hits = " + hitCount);
        if (hitCount < 1) {
            doNoSearchResults(response);
            return;
        }

        List<SearchResult> results = new ArrayList<SearchResult>();
        for (SolrDocument doc : docs) {
            try {
                String uri = doc.get(VitroSearchTermNames.URI).toString();
                // RY 7/1/2011
                // Comment was: VitroSearchTermNames.NAME_RAW is a multivalued field, so doc.get() returns a list.
                // Changed to: VitroSearchTermNames.NAME_RAW is a multivalued field, so doc.get() could return a list
                // But in fact: I'm no longer seeing any lists returned for individuals with multiple labels. Not sure
                // if this is new behavior or what. ???
                Object nameRaw = doc.get(VitroSearchTermNames.NAME_RAW);
                String name = null;
                if (nameRaw instanceof List<?>) {
                    @SuppressWarnings("unchecked")
                    List<String> nameRawList = (List<String>) nameRaw;
                    name = nameRawList.get(0);
                } else {
                    name = (String) nameRaw;
                }
                SearchResult result = new SearchResult(name, uri);
                results.add(result);
            } catch (Exception e) {
                log.error("problem getting usable individuals from search " + "hits" + e.getMessage());
            }
        }

        Collections.sort(results);

        JSONArray jsonArray = new JSONArray();
        for (SearchResult result : results) {
            jsonArray.put(result.toMap());
        }
        response.getWriter().write(jsonArray.toString());

    } catch (Throwable e) {
        log.error(e, e);
        doSearchError(response);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.search.controller.PagedSearchController.java

License:Open Source License

@Override
protected ResponseValues processRequest(VitroRequest vreq) {

    //There may be other non-html formats in the future
    Format format = getFormat(vreq);
    boolean wasXmlRequested = Format.XML == format;
    log.debug("Requested format was " + (wasXmlRequested ? "xml" : "html"));
    boolean wasHtmlRequested = !wasXmlRequested;

    try {/* w  w  w.  j a v  a2  s.co m*/

        //make sure an IndividualDao is available 
        if (vreq.getWebappDaoFactory() == null || vreq.getWebappDaoFactory().getIndividualDao() == null) {
            log.error("Could not get webappDaoFactory or IndividualDao");
            throw new Exception("Could not access model.");
        }
        IndividualDao iDao = vreq.getWebappDaoFactory().getIndividualDao();
        VClassGroupDao grpDao = vreq.getWebappDaoFactory().getVClassGroupDao();
        VClassDao vclassDao = vreq.getWebappDaoFactory().getVClassDao();

        ApplicationBean appBean = vreq.getAppBean();

        log.debug("IndividualDao is " + iDao.toString() + " Public classes in the classgroup are "
                + grpDao.getPublicGroupsWithVClasses().toString());
        log.debug("VClassDao is " + vclassDao.toString());

        int startIndex = getStartIndex(vreq);
        int hitsPerPage = getHitsPerPage(vreq);

        String queryText = vreq.getParameter(VitroQuery.QUERY_PARAMETER_NAME);
        log.debug("Query text is \"" + queryText + "\"");

        String badQueryMsg = badQueryText(queryText);
        if (badQueryMsg != null) {
            return doFailedSearch(badQueryMsg, queryText, format);
        }

        SolrQuery query = getQuery(queryText, hitsPerPage, startIndex, vreq);
        SolrServer solr = SolrSetup.getSolrServer(getServletContext());
        QueryResponse response = null;

        try {
            response = solr.query(query);
        } catch (Exception ex) {
            String msg = makeBadSearchMessage(queryText, ex.getMessage());
            log.error("could not run Solr query", ex);
            return doFailedSearch(msg, queryText, format);
        }

        if (response == null) {
            log.error("Search response was null");
            return doFailedSearch("The search request contained errors.", queryText, format);
        }

        SolrDocumentList docs = response.getResults();
        if (docs == null) {
            log.error("Document list for a search was null");
            return doFailedSearch("The search request contained errors.", queryText, format);
        }

        long hitCount = docs.getNumFound();
        log.debug("Number of hits = " + hitCount);
        if (hitCount < 1) {
            return doNoHits(queryText, format);
        }

        List<Individual> individuals = new ArrayList<Individual>(docs.size());
        Iterator<SolrDocument> docIter = docs.iterator();
        while (docIter.hasNext()) {
            try {
                SolrDocument doc = docIter.next();
                String uri = doc.get(VitroSearchTermNames.URI).toString();
                Individual ind = iDao.getIndividualByURI(uri);
                if (ind != null) {
                    ind.setSearchSnippet(getSnippet(doc, response));
                    individuals.add(ind);
                }
            } catch (Exception e) {
                log.error("Problem getting usable individuals from search hits. ", e);
            }
        }

        ParamMap pagingLinkParams = new ParamMap();
        pagingLinkParams.put(PARAM_QUERY_TEXT, queryText);
        pagingLinkParams.put(PARAM_HITS_PER_PAGE, String.valueOf(hitsPerPage));

        if (wasXmlRequested) {
            pagingLinkParams.put(PARAM_XML_REQUEST, "1");
        }

        /* Compile the data for the templates */

        Map<String, Object> body = new HashMap<String, Object>();

        String classGroupParam = vreq.getParameter(PARAM_CLASSGROUP);
        boolean classGroupFilterRequested = false;
        if (!StringUtils.isEmpty(classGroupParam)) {
            VClassGroup grp = grpDao.getGroupByURI(classGroupParam);
            classGroupFilterRequested = true;
            if (grp != null && grp.getPublicName() != null)
                body.put("classGroupName", grp.getPublicName());
        }

        String typeParam = vreq.getParameter(PARAM_RDFTYPE);
        boolean typeFilterRequested = false;
        if (!StringUtils.isEmpty(typeParam)) {
            VClass type = vclassDao.getVClassByURI(typeParam);
            typeFilterRequested = true;
            if (type != null && type.getName() != null)
                body.put("typeName", type.getName());
        }

        /* Add ClassGroup and type refinement links to body */
        if (wasHtmlRequested) {
            if (!classGroupFilterRequested && !typeFilterRequested) {
                // Search request includes no ClassGroup and no type, so add ClassGroup search refinement links.
                body.put("classGroupLinks", getClassGroupsLinks(grpDao, docs, response, queryText));
            } else if (classGroupFilterRequested && !typeFilterRequested) {
                // Search request is for a ClassGroup, so add rdf:type search refinement links
                // but try to filter out classes that are subclasses
                body.put("classLinks", getVClassLinks(vclassDao, docs, response, queryText));
                pagingLinkParams.put(PARAM_CLASSGROUP, classGroupParam);

            } else {
                //search request is for a class so there are no more refinements
                pagingLinkParams.put(PARAM_RDFTYPE, typeParam);
            }
        }

        body.put("individuals", IndividualSearchResult.getIndividualTemplateModels(individuals, vreq));

        body.put("querytext", queryText);
        body.put("title", queryText + " - " + appBean.getApplicationName() + " Search Results");

        body.put("hitCount", hitCount);
        body.put("startIndex", startIndex);

        body.put("pagingLinks",
                getPagingLinks(startIndex, hitsPerPage, hitCount, vreq.getServletPath(), pagingLinkParams));

        if (startIndex != 0) {
            body.put("prevPage",
                    getPreviousPageLink(startIndex, hitsPerPage, vreq.getServletPath(), pagingLinkParams));
        }
        if (startIndex < (hitCount - hitsPerPage)) {
            body.put("nextPage",
                    getNextPageLink(startIndex, hitsPerPage, vreq.getServletPath(), pagingLinkParams));
        }

        // VIVO OpenSocial Extension by UCSF
        try {
            OpenSocialManager openSocialManager = new OpenSocialManager(vreq, "search");
            // put list of people found onto pubsub channel 
            // only turn this on for a people only search
            if ("http://vivoweb.org/ontology#vitroClassGrouppeople"
                    .equals(vreq.getParameter(PARAM_CLASSGROUP))) {
                List<String> ids = OpenSocialManager.getOpenSocialId(individuals);
                openSocialManager.setPubsubData(OpenSocialManager.JSON_PERSONID_CHANNEL,
                        OpenSocialManager.buildJSONPersonIds(ids, "" + ids.size() + " people found"));
            }
            // TODO put this in a better place to guarantee that it gets called at the proper time!
            openSocialManager.removePubsubGadgetsWithoutData();
            body.put("openSocial", openSocialManager);
            if (openSocialManager.isVisible()) {
                body.put("bodyOnload", "my.init();");
            }
        } catch (IOException e) {
            log.error("IOException in doTemplate()", e);
        } catch (SQLException e) {
            log.error("SQLException in doTemplate()", e);
        }

        String template = templateTable.get(format).get(Result.PAGED);

        return new TemplateResponseValues(template, body);
    } catch (Throwable e) {
        return doSearchError(e, format);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.solr.SolrResultsParser.java

License:Open Source License

/**
 * Parse the entire response into a list of maps.
 */// w w w. java 2  s .  co  m
public List<Map<String, String>> parse() {
    List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

    if (queryResponse == null) {
        log.error("Query response for a search was null");
        return maps;
    }

    SolrDocumentList docs = queryResponse.getResults();
    if (docs == null) {
        log.error("Docs for a search was null");
        return maps;
    }
    log.debug("Total number of hits = " + docs.getNumFound());

    for (SolrDocument doc : docs) {
        maps.add(parseSingleDocument(doc));
    }

    return maps;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.solr.SolrResultsParser.java

License:Open Source License

/**
 * Parse the response, accepting only those maps that are acceptable to the
 * filter, until we reach the maximum desired number of results (or until we
 * have parsed the entire response).//from   w  w w .j  a  v a2 s  . c om
 */
public List<Map<String, String>> parseAndFilterResponse(SolrResponseFilter filter, int maxNumberOfResults) {
    List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

    if (queryResponse == null) {
        log.error("Query response for a search was null");
        return maps;
    }

    SolrDocumentList docs = queryResponse.getResults();
    if (docs == null) {
        log.error("Docs for a search was null");
        return maps;
    }
    log.debug("Total number of hits = " + docs.getNumFound());

    for (SolrDocument doc : docs) {
        Map<String, String> map = parseSingleDocument(doc);
        if (filter.accept(map)) {
            maps.add(map);
        }
        if (maps.size() >= maxNumberOfResults) {
            break;
        }
    }

    return maps;
}

From source file:edu.harvard.lib.lcloud.ItemDAO.java

License:Open Source License

/**
 * Returns SearchResults for a given SolrDocumentList. A full results object with an "items" wrapper
 * element around the mods items is used to logically separate pagination, items and facets in the XML
 * @param doc   solr document list to build results
 * @return      the SearchResults object for this solr result
 * @see         SearchResults/*from   w  w w . jav  a  2 s.  com*/
 */

private SearchResults buildFullResults(SolrDocumentList docs) {
    SearchResults results = new SearchResults();
    Pagination pagination = new Pagination();
    pagination.setNumFound(docs.getNumFound());
    pagination.setStart(docs.getStart());
    pagination.setRows(limit);
    //List<ModsType> modsTypes = new ArrayList<ModsType>();
    ItemGroup itemGroup = new ItemGroup();
    List<Item> items = new ArrayList<Item>();
    for (final SolrDocument doc : docs) {
        Item item = new Item();
        ModsType modsType = null;
        try {
            modsType = (new ItemDAO()).getModsType(doc);
        } catch (JAXBException je) {
            log.error(je.getMessage());
            je.printStackTrace();
        }
        //modsTypes.add(modsType);
        //items.add(item);
        item.setModsType(modsType);
        items.add(item);
    }
    //items.setModsType(modsType);
    itemGroup.setItems(items);
    results.setItemGroup(itemGroup);
    results.setPagination(pagination);
    if (facet != null)
        results.setFacet(facet);
    return results;
}

From source file:edu.harvard.lib.lcloud.ItemDAO.java

License:Open Source License

/**
 * Returns SearchResultsSlim for a given SolrDocumentList. A "slimmer" results object without the 
 * "items" wrapper element is created for better transform to json.
 * @param doc   solr document list to build results
 * @return      the SearchResultsSlim object for this solr result
 * @see         SearchResultsSlim// ww  w  . j a v a2 s .  c  o  m
 */

private SearchResultsSlim buildSlimResults(SolrDocumentList docs) {
    SearchResultsSlim results = new SearchResultsSlim();
    Pagination pagination = new Pagination();
    pagination.setNumFound(docs.getNumFound());
    pagination.setStart(docs.getStart());
    pagination.setRows(limit);
    //List<ModsType> modsTypes = new ArrayList<ModsType>();
    List<Item> items = new ArrayList<Item>();
    for (final SolrDocument doc : docs) {
        Item item = new Item();
        ModsType modsType = null;
        try {
            modsType = (new ItemDAO()).getModsType(doc);
        } catch (JAXBException je) {
            log.error(je.getMessage());
            je.printStackTrace();
        }
        item.setModsType(modsType);
        items.add(item);
    }
    results.setItems(items);
    results.setPagination(pagination);
    if (facet != null)
        results.setFacet(facet);
    return results;
}

From source file:edu.harvard.liblab.ecru.rs.ResourceUtils.java

License:Open Source License

public static ResultsList createResultsList(QueryResponse qr, String inputRows, UriInfo req) throws Exception {
    ResultsList rl = new ResultsList();
    URI uri = req.getRequestUri();
    String query = uri.getQuery();

    UriBuilder uriBuild = req.getRequestUriBuilder();
    SolrDocumentList docList = qr.getResults();
    long numFound = docList.getNumFound();
    long start = docList.getStart();
    long rows = 10;
    try {//from   www. j  a  v a  2s.c  om
        if (inputRows != null && !inputRows.trim().isEmpty()) {
            Long r = new Long(inputRows);
            rows = r.longValue();
        }
    } catch (NumberFormatException e) {
        System.err.println("Couldn't convert rows: " + inputRows);
    }

    rl.setItems(convertEcruBeans(qr.getBeans(EcruBase.class), start, uriBuild));

    Summary sum = new Summary(query, numFound, rows, start);

    // Don't bother with faceting if there are no more found than the rows
    if (numFound > rows) {
        Faceting facets = getFaceting(qr.getFacetFields());
        facets = decorateFacets(uriBuild, facets);
        rl.setFaceting(facets);
        // get prev and next, if relevant
        if (start + rows < numFound) {
            sum.setNextUrl(createUrlWithNewParam(uriBuild, "start", (start + rows)));
        }
        if (start - rows > -1) {
            sum.setPrevUrl(createUrlWithNewParam(uriBuild, "start", (start - rows)));
        }
    }
    rl.setSummary(sum);
    return rl;
}

From source file:edu.lternet.pasta.datapackagemanager.solr.search.SimpleSolrSearch.java

License:Apache License

private String solrDocumentListToXML(SolrDocumentList solrDocumentList) {
    String xmlString = "";
    final String INDENT = "    ";
    long numFound = solrDocumentList.getNumFound();
    long start = solrDocumentList.getStart();
    String[] fieldsArray = getFieldList();

    String firstLine = String.format("<resultset numFound='%d' start='%d' rows='%d'>\n", numFound, start, rows);
    StringBuilder sb = new StringBuilder(firstLine);

    for (SolrDocument solrDocument : solrDocumentList) {
        sb.append(String.format("%s<document>\n", INDENT));

        if (fieldsArray != null) {

            for (String fieldName : fieldsArray) {

                String wrapperElement = wrapperElements.get(fieldName);

                if (fieldName.equals("title")) {
                    String title = (String) solrDocument.getFirstValue("title");
                    sb.append(String.format("%s%s<%s>%s</%s>\n", INDENT, INDENT, fieldName, title, fieldName));
                } else if (wrapperElement != null) {
                    sb.append(String.format("%s%s<%s>\n", INDENT, INDENT, wrapperElement));
                    Collection<Object> multiValues = solrDocument.getFieldValues(fieldName);
                    if (multiValues != null && multiValues.size() > 0) {
                        for (Object value : multiValues) {
                            String valueStr = null;
                            if (isDateField(fieldName)) {
                                Date dateValue = (Date) value;
                                dateValue = adjustDate(dateValue);
                                String formatPattern = bestDateFormat(fieldName);
                                SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
                                if (dateValue != null) {
                                    valueStr = sdf.format(dateValue);
                                }//from w  w  w. ja v  a  2 s . c o m
                            } else {
                                valueStr = (String) value;
                            }

                            sb.append(String.format("%s%s%s<%s>%s</%s>\n", INDENT, INDENT, INDENT, fieldName,
                                    valueStr, fieldName));
                        }
                    }
                    sb.append(String.format("%s%s</%s>\n", INDENT, INDENT, wrapperElement));
                } else {
                    String fieldValue = "";
                    if (isDateField(fieldName)) {
                        Date dateValue = (Date) solrDocument.getFieldValue(fieldName);
                        dateValue = adjustDate(dateValue);
                        String formatPattern = bestDateFormat(fieldName);
                        SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
                        if (dateValue != null) {
                            fieldValue = sdf.format(dateValue);
                        }
                    } else {
                        fieldValue = (String) solrDocument.getFieldValue(fieldName);
                        if (fieldValue == null)
                            fieldValue = "";
                    }
                    sb.append(String.format("%s%s<%s>%s</%s>\n", INDENT, INDENT, fieldName, fieldValue,
                            fieldName));

                    /*
                     * Support the older format for search results.
                     * 
                     * These element names ("docid", "packageId", and "pubDate")
                     * were never officially documented but some clients might rely 
                     * on them. They should be deprecated. They have been
                     * replaced with element names that exactly match their
                     * corresponding Solr field names: "id", "packageid", and "pubdate".
                     */
                    if (fieldName.equals("id")) {
                        sb.append(String.format("%s%s<docid>%s</docid>\n", INDENT, INDENT, fieldValue));
                    } else if (fieldName.equals("packageid")) {
                        sb.append(String.format("%s%s<packageId>%s</packageId>\n", INDENT, INDENT, fieldValue));
                    } else if (fieldName.equals("pubdate")) {
                        sb.append(String.format("%s%s<pubDate>%s</pubDate>\n", INDENT, INDENT, fieldValue));
                    }
                }
            }
        }

        sb.append(String.format("%s</document>\n", INDENT));
    }

    sb.append("</resultset>\n");
    xmlString = sb.toString();

    return xmlString;
}