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

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

Introduction

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

Prototype

public SolrQuery setFacetSort(String sort) 

Source Link

Document

set facet sort

Usage

From source file:org.kuali.ole.docstore.engine.service.rest.DocstoreRestClient_UT.java

License:Open Source License

public void searchBibWithFacets(String facetSort) throws Exception {
    String[] facets = { "Author_facet", "Format_facet", "Genre_facet", "Language_facet",
            "PublicationDate_facet", "PublicationDate_sort" };
    String[] fieldList = { "LocalId_display", "Title_sort", "Title_display", "JournalTitle_display",
            "Author_display", "Publisher_display", "ISBN_display", "ISSN_display", "Subject_display",
            "Publisher_display", "PublicationDate_display", "Edition_display", "Format_display",
            "Language_display", "Description_display", "FormGenre_display", "DocFormat", "staffOnlyFlag",
            "bibIdentifier", "holdingsIdentifier" };
    String args = "(DocType:bibliographic)AND((*:*))";
    SolrServer solr = SolrServerManager.getInstance().getSolrServer();
    SolrQuery query = new SolrQuery();
    query.setQuery(args);/*  w w w.  j  a  va 2  s . co m*/
    query.setFacet(true);
    query.addFacetField(facets);
    query.setFacetMinCount(1);
    query.setFacetSort(facetSort);
    query.setFacetLimit(5);
    query.setFields(fieldList);
    query.set("facet.offset", "0");
    QueryResponse response = solr.query(query);
    System.out.println("Search result count " + response.getResults().getNumFound()
            + " and  time taken to search is " + response.getQTime());
}

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;
 * /* w  ww  .  ja va  2s . c o m*/
 * 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 {/*ww w  .  j a v  a  2 s . c o m*/
        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);
    }
}

From source file:org.roda.core.index.utils.SolrUtils.java

private static void parseAndConfigureFacets(Facets facets, SolrQuery query) {
    if (facets != null) {
        query.setFacetSort(getSolrFacetParameterSortValue(FacetParameter.DEFAULT_SORT));
        if (!"".equals(facets.getQuery())) {
            query.addFacetQuery(facets.getQuery());
        }/*w w  w . j  a v  a 2 s.  c  om*/
        StringBuilder filterQuery = new StringBuilder();
        for (Entry<String, FacetParameter> parameter : facets.getParameters().entrySet()) {
            FacetParameter facetParameter = parameter.getValue();
            setSolrFacetParameterSort(query, facetParameter);

            if (facetParameter instanceof SimpleFacetParameter) {
                setQueryFacetParameter(query, (SimpleFacetParameter) facetParameter);
                appendValuesUsingOROperator(filterQuery, facetParameter.getName(),
                        ((SimpleFacetParameter) facetParameter).getValues(), true);
            } else {
                LOGGER.error("Unsupported facet parameter class: {}", facetParameter.getClass().getName());
            }
        }
        if (filterQuery.length() > 0) {
            query.addFilterQuery(filterQuery.toString());
            LOGGER.trace("Query after defining facets: {}", query);
        }
    }
}

From source file:org.springframework.data.solr.core.DefaultQueryParser.java

License:Apache License

private boolean enableFaceting(SolrQuery solrQuery, FacetQuery query) {
    FacetOptions facetOptions = query.getFacetOptions();
    if (facetOptions == null || (!facetOptions.hasFields() && !facetOptions.hasFacetQueries()
            && !facetOptions.hasPivotFields())) {
        return false;
    }//from  www.  ja va  2 s  . com
    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(facetOptions.getFacetMinCount());
    solrQuery.setFacetLimit(facetOptions.getPageable().getPageSize());
    if (facetOptions.getPageable().getPageNumber() > 0) {
        int offset = Math.max(0, facetOptions.getPageable().getOffset());
        solrQuery.set(FacetParams.FACET_OFFSET, offset);
    }
    if (FacetOptions.FacetSort.INDEX.equals(facetOptions.getFacetSort())) {
        solrQuery.setFacetSort(FacetParams.FACET_SORT_INDEX);
    }
    return true;
}

From source file:org.wso2.carbon.registry.indexing.solr.SolrClient.java

License:Open Source License

