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

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

Introduction

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

Prototype

public SolrQuery setFacet(boolean b) 

Source Link

Document

enable/disable faceting.

Usage

From source file:org.mousephenotype.cda.solr.service.StatisticalResultService.java

License:Apache License

/**
 * This map is needed for the summary on phenotype pages (the percentages &
 * pie chart). It takes a long time to load so it does it asynchronously.
 *
 * @param sex// w ww.  java  2s.c o m
 * @return Map < String parameterStableId , List<String
 *         geneMgiIdWithParameterXMeasured>>
 * @throws SolrServerException, IOException
 * @throws InterruptedException
 * @throws ExecutionException
 * @author tudose
 */
public Map<String, List<String>> getParameterToGeneMap(SexType sex)
        throws SolrServerException, IOException, InterruptedException, ExecutionException {

    Map<String, List<String>> res = new ConcurrentHashMap<>(); //<parameter, <genes>>
    String pivotFacet = StatisticalResultDTO.PARAMETER_STABLE_ID + ","
            + StatisticalResultDTO.MARKER_ACCESSION_ID;
    SolrQuery q = new SolrQuery().setQuery(ObservationDTO.SEX + ":" + sex.name());
    q.setFilterQueries(StatisticalResultDTO.STRAIN_ACCESSION_ID + ":\"" + StringUtils.join(
            OverviewChartsConstants.B6N_STRAINS, "\" OR " + ObservationDTO.STRAIN_ACCESSION_ID + ":\"") + "\"");
    q.set("facet.pivot", pivotFacet);
    q.setFacet(true);
    q.setRows(1);
    q.set("facet.limit", -1);

    logger.info("Solr url for getParameterToGeneMap " + SolrUtils.getBaseURL(statisticalResultCore) + "/select?"
            + q);
    QueryResponse response = statisticalResultCore.query(q);

    for (PivotField pivot : response.getFacetPivot().get(pivotFacet)) {
        if (pivot.getPivot() != null) {
            List<String> genes = new ArrayList<>();
            for (PivotField gene : pivot.getPivot()) {
                genes.add(gene.getValue().toString());
            }
            res.put(pivot.getValue().toString(), new ArrayList<String>(genes));
        }
    }

    return res;
}

From source file:org.mule.modules.SolrConnector.java

License:Apache License

private void applyFacetingLogic(SolrQuery query, List<String> facetFields, int facetLimit, int facetMinCount) {
    if (facetFields == null || facetFields.isEmpty()) {
        logger.debug("Faceting is disabled for this query...");
        return;//  w w w  .j a v  a2s . c  o  m
    }

    query.setFacet(true);
    query.setFacetLimit(facetLimit);
    query.setFacetMinCount(facetMinCount);
    query.addFacetField(facetFields.toArray(EMPTY_STRING_ARRAY));
}

From source file:org.ofbiz.solr.SolrProductSearch.java

License:Apache License

/**
 * Runs a query on the Solr Search Engine and returns the results.
 * <p>//from w  w  w  .  j a  va 2s.  com
 * This function only returns an object of type QueryResponse, so it is probably not a good idea to call it directly from within the
 * groovy files (As a decent example on how to use it, however, use keywordSearch instead).
 */
