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

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

Introduction

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

Prototype

public SolrQuery setTimeAllowed(Integer milliseconds) 

Source Link

Document

Set the maximum time allowed for this query.

Usage

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;
 * //from   w  w w.  ja  v  a  2  s  .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 {//from  w  w  w  . j  av  a2  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.sleuthkit.autopsy.keywordsearch.TermComponentQuery.java

License:Open Source License

protected SolrQuery createQuery() {
    final SolrQuery q = new SolrQuery();
    q.setRequestHandler(TERMS_HANDLER);/*from   w w  w. j a v  a2s.  c  o m*/
    q.setTerms(true);
    q.setTermsLimit(TERMS_UNLIMITED);
    q.setTermsRegexFlag("case_insensitive"); //NON-NLS
    //q.setTermsLimit(200);
    //q.setTermsRegexFlag(regexFlag);
    //q.setTermsRaw(true);
    q.setTermsRegex(queryEscaped);
    q.addTermsField(TERMS_SEARCH_FIELD);
    q.setTimeAllowed(TERMS_TIMEOUT);

    return q;

}

From source file:org.sleuthkit.autopsy.keywordsearch.TermsComponentQuery.java

License:Open Source License

/**
 * Executes the regex query as a two step operation. In the first step, the
 * Solr terms component is used to find any terms in the index that match
 * the regex. In the second step, term queries are executed for each matched
 * term to produce the set of keyword hits for the regex.
 *
 * @return A QueryResult object or null.
 *
 * @throws NoOpenCoreException/*w w  w .j a  va  2s. c  o m*/
 */
@Override
public QueryResults performQuery() throws KeywordSearchModuleException, NoOpenCoreException {
    /*
     * Do a query using the Solr terms component to find any terms in the
     * index that match the regex.
     */
    final SolrQuery termsQuery = new SolrQuery();
    termsQuery.setRequestHandler(SEARCH_HANDLER);
    termsQuery.setTerms(true);
    termsQuery.setTermsRegexFlag(CASE_INSENSITIVE);
    termsQuery.setTermsRegex(searchTerm);
    termsQuery.addTermsField(SEARCH_FIELD);
    termsQuery.setTimeAllowed(TERMS_SEARCH_TIMEOUT);
    termsQuery.setShowDebugInfo(DEBUG_FLAG);
    termsQuery.setTermsLimit(MAX_TERMS_QUERY_RESULTS);
    List<Term> terms = KeywordSearch.getServer().queryTerms(termsQuery).getTerms(SEARCH_FIELD);
    /*
     * Do a term query for each term that matched the regex.
     */
    QueryResults results = new QueryResults(this, keywordList);
    for (Term term : terms) {
        /*
         * If searching for credit card account numbers, do a Luhn check on
         * the term and discard it if it does not pass.
         */
        if (keyword.getArtifactAttributeType() == ATTRIBUTE_TYPE.TSK_CARD_NUMBER) {
            Matcher matcher = CREDIT_CARD_NUM_PATTERN.matcher(term.getTerm());
            matcher.find();
            final String ccn = CharMatcher.anyOf(" -").removeFrom(matcher.group("ccn"));
            if (false == CREDIT_CARD_NUM_LUHN_CHECK.isValid(ccn)) {
                continue;
            }
        }

        /*
         * Do an ordinary query with the escaped term and convert the query
         * results into a single list of keyword hits without duplicates.
         *
         * Note that the filters field appears to be unused. There is an old
         * comment here, what does it mean? "Note: we can't set filter query
         * on terms query but setting filter query on fileResults query will
         * yield the same result." The filter is NOT being added to the term
         * query.
         */
        String escapedTerm = KeywordSearchUtil.escapeLuceneQuery(term.getTerm());
        LuceneQuery termQuery = new LuceneQuery(keywordList, new Keyword(escapedTerm, true));
        filters.forEach(termQuery::addFilter); // This appears to be unused
        QueryResults termQueryResult = termQuery.performQuery();
        Set<KeywordHit> termHits = new HashSet<>();
        for (Keyword word : termQueryResult.getKeywords()) {
            termHits.addAll(termQueryResult.getResults(word));
        }
        results.addResult(new Keyword(term.getTerm(), false), new ArrayList<>(termHits));
    }
    return results;
}