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:com.ms.scombiz.solr.utils.BaseSolrQueryConvert.java

License:Open Source License

protected static SolrQuery setQuery(List<String> params, SearchQuery searchQuery) {
    SolrQuery solrQuery = new SolrQuery();
    String query = null;//from  w  w w . j a va2  s  . c  o  m
    if (params.isEmpty()) {
        query = ("*:*");
    } else {
        query = StringUtils.join(params, " AND ");
    }
    solrQuery.setQuery(query);
    solrQuery.setStart(searchQuery.getStart());
    solrQuery.setRows(searchQuery.getRows());
    if (StringUtils.isNotBlank(searchQuery.getSortFiled())) {
        solrQuery.addSort(searchQuery.getSortFiled(), searchQuery.getOrderBy());
    }
    return solrQuery;
}

From source file:com.mycompany.sparkrentals.client.RentalSolrClient.java

/**
 * search results based on field values in cleanedData
 *
 * @param cleanedData/*from   ww w.j a va2  s  .  c o m*/
 * @param perPage
 * @return
 * @throws SolrServerException
 * @throws IOException
 */
public QueryResponse searchRentals(Map<String, Object> cleanedData, int perPage)
        throws SolrServerException, IOException {
    SolrQuery query = new SolrQuery("*");

    //start adding filtering
    for (String field : Arrays.asList("city", "province", "country", "type", "zipCode")) {
        if (cleanedData.get(field) != null) {
            query.addFilterQuery(field + ":\"" + cleanedData.get(field) + "\"");
        }
    }
    for (String field : Arrays.asList("hasAirCondition", "hasGarden", "hasPool", "isCloseToBeach")) {
        if (cleanedData.get(field) != null) {
            boolean isYes = cleanedData.get(field).equals("Yes");
            query.addFilterQuery(field + ":\"" + isYes + "\"");
        }
    }

    if (cleanedData.get("roomsNumberFrom") != null || cleanedData.get("roomsNumberTo") != null) {
        String roomsNumberFrom = "*";
        String roomsNumberTo = "*";
        if (cleanedData.get("roomsNumberFrom") != null) {
            roomsNumberFrom = String.valueOf((int) cleanedData.get("roomsNumberFrom"));
        }
        if (cleanedData.get("roomsNumberTo") != null) {
            roomsNumberTo = String.valueOf((int) cleanedData.get("roomsNumberTo"));
        }
        String filterString = "roomsNumber:[" + roomsNumberFrom + " TO " + roomsNumberTo + "]";
        query.addFilterQuery(filterString);
    }
    if (cleanedData.get("dailyPriceFrom") != null || cleanedData.get("dailyPriceTo") != null) {
        String dailyPriceFrom = "*";
        String dailyPriceTo = "*";
        if (cleanedData.get("dailyPriceFrom") != null) {
            dailyPriceFrom = String.format("%.2f", (float) cleanedData.get("dailyPriceFrom"));
        }
        if (cleanedData.get("dailyPriceTo") != null) {
            dailyPriceTo = String.format("%.2f", (float) cleanedData.get("dailyPriceTo"));
        }
        String filterString = "dailyPrice:[" + dailyPriceFrom + " TO " + dailyPriceTo + "]";
        query.addFilterQuery(filterString);
    }
    if (cleanedData.get("timePeriod") != null) {
        int timePeriod = (int) cleanedData.get("timePeriod");
        query.addFilterQuery("updated:[NOW-" + timePeriod + "DAY TO * ]");
    }

    //pagination
    int currentPage = (int) cleanedData.getOrDefault("page", 1);
    query.setStart((currentPage - 1) * perPage);
    query.setRows(perPage);

    return solrClient.query(query);
}

From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java

License:Apache License

/**
 * A variant of {@link #testMixedIrrelevantAndRelevantUpdates} which triggers the same situation
 * using updates to different rows instead of the same. Note that we need to make changes to a number
 * of rows larger than the number of SEP threads.
 *//*w  ww.j  a v a 2s  .  c  o m*/