public static Map<String, Object> runSolrQuery(DispatchContext dctx, Map<String, Object> context) {
    // get Connection
    HttpSolrServer server = null;
    Map<String, Object> result;

    Integer viewIndex = (Integer) context.get("viewIndex");
    Integer viewSize = (Integer) context.get("viewSize");
    if (viewIndex < 1) {
        viewIndex = 1;
    }
    try {
        server = new HttpSolrServer(SolrUtil.solrUrl);
        // create Query Object
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery((String) context.get("query"));
        // solrQuery.setQueryType("dismax");
        boolean faceted = (Boolean) context.get("facet");
        if (faceted) {
            solrQuery.setFacet(faceted);
            //solrQuery.addFacetField("manu");
            solrQuery.addFacetField("features");
            solrQuery.addFacetField("cat");
            solrQuery.setFacetMinCount(1);
            solrQuery.setFacetLimit(8);

            solrQuery.addFacetQuery("listPrice:[0 TO 50]");
            solrQuery.addFacetQuery("listPrice:[50 TO 100]");
            solrQuery.addFacetQuery("listPrice:[100 TO 250]");
            solrQuery.addFacetQuery("listPrice:[250 TO 500]");
            solrQuery.addFacetQuery("listPrice:[500 TO 1000]");
            solrQuery.addFacetQuery("listPrice:[1000 TO 2500]");
            solrQuery.addFacetQuery("listPrice:[2500 TO 5000]");
            solrQuery.addFacetQuery("listPrice:[5000 TO 10000]");
            solrQuery.addFacetQuery("listPrice:[10000 TO 50000]");
            solrQuery.addFacetQuery("listPrice:[50000 TO *]");
        }

        boolean spellCheck = (Boolean) context.get("spellcheck");
        if (spellCheck) {
            solrQuery.setParam("spellcheck", spellCheck);
        }

        boolean highLight = (Boolean) context.get("highlight");
        if (highLight) {
            solrQuery.setHighlight(highLight);
            solrQuery.setHighlightSimplePre("<span class=\"highlight\">");
            solrQuery.addHighlightField("description");
            solrQuery.setHighlightSimplePost("</span>");
            solrQuery.setHighlightSnippets(2);
        }

        // Set additional Parameter
        // SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (viewIndex != null && viewIndex > 0) {
            solrQuery.setStart((viewIndex - 1) * viewSize);
        }
        if (viewSize != null && viewSize > 0) {
            solrQuery.setRows(viewSize);
        }

        // if ((List) context.get("queryFilter") != null && ((ArrayList<SolrDocument>) context.get("queryFilter")).size() > 0) {
        // List filter = (List) context.get("queryFilter");
        // String[] tn = new String[filter.size()];
        // Iterator it = filter.iterator();
        // for (int i = 0; i < filter.size(); i++) {
        // tn[i] = (String) filter.get(i);
        // }
        // solrQuery.setFilterQueries(tn);
        // }
        String queryFilter = (String) context.get("queryFilter");
        if (UtilValidate.isNotEmpty(queryFilter))
            solrQuery.setFilterQueries(queryFilter.split(" "));
        if ((String) context.get("returnFields") != null) {
            solrQuery.setFields((String) context.get("returnFields"));
        }

        // if((Boolean)context.get("sortByReverse"))order.reverse();
        if ((String) context.get("sortBy") != null && ((String) context.get("sortBy")).length() > 0) {
            SolrQuery.ORDER order;
            if (!((Boolean) context.get("sortByReverse")))
                order = SolrQuery.ORDER.asc;
            else
                order = SolrQuery.ORDER.desc;
            solrQuery.setSort(((String) context.get("sortBy")).replaceFirst("-", ""), order);
        }

        if ((String) context.get("facetQuery") != null) {
            solrQuery.addFacetQuery((String) context.get("facetQuery"));
        }

        QueryResponse rsp = server.query(solrQuery);
        result = ServiceUtil.returnSuccess();
        result.put("queryResult", rsp);
    } catch (Exception e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    }
    return result;
}

From source file:org.ofbiz.solr.SolrUtil.java

License:Apache License

