Example usage for org.apache.solr.client.solrj SolrQuery setRows

List of usage examples for org.apache.solr.client.solrj SolrQuery setRows

Introduction

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

Prototype

public SolrQuery setRows(Integer rows) 

Source Link

Usage

From source file:org.dataconservancy.dcs.access.impl.solr.DcsSolrSearcher.java

License:Apache License

public QueryResponse search(String query, int offset, int maxmatches, String... params)
        throws SolrServerException {
    SolrQuery q = new SolrQuery(query);

    q.setStart(offset);// w w  w .  j a v  a 2  s . c o  m
    q.setRows(maxmatches);

    for (int i = 0; i < params.length;) {
        String name = params[i++];
        String val = params[i++];

        q.setParam(name, val);
    }

    return server.query(q);
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SeadSolrService.java

License:Apache License

@Override
public QueryResponse search(String query, int offset, int matches, String... params)
        throws SolrServerException {
    SolrQuery q = new SolrQuery(query);

    q.setStart(offset);//from  w ww .j a v a  2 s.  com
    q.setRows(matches);

    for (int i = 0; i < params.length;) {
        System.out.println(i + " " + params[i] + " length=" + params.length);

        if (params[i].equalsIgnoreCase("facet.field")) //if condition checks and enables faceted search
        {
            i++;
            String[] facets = params[i++].split(",");
            for (int j = 0; j < facets.length; j++) {
                System.out.println(i + " " + j + " " + facets[j]);
                q.addFacetField(facets[j]);
            }
        }
        if (i == params.length)
            break;
        if (params[i].equalsIgnoreCase("sort")) //if condition checks and enables sort
        {
            i++;
            String[] sortFields = params[i++].split(",");
            for (int j = 0; j < sortFields.length; j++) {

                j++;
                if (sortFields[j].equalsIgnoreCase("asc"))
                    q.addSortField(sortFields[j - 1], SolrQuery.ORDER.asc);
                else
                    q.addSortField(sortFields[j - 1], SolrQuery.ORDER.desc);
            }
        } else {
            String name = params[i++];
            String val = params[i++];

            q.setParam(name, val);
        }
    }
    QueryResponse resp = server().query(q);
    return resp;
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrService.java

License:Apache License

/**
 * @return number of documents in the index
 * @throws SolrServerException/*w w  w.  ja  v a2 s.c o  m*/
 */
public long size() throws SolrServerException {
    SolrQuery q = new SolrQuery("*:*");
    q.setRows(0);

    return server().query(q).getResults().getNumFound();
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrService.java

License:Apache License

/**
 * @param query//from w w w .j  av  a2  s . com
 * @param offset
 * @param matches
 * @param params
 *            name,value pairs of Solr params
 * @return
 * @throws SolrServerException
 */
public QueryResponse search(String query, int offset, int matches, String... params)
        throws SolrServerException {
    SolrQuery q = new SolrQuery(query);

    q.setStart(offset);
    q.setRows(matches);

    for (int i = 0; i < params.length;) {
        String name = params[i++];
        String val = params[i++];

        q.setParam(name, val);
    }

    return server.query(q);
}

From source file:org.dataone.proto.trove.mn.dao.v1.SolrLogIndexer.java

License:Apache License

/**
 * Pull solr query field parameters from a Properties file and use them to issue a solr query.
 *
 * The solr query results are transformed into a Log object and returned.
 *
 * The following fields are currently supported and passed through to the solr log index as a query
 *
 * fromDate (Types.DateTime)  Records with time stamp greater than or equal to (>=) this value will be returned.
 * Transmitted as a URL query parameter, and so must be escaped accordingly. toDate (Types.DateTime)  Records with
 * a time stamp less than this value will be returned. If not specified, then defaults to now. Transmitted as a URL
 * query parameter, and so must be escaped accordingly. event (Types.Event)  Return only log records for the
 * specified type of event. Default is all. Transmitted as a URL query parameter, and so must be escaped
 * accordingly. pidFilter (string)  Return only log records for identifiers that start with the supplied identifier
 * string. Support for this parameter is optional and MAY be ignored by the Member Node implementation with no
 * warning. Transmitted as a URL query parameter, and so must be escaped accordingly. start=0 (integer)  Optional
 * zero based offset from the first record in the set of matching log records. Used to assist with paging the
 * response. Transmitted as a URL query parameter, and so must be escaped accordingly. count=1000 (integer)  The
 * maximum number of log records that should be returned in the response. The Member Node may return fewer and the
 * caller should check the total in the response to determine if further pages may be retrieved. Transmitted as a
 * URL query parameter, and so must be escaped accordingly.
 *
 *
 *
 *
 * @param query/*  w  ww  .  j  a  v a2 s.c  o  m*/
 * @return Log
 * @throws org.apache.commons.configuration.ConfigurationException
 * @throws java.io.IOException
 * @throws org.apache.solr.client.solrj.SolrServerException
 */
@Override
public Log search(Properties query) throws ConfigurationException, IOException, SolrServerException {

    // since this is the implementation assume the input to be a properties file.
    Configuration apacheConfiguration = getApacheConfiguration(query);

    List<LogEntrySolrItem> outputList = new ArrayList(); // may not have to initialize
    Log log = new Log();

    SolrQuery queryParams = new SolrQuery();
    queryParams.setQuery(queryStringBuilder(apacheConfiguration));

    queryParams.setSortField("dateAggregated", SolrQuery.ORDER.desc);
    if (apacheConfiguration.containsKey("start")) {
        queryParams.setStart(apacheConfiguration.getInt("start"));
    } else {
        queryParams.setStart(0);
    }
    if (apacheConfiguration.containsKey("count")) {
        queryParams.setRows(apacheConfiguration.getInt("count"));
    } else {
        queryParams.setRows(defaultCount);
    }
    QueryResponse queryResponse = solrServer.query(queryParams);
    queryResponse.getResults();

    List<LogEntrySolrItem> logEntryList = queryResponse.getBeans(LogEntrySolrItem.class);
    for (LogEntrySolrItem lesi : logEntryList) {
        log.addLogEntry(lesi.getLogEntry());
    }
    log.setTotal(Long.valueOf(queryResponse.getResults().getNumFound()).intValue());

    log.setStart(Long.valueOf(queryResponse.getResults().getStart()).intValue());

    log.setCount(logEntryList.size());

    return log;
}

From source file:org.dataone.proto.trove.mn.dao.v1.SolrQueryIndexer.java

License:Apache License

@Override
public ObjectList search(Properties query) throws Exception {
    // since this is the implementation assume the input to be a properties file.
    Configuration apacheConfiguration = getApacheConfiguration(query);

    ObjectList objectList = new ObjectList();

    SolrQuery queryParams = new SolrQuery();
    queryParams.setQuery(queryStringBuilder(apacheConfiguration));

    queryParams.setSortField("id", SolrQuery.ORDER.desc);
    if (apacheConfiguration.containsKey("start")) {
        queryParams.setStart(apacheConfiguration.getInt("start"));
    } else {//  w  w w  . j  a va  2  s  .c  om
        queryParams.setStart(0);
    }
    if (apacheConfiguration.containsKey("count")) {
        queryParams.setRows(apacheConfiguration.getInt("count"));
    } else {
        queryParams.setRows(defaultCount);
    }
    QueryResponse queryResponse = solrServer.query(queryParams);
    queryResponse.getResults();

    List<ObjectInfoSolrItem> objectInfoList = queryResponse.getBeans(ObjectInfoSolrItem.class);
    for (ObjectInfoSolrItem lesi : objectInfoList) {
        objectList.addObjectInfo(lesi.getObjectInfo());
    }
    objectList.setTotal(Long.valueOf(queryResponse.getResults().getNumFound()).intValue());

    objectList.setStart(Long.valueOf(queryResponse.getResults().getStart()).intValue());

    objectList.setCount(objectInfoList.size());

    return objectList;
}

From source file:org.dbflute.solr.bhv.AbstractSolrBehavior.java

License:Apache License

/**
 * Retrieve count of the number of entities matched to the specified query condition.
 * @param cbLambda ConditionBean callback function (NotNull)
 * @return Total number of entities matched to query condition
 *//*  ww w  . j av  a 2s  .  com*/
public int selectCount(SolrCBCall<CB> cbLambda) {
    CB cb = createCB(cbLambda);
    SolrQuery query = cb.buildSolrQuery();
    query.setStart(0);
    query.setRows(0);

    if (logger.isDebugEnabled()) {
        logger.debug("cb={}", cb);
    }

    try {
        QueryResponse rsp = getSlaveSolrClient().query(query, METHOD.POST);
        return (int) rsp.getResults().getNumFound();
    } catch (SolrServerException | IOException e) {
        throw new SolrException("Failed to Solr of access.", e);
    }
}

From source file:org.dbflute.solr.bhv.AbstractSolrBehavior.java

License:Apache License

/**
 * Retrieve entities by faceted search.//  w w  w  .  j a v a2 s.c om
 * @param cbLambda ConditionBean callback function
 * @return List of faceted search result (NotNull)
 */
public SolrFacetResultBean selectFacetQuery(SolrCBCall<CB> cbLambda) {
    CB cb = createCB(cbLambda);
    SolrQuery query = cb.buildSolrQuery();
    query.setStart(0);
    query.setRows(0);

    if (logger.isDebugEnabled()) {
        logger.debug("cb={}", cb);
    }

    try {
        QueryResponse rsp = getSlaveSolrClient().query(query, METHOD.POST);

        SolrFacetResultBean resultBean = new SolrFacetResultBean();
        resultBean.setFacetResult(rsp.getFacetQuery());
        resultBean.setFacetFieldList(rsp.getFacetFields());
        resultBean.setQueryString(query.toString());
        resultBean.setQueryTime(rsp.getQTime());
        return resultBean;
    } catch (SolrServerException | IOException e) {
        throw new SolrException("Failed to Solr of access.", e);
    }
}

From source file:org.dbflute.solr.cbean.AbstractSolrConditionBean.java

License:Apache License

@Override
public SolrQuery buildSolrQuery() {
    SolrQuery query = new SolrQuery();

    if (this.isSpecified()) {
        query.setFields(this.getSpecifyFields());
    } else {/*from www  .  j a  v a 2  s .  com*/
        query.setFields(this.getAllFields());
    }
    if (this.isUseFilterQuery()) {
        query.setQuery(this.getMinimumQuery());
        query.setFilterQueries(this.getQueryArray());
    } else {
        query.setQuery(this.getQueryString());
        String[] filterQueryArray = this.getFilterQueryArray();
        if (filterQueryArray != null && filterQueryArray.length != 0) {
            query.setFilterQueries(this.getFilterQueryArray());
        }
    }
    if (this.isUseSort()) {
        for (SortClause sortClause : this.getSolrSortClauseList()) {
            query.addSort(sortClause);
        }
    }

    for (SolrQueryBean queryBean : this.getFacetQueryList()) {
        query.addFacetQuery(queryBean.getQueryString());
    }

    SolrSpecification facetSpecifyBean = getFacetSpecification();
    if (facetSpecifyBean.getSpecifyFields().length > 0) {
        query.addFacetField(facetSpecifyBean.getSpecifyFields());
    }

    query.setStart(this.getPageStart());
    query.setRows(this.getPageSize());

    return query;
}

From source file:org.dspace.app.cris.batch.ScriptAddPMCDataToRP.java

/**
 * Batch script to aggregate PMC data to RPs. See the technical
 * documentation for further details.//w  ww. ja va 2 s. c o m
 * 
 * @throws SearchServiceException
 */
public static void main(String[] args) throws ParseException, SQLException, SearchServiceException {
    log.info("#### START AddPMCDataToRP: -----" + new Date() + " ----- ####");

    DSpace dspace = new DSpace();
    ApplicationService applicationService = dspace.getServiceManager().getServiceByName("applicationService",
            ApplicationService.class);

    CrisSearchService searchService = dspace.getServiceManager()
            .getServiceByName(CrisSearchService.class.getName(), CrisSearchService.class);
    PMCPersistenceService pmcService = dspace.getServiceManager()
            .getServiceByName(PMCPersistenceService.class.getName(), PMCPersistenceService.class);

    List<ResearcherPage> rs = applicationService.getList(ResearcherPage.class);

    for (ResearcherPage rp : rs) {
        boolean updated = false;
        int itemsCited = 0;
        int citations = 0;
        SolrQuery query = new SolrQuery();
        query.setQuery("dc.identifier.pmid:[* TO *]");
        query.addFilterQuery("{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp),
                "NOT(withdrawn:true)");
        query.setFields("dc.identifier.pmid");
        query.setRows(Integer.MAX_VALUE);

        QueryResponse response = searchService.search(query);
        SolrDocumentList results = response.getResults();
        for (SolrDocument doc : results) {
            Integer pmid = null;
            try {
                pmid = Integer.valueOf((String) doc.getFirstValue("dc.identifier.pmid"));
            } catch (NumberFormatException e) {
                log.warn("Found invalid pmid: " + doc.getFieldValue("dc.identifier.pmid") + " for rp: "
                        + ResearcherPageUtils.getPersistentIdentifier(rp));
            }
            if (pmid != null) {
                PMCCitation pmccitation = pmcService.get(PMCCitation.class, pmid);
                if (pmccitation != null && pmccitation.getNumCitations() > 0) {
                    itemsCited++;
                    citations += pmccitation.getNumCitations();
                }
            }
        }

        updated = setValue(applicationService, rp, itemsCitedTP, String.valueOf(itemsCited));
        // caution don't use the short-circuit OR operator (i.e || otherwise
        // only the first found pmcdata value will be recorded!) 
        updated = updated | setValue(applicationService, rp, citationsTP, String.valueOf(citations));
        updated = updated
                | setValue(applicationService, rp, itemsInPubmedTP, String.valueOf(results.getNumFound()));

        if (StringUtils.isNotEmpty(itemsInPMCTP)) {
            query = new SolrQuery();
            query.setQuery("dc.identifier.pmcid:[* TO *]");
            query.addFilterQuery(
                    "{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp),
                    "NOT(withdrawn:true)");
            query.setRows(0);

            response = searchService.search(query);
            results = response.getResults();
            // caution don't use the short-circuit OR operator (i.e || otherwise
            // only the first found pmcdata value will be recorded!)
            updated = updated
                    | setValue(applicationService, rp, itemsInPMCTP, String.valueOf(results.getNumFound()));
        }

        if (updated) {
            applicationService.saveOrUpdate(ResearcherPage.class, rp);
        }
    }
    log.info("#### END AddPMCDataToRP: -----" + new Date() + " ----- ####");
}