@Test
public void testManyMixedIrrelevantAndRelevantUpdates() throws Exception {
    createTable("table1", "family1");

    HTable table = new HTable(conf, "table1");

    StringBuilder indexerConf = new StringBuilder();
    indexerConf.append("<indexer table='table1'>");
    indexerConf.append("  <field name='field1_s' value='family1:field1' type='string'/>");
    indexerConf.append("</indexer>");

    createIndexer1(indexerConf.toString());

    SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1"));

    int expectedRows = 0;
    List<Put> puts = Lists.newArrayList();
    for (int i = 0; i < 100; i++) {
        Put put = new Put(Bytes.toBytes(String.valueOf(i)));
        put.add(b("family1"), b("irrelevant_field"), b("value1"));
        if (Math.random() >= 0.5d) {
            put.add(b("family1"), b("field1"), b("value1"));
            expectedRows++;
        }
        puts.add(put);
    }
    table.put(puts);

    SepTestUtil.waitOnReplication(conf, 60000L);
    collection1.commit();

    SolrQuery params = new SolrQuery("*:*");
    params.setRows(100);
    QueryResponse response = collection1.query(params);
    assertEquals(expectedRows, response.getResults().size());

    table.close();
}

From source file:com.nominanuda.solr.SolrHelper.java

License:Apache License