public static Map<String, Object> categoriesAvailable(String catalogId, String categoryId, String productId,
        String facetPrefix, boolean displayproducts, int viewIndex, int viewSize) {
    // create the data model
    Map<String, Object> result = FastMap.newInstance();
    HttpSolrServer server = null;//from w  ww.j a  v a2  s .co  m
    QueryResponse returnMap = new QueryResponse();
    try {
        // do the basic query
        server = new HttpSolrServer(solrUrl);
        // create Query Object
        String query = "inStock[1 TO *]";
        if (categoryId != null)
            query += " +cat:" + categoryId;
        else if (productId != null)
            query += " +productId:" + productId;
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(query);

        if (catalogId != null)
            solrQuery.setFilterQueries("catalog:" + catalogId);
        if (displayproducts) {
            if (viewSize > -1) {
                solrQuery.setRows(viewSize);
            } else
                solrQuery.setRows(50000);
            if (viewIndex > -1) {
                solrQuery.setStart(viewIndex);
            }
        } else {
            solrQuery.setFields("cat");
            solrQuery.setRows(0);
        }

        if (UtilValidate.isNotEmpty(facetPrefix)) {
            solrQuery.setFacetPrefix(facetPrefix);
        }

        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        solrQuery.addFacetField("cat");
        solrQuery.setFacetLimit(-1);
        Debug.logVerbose("solr: solrQuery: " + solrQuery, module);
        returnMap = server.query(solrQuery, METHOD.POST);
        result.put("rows", returnMap);
        result.put("numFound", returnMap.getResults().getNumFound());
    } catch (Exception e) {
        Debug.logError(e.getMessage(), module);
    }
    return result;
}

From source file:org.openbrr.search.solr.ObrrSolrServer.java