private String addFacetFields(Map<String, String> fields, SolrQuery query) {
    //set the facet true to enable facet
    //Need to set the Facet to true to enable Facet Query.
    query.setFacet(true);/*from ww w  .  j a  va  2 s . c o m*/
    String fieldName = fields.get(IndexingConstants.FACET_FIELD_NAME);
    String queryField = null;
    if (fieldName != null) {
        //set the field for the facet
        if (IndexingConstants.FIELD_TAGS.equals(fieldName) || IndexingConstants.FIELD_COMMENTS.equals(fieldName)
                || IndexingConstants.FIELD_ASSOCIATION_DESTINATIONS.equals(fieldName)
                || IndexingConstants.FIELD_ASSOCIATION_TYPES.equals(fieldName)) {
            queryField = fieldName + SolrConstants.SOLR_MULTIVALUED_STRING_FIELD_KEY_SUFFIX;
            query.addFacetField(queryField);
        } else {
            queryField = fieldName + SolrConstants.SOLR_STRING_FIELD_KEY_SUFFIX;
            query.addFacetField(queryField);
        }
        //remove the facet field avoid affecting to query results
        fields.remove(IndexingConstants.FACET_FIELD_NAME);
        //set the limit for the facet
        if (fields.get(IndexingConstants.FACET_LIMIT) != null) {
            query.setFacetLimit(Integer.parseInt(fields.get(IndexingConstants.FACET_LIMIT)));
            fields.remove(IndexingConstants.FACET_LIMIT);
        } else {
            query.setFacetLimit(IndexingConstants.FACET_LIMIT_DEFAULT);
        }
        //set the min count for the facet
        if (fields.get(IndexingConstants.FACET_MIN_COUNT) != null) {
            query.setFacetMinCount(Integer.parseInt(fields.get(IndexingConstants.FACET_MIN_COUNT)));
            fields.remove(IndexingConstants.FACET_MIN_COUNT);
        } else {
            query.setFacetMinCount(IndexingConstants.FACET_MIN_COUNT_DEFAULT);
        }
        //set the sort value for facet: possible values : index or count
        if (fields.get(IndexingConstants.FACET_SORT) != null) {
            query.setFacetSort(fields.get(IndexingConstants.FACET_SORT));
            fields.remove(IndexingConstants.FACET_SORT);
        }
        // set the prefix value for facet
        if (fields.get(IndexingConstants.FACET_PREFIX) != null) {
            query.setFacetPrefix(fields.get(IndexingConstants.FACET_PREFIX));
            fields.remove(IndexingConstants.FACET_PREFIX);
        }
    }
    return queryField;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.IntactSolrSearcher.java

License:Apache License

public Multimap<String, InteractorIdCount> searchInteractors(SolrQuery originalQuery,
        IntactFacetField[] intactFacetFields) throws IntactSolrException {
    SolrQuery query = originalQuery.getCopy();
    query.setRows(0);/*w ww .jav  a  2  s  .co m*/

    // we allow faceting
    query.setFacet(true);

    // we want all the facet fields with min count = 1. The facet fields with count = 0 are not interesting
    query.setFacetMinCount(1);

    Multimap<String, InteractorIdCount> interactors = HashMultimap.create();

    if (intactFacetFields != null) {

        // we sort the results : the biggest count first
        query.setFacetSort(FacetParams.FACET_SORT_COUNT);

        for (IntactFacetField facetField : intactFacetFields) {
            final String fieldName = createFieldName(facetField.getFieldName());

            // important optimization. We don't want to return all the fields, only a certain number for pagination
            if (facetField.getFirst() != null) {
                query.set("f." + fieldName + ".facet.offset", facetField.getFirst());
            }
            if (facetField.getMax() != null) {
                query.set("f." + fieldName + ".facet.limit", facetField.getMax());
            }

            query.addFacetField(fieldName);
        }
    }

    QueryResponse queryResponse = executeQuery(query);

    List<FacetField> facetFields = queryResponse.getFacetFields();

    if (facetFields == null || facetFields.isEmpty()) {
        return interactors;
    }

    for (FacetField ff : facetFields) {
        if (ff != null && ff.getValues() != null) {
            for (FacetField.Count c : ff.getValues()) {
                interactors.put(extractInteractorTypeFromFieldName(ff.getName()),
                        new InteractorIdCount(c.getName(), c.getCount()));
            }
        }
    }

    return interactors;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.IntactSolrSearcher.java

License:Apache License

public Map<String, Integer> countAllInteractors(SolrQuery originalQuery, String[] interactorTypeMis)
        throws IntactSolrException {
    boolean endOfFacetResults = false;

    int firstResults = 0;

    Map<String, Integer> results = new HashMap<String, Integer>();

    while (!endOfFacetResults) {
        int chunkResults = 0;

        SolrQuery query = originalQuery.getCopy();
        query.setRows(0);//ww w.  j a  v a2 s .  co  m

        // we allow faceting
        query.setFacet(true);

        // we want all the facet fields with min count = 1. The facet fields with count = 0 are not interesting
        query.setFacetMinCount(1);

        // important optimization. We don't want to return all the fields, only a certain number for pagination
        query.set(FacetParams.FACET_OFFSET, firstResults);
        query.setFacetLimit(CHUNK_FACET_THRESHOLD);

        // we sort the results : the biggest count first
        query.setFacetSort(FacetParams.FACET_SORT_COUNT);

        for (String mi : interactorTypeMis) {
            final String fieldName = createFieldName(mi);

            query.addFacetField(fieldName);
        }

        QueryResponse queryResponse = executeQuery(query);

        List<FacetField> facetFields = queryResponse.getFacetFields();
        if (facetFields == null || facetFields.isEmpty()) {
            endOfFacetResults = true;
        } else {
            for (FacetField facetField : facetFields) {
                if (facetField.getValueCount() > 0) {
                    chunkResults += facetField.getValueCount();

                    if (results.containsKey(facetField.getName())) {
                        int current = results.get(facetField.getName());
                        results.put(facetField.getName(), current + facetField.getValueCount());
                    } else {
                        results.put(facetField.getName(), facetField.getValueCount());
                    }
                } else if (!results.containsKey(facetField.getName())) {
                    results.put(facetField.getName(), 0);
                }
            }

            if (chunkResults < CHUNK_FACET_THRESHOLD) {
                endOfFacetResults = true;
            }
        }

        firstResults += CHUNK_FACET_THRESHOLD;
    }

    return results;
}