public DataArray listResultsDotAware(SolrServer solr, SolrQuery sq, int start, int count)
        throws SolrServerException {
    DataArray res = new DataArrayImpl();
    sq.setStart(start);/*from   w w  w.ja v a  2 s  .c  om*/
    sq.setRows(count);
    QueryResponse qr = solr.query(sq);
    qr.getResults();
    SolrDocumentList sdl = qr.getResults();
    for (Map<String, Object> d : sdl) {
        DataObject o = solrDoc2DataObject(d);
        res.add(o);
    }
    return res;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

/**
 * Calculates a count (using a wildcard criteria) of all the
 * rows stored in the content source and returns that value.
 *
 * @return Count of all rows in the content source.
 * @throws DSException Data source related exception.
 *//*ww w . j  av  a 2s  .c o m*/
@Override
public int count() throws DSException {
    int documentCount;
    Logger appLogger = mAppMgr.getLogger(this, "count");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    initialize();

    DSCriteria dsCriteria = new DSCriteria("Solr Query");
    dsCriteria.add(Solr.FIELD_QUERY_NAME, Field.Operator.EQUAL, Solr.QUERY_ALL_DOCUMENTS);

    SolrQuery solrQuery = mSolrQueryBuilder.create(dsCriteria);
    solrQuery.setStart(0);
    solrQuery.setRows(1);

    appLogger.debug(String.format("%s: %s %s", dsCriteria.getName(), mSolrIdentity, solrQuery.toString()));

    QueryResponse queryResponse = queryExecute(solrQuery);
    SolrDocumentList solrDocumentList = queryResponse.getResults();
    documentCount = (int) solrDocumentList.getNumFound();

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return documentCount;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

/**
 * Returns a count of rows that match the <i>DSCriteria</i> specified
 * in the parameter./*from ww  w .  j a  va  2s .  c o m*/
 *
 * @param aDSCriteria Data source criteria.
 *
 * @return Count of rows matching the data source criteria.
 *
 * @throws com.nridge.core.base.ds.DSException Data source related exception.
 */
@Override
public int count(DSCriteria aDSCriteria) throws DSException {
    int documentCount;
    Logger appLogger = mAppMgr.getLogger(this, "count");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    initialize();

    SolrQuery solrQuery = mSolrQueryBuilder.create(aDSCriteria);
    solrQuery.setStart(0);
    solrQuery.setRows(1);

    appLogger.debug(String.format("%s: %s %s", aDSCriteria.getName(), mSolrIdentity, solrQuery.toString()));

    QueryResponse queryResponse = queryExecute(solrQuery);
    SolrDocumentList solrDocumentList = queryResponse.getResults();
    documentCount = (int) solrDocumentList.getNumFound();

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return documentCount;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

/**
 * Returns a <i>Document</i> representation of all documents
 * fetched from the underlying content source (using a wildcard
 * criteria).//from  www  .  j a v a 2  s  .  co  m
 * <p>
 * <b>Note:</b> Depending on the size of the content source
 * behind this data source, this method could consume large
 * amounts of heap memory.  Therefore, it should only be
 * used when the number of column and rows is known to be
 * small in size.
 * </p>
 *
 * @return Document hierarchy representing all documents in
 * the content source.
 *
 * @throws com.nridge.core.base.ds.DSException Data source related exception.
 */
@Override
public Document fetch() throws DSException {
    Document solrDocument;
    Logger appLogger = mAppMgr.getLogger(this, "fetch");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    initialize();

    DSCriteria dsCriteria = new DSCriteria("Solr Query");
    dsCriteria.add(Solr.FIELD_QUERY_NAME, Field.Operator.EQUAL, Solr.QUERY_ALL_DOCUMENTS);

    SolrQuery solrQuery = mSolrQueryBuilder.create(dsCriteria);
    solrQuery.setStart(Solr.QUERY_OFFSET_DEFAULT);
    solrQuery.setRows(Solr.QUERY_PAGESIZE_DEFAULT);

    appLogger.debug(String.format("%s: %s %s", dsCriteria.getName(), mSolrIdentity, solrQuery.toString()));

    SolrResponseBuilder solrResponseBuilder = createResponseBuilder();
    QueryResponse queryResponse = queryExecute(solrQuery);
    solrDocument = solrResponseBuilder.extract(queryResponse);
    DataBag headerBag = Solr.getHeader(solrDocument);
    if (headerBag != null)
        headerBag.setValueByName("collection_name", getCollectionName());
    String requestHandler = solrQuery.getRequestHandler();
    solrResponseBuilder.updateHeader(mBaseSolrURL, solrQuery.getQuery(), requestHandler, solrQuery.getStart(),
            solrQuery.getRows());

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return solrDocument;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

/**
 * Returns a <i>Document</i> representation of the documents
 * that match the <i>DSCriteria</i> specified in the parameter.
 * In addition, this method offers a paging mechanism where the
 * starting offset and a fetch limit can be applied to each
 * content fetch query./*from   w w w. ja va2 s  . c  o  m*/
 *
 * @param aDSCriteria Data source criteria.
 * @param anOffset    Starting offset into the matching content rows.
 * @param aLimit      Limit on the total number of rows to extract from
 *                    the content source during this fetch operation.
 *
 * @return Document hierarchy representing all documents that
 * match the criteria in the content source. (based on the offset
 * and limit values).
 *
 * @throws com.nridge.core.base.ds.DSException Data source related exception.
 *
 *  @see <a href="http://lucene.apache.org/solr/guide/7_6/common-query-parameters.html">Solr Common Query Parametersr</a>
 *    @see <a href="https://lucene.apache.org/solr/guide/7_6/the-standard-query-parser.html">Solr Standard Query Parserr</a>
 */
@Override
public Document fetch(DSCriteria aDSCriteria, int anOffset, int aLimit) throws DSException {
    Document solrDocument;
    Logger appLogger = mAppMgr.getLogger(this, "fetch");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    initialize();

    SolrQuery solrQuery = mSolrQueryBuilder.create(aDSCriteria);
    solrQuery.setStart(anOffset);
    solrQuery.setRows(aLimit);

    appLogger.debug(String.format("%s: %s %s", aDSCriteria.getName(), mSolrIdentity, solrQuery.toString()));

    SolrResponseBuilder solrResponseBuilder = createResponseBuilder();
    QueryResponse queryResponse = queryExecute(solrQuery);
    solrDocument = solrResponseBuilder.extract(queryResponse, anOffset, aLimit);
    DataBag headerBag = Solr.getHeader(solrDocument);
    if (headerBag != null)
        headerBag.setValueByName("collection_name", getCollectionName());
    String requestHandler = solrQuery.getRequestHandler();
    solrResponseBuilder.updateHeader(mBaseSolrURL, solrQuery.getQuery(), requestHandler, anOffset, aLimit);
    if (Solr.isCriteriaParentChild(aDSCriteria)) {
        SolrParentChild solrParentChild = new SolrParentChild(mAppMgr, this);
        solrParentChild.expand(solrDocument, aDSCriteria);
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return solrDocument;
}

From source file:com.pearson.openideas.cq5.components.services.solr.SolrHelper.java

License:Open Source License

/**
 * Build a SolrQuery object based on the parameters.
 * //from   w ww . j  a  v  a  2s.  c o  m
 * @param parameters
 *            the search parameters
 * @return the SolrQuery object
 */
public static SolrQuery buildQuery(final SolrSearchParameters parameters) {
    log.debug("Building SolrQuery.");
    log.info("Build the base query.");

    String q = "*:*";

    if (parameters.getQuery() != null) {
        q = parameters.getQuery();
    }
    SolrQuery query = new SolrQuery(q);

    if (parameters.getType() != null) {
        log.debug("set query query type to {}", parameters.getType());
        query.setQueryType(parameters.getType());
    }

    if (parameters.getDisplayCount() > 0) {
        log.debug("set query display count to {}", parameters.getDisplayCount());
        query.setRows(parameters.getDisplayCount());
    }

    if (parameters.getCurrentRecord() > 0) {
        log.debug("set query start to {}", parameters.getCurrentRecord());
        query.setStart(parameters.getCurrentRecord());
    }
    log.info("Done building the base query.");

    log.info("Adding facet to query.");
    addFacet(parameters.getFacetField(), query);
    log.info("Done adding facet to query.");

    log.info("Adding search criteria to query.");
    addSearchCriteria(parameters.getSearchCriteria(), query);
    log.info("Done adding search criteria to query.");

    log.info("Adding filter criteria to query.");
    addFilterCriteria(parameters.getFilterCriteria(), query);
    log.info("Done adding filter criteria to query.");

    log.info("Adding sort fields to query.");
    addSortFields(parameters.getSortFields(), query);
    log.info("Done adding sort fields to query.");

    log.debug("done building SolrQuery, returning.");
    return query;
}

From source file:com.plugtree.solrmeter.model.service.impl.QueryServiceSolrJImpl.java

License:Apache License

protected SolrQuery createQuery(String q, String fq, String qt, boolean highlight, String facetFields,
        String sort, String sortOrder, Integer rows, Integer start, String otherParams) throws QueryException {
    SolrQuery query = new SolrQuery();
    if (q != null) {
        query.setQuery(q);/*from  ww w  . ja v  a 2  s  .co  m*/
    }
    if (fq != null) {
        List<String> filterQueries = this.getFilterQueries(fq);
        for (String filterQuery : filterQueries) {
            query.addFilterQuery(filterQuery);
        }
    }
    if (qt != null) {
        query.setQueryType(qt);
    }
    query.setHighlight(highlight);
    if (facetFields == null || "".equals(facetFields)) {
        query.setFacet(false);
    } else {
        query.setFacet(true);
        List<String> facets = this.getFacets(facetFields);
        for (String facet : facets) {
            query.addFacetField(facet);
        }
    }
    if (sort != null && !"".equals(sort)) {
        query.setSortField(sort, ORDER.valueOf(sortOrder));
    }
    if (rows != null && rows < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (rows != null) {
        query.setRows(rows);
    }
    if (start != null && start < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (start != null) {
        query.setStart(start);
    }

    if (otherParams != null) {
        List<String> params = this.getOtherParams(otherParams);
        for (String param : params) {
            query.add(getParamName(param), getParamValue(param));
        }
    }
    return query;
}