public static List<ProjectIndexerData> query(String _query) {

    List<ProjectIndexerData> results = new ArrayList<ProjectIndexerData>();

    SolrQuery sq = new SolrQuery();
    sq.setQuery(_query);/*  w w w .  j  a  va 2  s  .  co  m*/
    sq.setFacet(true);
    sq.addFacetField(IndexFields.ID);
    sq.addFacetField(IndexFields.NAME);
    sq.addFacetField(IndexFields.DESC);
    sq.addFacetField(IndexFields.URL);
    sq.addFacetField(IndexFields.LIC_IDS);
    sq.addFacetField(IndexFields.TOPIC_IDS);
    sq.addFacetField(IndexFields.OS_IDS);
    sq.addFacetField(IndexFields.AUD_IDS);
    sq.addFacetField(IndexFields.PL_IDS);
    sq.addFacetField(IndexFields.DS_IDS);

    //sq.setIncludeScore(true);

    try {
        QueryResponse qr = server.query(sq);

        SolrDocumentList sdl = qr.getResults();

        for (SolrDocument d : sdl) {
            ProjectIndexerData data = new ProjectIndexerData((Integer) d.getFieldValue(IndexFields.ID),
                    (String) d.getFieldValue(IndexFields.NAME), (String) d.getFieldValue(IndexFields.DESC));

            data.setLicenseIds(stringToList((String) (d.getFieldValue(IndexFields.LIC_IDS))));
            data.setOsIds(stringToList((String) (d.getFieldValue(IndexFields.OS_IDS))));
            data.setTopicIds(stringToList((String) (d.getFieldValue(IndexFields.TOPIC_IDS))));
            data.setAudienceIds(stringToList((String) (d.getFieldValue(IndexFields.AUD_IDS))));
            data.setProgLangIds(stringToList((String) (d.getFieldValue(IndexFields.PL_IDS))));
            data.setDataStoreIds(stringToList((String) (d.getFieldValue(IndexFields.DS_IDS))));

            results.add(data);
        }
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.opencastproject.workflow.impl.WorkflowServiceSolrIndex.java

License:Educational Community License

/**
 * {@inheritDoc}/*from   w w  w  .  j  a va2  s .  co  m*/
 * 
 * @see org.opencastproject.workflow.impl.WorkflowServiceIndex#getStatistics()
 */
@Override
public WorkflowStatistics getStatistics() throws WorkflowDatabaseException {

    long total = 0;
    long paused = 0;
    long failed = 0;
    long failing = 0;
    long instantiated = 0;
    long running = 0;
    long stopped = 0;
    long succeeded = 0;

    WorkflowStatistics stats = new WorkflowStatistics();

    // Get all definitions and then query for the numbers and the current operation per definition
    try {
        String orgId = securityService.getOrganization().getId();
        StringBuilder queryString = new StringBuilder().append(ORG_KEY).append(":")
                .append(escapeQueryChars(orgId));
        appendSolrAuthFragment(queryString, WRITE_PERMISSION);
        SolrQuery solrQuery = new SolrQuery(queryString.toString());
        solrQuery.addFacetField(WORKFLOW_DEFINITION_KEY);
        solrQuery.addFacetField(OPERATION_KEY);
        solrQuery.setFacetMinCount(0);
        solrQuery.setFacet(true);
        QueryResponse response = solrServer.query(solrQuery);

        FacetField templateFacet = response.getFacetField(WORKFLOW_DEFINITION_KEY);
        FacetField operationFacet = response.getFacetField(OPERATION_KEY);

        // For every template and every operation
        if (templateFacet != null && templateFacet.getValues() != null) {

            for (Count template : templateFacet.getValues()) {

                WorkflowDefinitionReport templateReport = new WorkflowDefinitionReport();
                templateReport.setId(template.getName());

                long templateTotal = 0;
                long templatePaused = 0;
                long templateFailed = 0;
                long templateFailing = 0;
                long templateInstantiated = 0;
                long templateRunning = 0;
                long templateStopped = 0;
                long templateSucceeded = 0;

                if (operationFacet != null && operationFacet.getValues() != null) {

                    for (Count operation : operationFacet.getValues()) {

                        OperationReport operationReport = new OperationReport();
                        operationReport.setId(operation.getName());

                        StringBuilder baseSolrQuery = new StringBuilder().append(ORG_KEY).append(":")
                                .append(escapeQueryChars(orgId));
                        appendSolrAuthFragment(baseSolrQuery, WRITE_PERMISSION);
                        solrQuery = new SolrQuery(baseSolrQuery.toString());
                        solrQuery.addFacetField(STATE_KEY);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.FAILING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.INSTANTIATED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.PAUSED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.RUNNING);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.STOPPED);
                        solrQuery.addFacetQuery(STATE_KEY + ":" + WorkflowState.SUCCEEDED);
                        solrQuery.addFilterQuery(WORKFLOW_DEFINITION_KEY + ":" + template.getName());
                        solrQuery.addFilterQuery(OPERATION_KEY + ":" + operation.getName());
                        solrQuery.setFacetMinCount(0);
                        solrQuery.setFacet(true);

                        response = solrServer.query(solrQuery);

                        // Add the states
                        FacetField stateFacet = response.getFacetField(STATE_KEY);
                        for (Count stateValue : stateFacet.getValues()) {
                            WorkflowState state = WorkflowState.valueOf(stateValue.getName().toUpperCase());
                            templateTotal += stateValue.getCount();
                            total += stateValue.getCount();
                            switch (state) {
                            case FAILED:
                                operationReport.setFailed(stateValue.getCount());
                                templateFailed += stateValue.getCount();
                                failed += stateValue.getCount();
                                break;
                            case FAILING:
                                operationReport.setFailing(stateValue.getCount());
                                templateFailing += stateValue.getCount();
                                failing += stateValue.getCount();
                                break;
                            case INSTANTIATED:
                                operationReport.setInstantiated(stateValue.getCount());
                                templateInstantiated += stateValue.getCount();
                                instantiated += stateValue.getCount();
                                break;
                            case PAUSED:
                                operationReport.setPaused(stateValue.getCount());
                                templatePaused += stateValue.getCount();
                                paused += stateValue.getCount();
                                break;
                            case RUNNING:
                                operationReport.setRunning(stateValue.getCount());
                                templateRunning += stateValue.getCount();
                                running += stateValue.getCount();
                                break;
                            case STOPPED:
                                operationReport.setStopped(stateValue.getCount());
                                templateStopped += stateValue.getCount();
                                stopped += stateValue.getCount();
                                break;
                            case SUCCEEDED:
                                operationReport.setFinished(stateValue.getCount());
                                templateSucceeded += stateValue.getCount();
                                succeeded += stateValue.getCount();
                                break;
                            default:
                                throw new IllegalStateException("State '" + state + "' is not handled");
                            }
                        }

                        templateReport.getOperations().add(operationReport);
                    }
                }

                // Update the template statistics
                templateReport.setTotal(templateTotal);
                templateReport.setFailed(templateFailed);
                templateReport.setFailing(templateFailing);
                templateReport.setInstantiated(templateInstantiated);
                templateReport.setPaused(templatePaused);
                templateReport.setRunning(templateRunning);
                templateReport.setStopped(templateStopped);
                templateReport.setFinished(templateSucceeded);

                // Add the definition report to the statistics
                stats.getDefinitions().add(templateReport);

            }

        }
    } catch (SolrServerException e) {
        throw new WorkflowDatabaseException(e);
    }

    stats.setTotal(total);
    stats.setFailed(failed);
    stats.setFailing(failing);
    stats.setInstantiated(instantiated);
    stats.setPaused(paused);
    stats.setRunning(running);
    stats.setStopped(stopped);
    stats.setFinished(succeeded);

    return stats;
}

From source file:org.opencms.search.solr.TestSolrSearch.java

License:Open Source License

/**
 * @throws Throwable if something goes wrong
 */// ww  w. ja  v  a  2  s. co m
public void testAdvancedFacetting() throws Throwable {

    echo("Testing facet query count");

    // creating the query: facet=true&facet.field=Title_exact&facet.mincount=1&facet.query=text:OpenCms&rows=0
    SolrQuery query = new CmsSolrQuery(getCmsObject(), null);
    // facet=true
    query.setFacet(true);
    // facet.field=Title_exact
    query.addFacetField("Title_exact");
    // facet.mincount=1
    query.add("facet.mincount", "1");
    // facet.query=text:OpenCms
    query.addFacetQuery("text:OpenCms");
    // facet.query=Title_prop:OpenCms
    query.addFacetQuery("Title_prop:OpenCms");
    // rows=0
    query.setRows(new Integer(0));

    CmsSolrIndex index = OpenCms.getSearchManager().getIndexSolr(AllTests.SOLR_ONLINE);
    CmsSolrResultList results = index.search(getCmsObject(), query);
    long facetTextCount = results.getFacetQuery().get("text:OpenCms").intValue();
    long facetTitleCount = results.getFacetQuery().get("Title_prop:OpenCms").intValue();
    echo("Found '" + results.getFacetField("Title_exact").getValueCount()
            + "' facets for the field \"Title_exact\" and '" + facetTextCount
            + "' of them containing the word: \"OpenCms\" in the field 'text' and '" + facetTitleCount
            + "' of them containing the word \"OpenCms\" in the field 'Title_prop!'");

    query = new CmsSolrQuery(getCmsObject(), CmsRequestUtil.createParameterMap("q=text:OpenCms"));
    results = index.search(getCmsObject(), query);
    long numExpected = results.getNumFound();

    assertEquals(numExpected, facetTextCount);
    echo("Great Solr works fine!");
}

From source file:org.openmrs.module.chartsearch.solr.ChartSearchSearcher.java

License:Mozilla Public License

public List<ChartListItem> getDocumentList(Integer patientId, String searchText, Integer start, Integer length,
        List<String> selectedCategories) throws Exception {
    SolrServer solrServer = SolrSingleton.getInstance().getServer();
    SolrQuery query = new SolrQuery();
    List<ChartListItem> list = new ArrayList<ChartListItem>();
    ChartSearchNonFacetFiltering nonFaceting = new ChartSearchNonFacetFiltering();

    searchText = StringUtils.isNotBlank(searchText) ? searchText : "*";
    //check for existence of characters such as ", and : in the search text and submit as it is if so
    if (searchText.contains("\"")) {
        String searchWhole = "text:" + searchText;
        query.setQuery(searchWhole);//from   ww  w .ja  va2s.  c om
    } else if (searchText.contains(":")) {
        query.setQuery(searchText);
    } else if (searchText.equals("*")) {
        query.setQuery(String.format("text:(%s)", searchText));
    } else {
        ChartSearchSyntax searchSyntax = new ChartSearchSyntax(searchText);
        searchText = searchSyntax.getSearchQuery();

        if (StringUtils.isNumeric(searchText)) {
            //this allows 36 returning 36.* for numerics
            searchText = searchText + ".*" + " || " + searchText;
        }
        query.setQuery(String.format("text:(%s)", searchText));
    }

    query.addFilterQuery(String.format("person_id:%d", patientId));
    addSelectedFilterQueriesToQuery(query, selectedCategories);
    query.setStart(start);
    query.setRows(length);
    query.setHighlight(true).setHighlightSnippets(1).setHighlightSimplePre("<b>")
            .setHighlightSimplePost("</b>");
    query.setParam("hl.fl", "text");

    query.remove(FacetParams.FACET_FIELD);
    query.setFacet(true);
    //adding facet field for concept_class
    query.addFacetField("concept_class_name");

    nonFaceting.applyNonFacetingLogicWhileSearching(patientId, searchText, selectedCategories, solrServer,
            query, list);

    return list;
}

From source file:org.pentaho.di.trans.steps.solrinput.SolrInput.java

License:Apache License

/**
 * Once the transformation starts executing, the processRow() method is called repeatedly
 * by PDI for as long as it returns true. To indicate that a step has finished processing rows
 * this method must call setOutputDone() and return false;
 * /*  www . j ava  2s . c om*/
 * Steps which process incoming rows typically call getRow() to read a single row from the
 * input stream, change or add row content, call putRow() to pass the changed row on 
 * and return true. If getRow() returns null, no more rows are expected to come in, 
 * and the processRow() implementation calls setOutputDone() and returns false to
 * indicate that it is done too.
 * 
 * Steps which generate rows typically construct a new row Object[] using a call to
 * RowDataUtil.allocateRowData(numberOfFields), add row content, and call putRow() to
 * pass the new row on. Above process may happen in a loop to generate multiple rows,
 * at the end of which processRow() would call setOutputDone() and return false;
 * 
 * @param smi the step meta interface containing the step settings
 * @param sdi the step data interface that should be used to store
 * 
 * @return true to indicate that the function should be called again, false if the step is done
 */
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {

    if (first) {
        first = false;
        // Create the output row meta-data
        data.outputRowMeta = new RowMeta();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // For String to <type> conversions, we allocate a conversion meta data row as well...
        data.convertRowMeta = data.outputRowMeta.cloneToType(ValueMetaInterface.TYPE_STRING);

        // get real values
        boolean tryCursor = true;
        Integer startRecord = 0;
        Integer chunkRowSize = 20;
        String realURL = meta.getURL();
        String realQ = meta.getQ();
        String realSort = meta.getSort();
        String realCursor = meta.getCursor();
        String realFq = meta.getFq();
        String realFl = meta.getFl();
        String realFacetQuery = meta.getFacetQuery();
        String realFacetField = meta.getFacetField();
        /* Send and Get the report */
        SolrQuery query = new SolrQuery();
        query.set("rows", chunkRowSize);
        if (realQ != null && !realQ.equals("")) {
            query.set("q", realQ);
        }
        if (realSort != null && !realSort.equals("")) {
            query.set("sort", realSort);
        } else {
            tryCursor = false;
        }
        if (realCursor != null && !realCursor.equals("")) {
            if (realCursor.equals("No")) {
                tryCursor = false;
            }
        }
        if (realFl != null && !realFl.equals("")) {
            query.set("fl", realFl);
        }
        if (realFq != null && !realFq.equals("")) {
            query.set("fq", realFq);
        }
        if (realFacetField != null && !realFacetField.equals("")) {
            //TODO incorporate multiple facet fields at once
            //String[] facetFields = realFacetField.split("\\s*,\\s*");
            //for(int i =0; i < facetFields.length; i++){
            query.addFacetField(realFacetField);
            //}
            query.setFacet(true);
            query.setFacetLimit(-1);
            query.setFacetMinCount(0);
            query.setFacetSort("count");
            query.set("rows", 0);
            tryCursor = false;
            data.facetRequested = true;
        }
        if (realFacetQuery != null && !realFacetQuery.equals("")) {
            query.addFacetQuery(realFacetQuery);
        }
        // You can't use "TimeAllowed" with "CursorMark"
        // The documentation says "Values <= 0 mean 
        // no time restriction", so setting to 0.
        query.setTimeAllowed(0);
        HttpSolrServer solr = new HttpSolrServer(realURL);
        String cursorMark = CursorMarkParams.CURSOR_MARK_START;
        boolean done = false;
        QueryResponse rsp = null;
        while (!done) {
            if (tryCursor) {
                query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
            } else {
                query.setStart(startRecord);
            }
            try {
                rsp = solr.query(query);
            } catch (SolrServerException e) {
                e.printStackTrace();
            }
            if (data.facetRequested) {
                data.facetCountName = rsp.getFacetFields().get(0).getName();
                data.facetCounts = rsp.getFacetFields().get(0).getValues();
                done = true;
            } else {
                SolrDocumentList theseDocs = rsp.getResults();
                for (SolrDocument doc : theseDocs) {
                    data.documentList.add(doc);
                }
            }
            if (tryCursor) {
                String nextCursorMark = rsp.getNextCursorMark();
                if (cursorMark.equals(nextCursorMark)) {
                    done = true;
                } else {
                    cursorMark = nextCursorMark;
                }
            } else {
                startRecord = startRecord + chunkRowSize;
                if (startRecord >= rsp.getResults().getNumFound()) {
                    done = true;
                }
            }
        }
    }

    Object[] outputRowData = null;

    try {
        if (data.facetRequested) {
            // get one row if we can
            if (data.facetCounts.size() - 1 < data.recordIndex) {
                setOutputDone();
                return false;
            }
            FacetField.Count facetRecord = data.facetCounts.get(data.recordIndex);
            outputRowData = prepareFacetRecord(facetRecord);
        } else {
            // get one row if we can
            if (data.documentList.size() - 1 < data.recordIndex) {
                setOutputDone();
                return false;
            }
            SolrDocument record = data.documentList.get(data.recordIndex);
            outputRowData = prepareRecord(record);
        }
        putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s);
        data.recordIndex++;
        return true;
    } catch (KettleException e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;
        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            logError(BaseMessages.getString(PKG, "SolrInput.log.Exception", e.getMessage()));
            logError(Const.getStackTracker(e));
            setErrors(1);
            stopAll();
            setOutputDone(); // signal end to receiver(s)
            return false;
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), outputRowData, 1, errorMessage, null, "SolrInput001");
        }
    }
    return true;
}

From source file:org.pentaho.di.ui.trans.steps.solrinput.SolrInputDialog.java

License:Apache License

private void getFields() {

    try {/*from   w  w  w.  ja  v a  2 s.com*/
        SolrInputMeta meta = new SolrInputMeta();
        getInfo(meta);
        // clear the current fields grid
        wFields.removeAll();
        // get real values
        boolean tryCursor = true;
        boolean facetRequested = false;
        Integer startRecord = 0;
        Integer chunkRowSize = 20;
        String realURL = transMeta.environmentSubstitute(meta.getURL());
        String realQ = transMeta.environmentSubstitute(meta.getQ());
        String realSort = transMeta.environmentSubstitute(meta.getSort());
        String realCursor = transMeta.environmentSubstitute(meta.getCursor());
        String realFq = transMeta.environmentSubstitute(meta.getFq());
        String realFl = transMeta.environmentSubstitute(meta.getFl());
        String realFacetQuery = transMeta.environmentSubstitute(meta.getFacetQuery());
        String realFacetField = transMeta.environmentSubstitute(meta.getFacetField());
        /* Send and Get the report */
        SolrQuery query = new SolrQuery();
        query.set("rows", chunkRowSize);
        if (realQ != null && !realQ.equals("")) {
            query.set("q", realQ);
        }
        if (realSort != null && !realSort.equals("")) {
            query.set("sort", realSort);
        } else {
            tryCursor = false;
        }
        if (realCursor != null && !realCursor.equals("")) {
            if (realCursor.equals("No")) {
                tryCursor = false;
            }
        }
        if (realFl != null && !realFl.equals("")) {
            query.set("fl", realFl);
        }
        if (realFq != null && !realFq.equals("")) {
            query.set("fq", realFq);
        }
        if (realFacetField != null && !realFacetField.equals("")) {
            //TODO incorporate multiple facet fields at once
            //String[] facetFields = realFacetField.split("\\s*,\\s*");
            //for(int i =0; i < facetFields.length; i++){
            query.addFacetField(realFacetField);
            //}
            query.setFacet(true);
            query.setFacetLimit(-1);
            query.setFacetMinCount(0);
            query.setFacetSort("count");
            query.set("rows", 0);
            tryCursor = false;
            facetRequested = true;
        }
        if (realFacetQuery != null && !realFacetQuery.equals("")) {
            query.addFacetQuery(realFacetQuery);
        }
        // You can't use "TimeAllowed" with "CursorMark"
        // The documentation says "Values <= 0 mean 
        // no time restriction", so setting to 0.
        query.setTimeAllowed(0);
        HttpSolrServer solr = new HttpSolrServer(realURL);
        String cursorMark = CursorMarkParams.CURSOR_MARK_START;
        boolean done = false;
        List<String> headerNames = new ArrayList<String>();
        QueryResponse rsp = null;
        while (!done) {
            if (tryCursor) {
                query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
            } else {
                query.setStart(startRecord);
            }
            try {
                rsp = solr.query(query);
            } catch (SolrServerException e) {
                e.printStackTrace();
            }
            if (facetRequested) {
                headerNames.add(rsp.getFacetFields().get(0).getName());
                headerNames.add("count");
                done = true;
            } else {
                SolrDocumentList docs = rsp.getResults();
                for (SolrDocument doc : docs) {
                    Collection<String> thisNamesArray = doc.getFieldNames();
                    String[] a = thisNamesArray.toArray(new String[thisNamesArray.size()]);
                    for (int j = 0; j < a.length; j++) {
                        if (!headerNames.contains(a[j])) {
                            headerNames.add(a[j]);
                        }
                    }
                }
            }
            if (tryCursor) {
                String nextCursorMark = rsp.getNextCursorMark();
                if (cursorMark.equals(nextCursorMark)) {
                    done = true;
                } else {
                    cursorMark = nextCursorMark;
                }
            } else {
                startRecord = startRecord + chunkRowSize;
                if (startRecord >= rsp.getResults().getNumFound()) {
                    done = true;
                }
            }
        }
        getTableView().table.setItemCount(headerNames.size());
        for (int j = 0; j < headerNames.size(); j++) {
            TableItem item = getTableView().table.getItem(j);
            item.setText(1, headerNames.get(j));
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);
        getInput().setChanged();
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "SolrInputMeta.ErrorRetrieveData.DialogTitle"),
                BaseMessages.getString(PKG, "SolrInputMeta.ErrorRetrieveData.DialogMessage"), e);
    }
}