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

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

Introduction

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

Prototype

public SolrQuery setRequestHandler(String qt) 

Source Link

Document

The Request Handler to use (see the solrconfig.xml), which is stored in the "qt" parameter.

Usage

From source file:com.doculibre.constellio.services.StatusServicesImpl.java

License:Open Source License

@Override
public List<Record> listLastIndexedRecords(RecordCollection collection, int maxSize) {
    List<Record> lastIndexedRecords;
    RecordServices recordServices = ConstellioSpringUtils.getRecordServices();

    String luceneQuery = SimpleSearch.SEARCH_ALL;
    SolrQuery solrQuery = new SolrQuery(luceneQuery);
    solrQuery.setRequestHandler("standard");
    solrQuery.setRows(maxSize);// ww  w . jav  a 2  s.c  o  m
    solrQuery.setSort(IndexField.LAST_INDEXED_FIELD, ORDER.desc);

    String collectionName = collection.getName();
    solrQuery.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, luceneQuery);
    solrQuery.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, collectionName);

    SolrServer server = SolrCoreContext.getSolrServer(collectionName);
    if (server != null && collection.getIndexField(IndexField.LAST_INDEXED_FIELD) != null) {
        try {
            QueryResponse queryResponse = server.query(solrQuery);
            SolrDocumentList results = queryResponse.getResults();
            List<Number> recordIds = new ArrayList<Number>();
            for (SolrDocument result : results) {
                Long recordId = new Long(result.getFieldValue(IndexField.RECORD_ID_FIELD).toString());
                recordIds.add(recordId);
            }
            if (!recordIds.isEmpty()) {
                lastIndexedRecords = recordServices.list(recordIds, collection);
            } else {
                lastIndexedRecords = new ArrayList<Record>();
            }
        } catch (SolrServerException e) {
            throw new RuntimeException(e);
        }
    } else if (!collection.isSynchronizationRequired()) {
        String msg = "No SolrServer available for collection id " + collection.getId();
        LOGGER.log(Level.SEVERE, msg);
        lastIndexedRecords = new ArrayList<Record>();
    } else if (collection.getIndexField(IndexField.LAST_INDEXED_FIELD) == null) {
        String msg = "No " + IndexField.LAST_INDEXED_FIELD + " index field for collection id "
                + collection.getId();
        LOGGER.log(Level.SEVERE, msg);
        lastIndexedRecords = new ArrayList<Record>();
    } else {
        lastIndexedRecords = new ArrayList<Record>();
    }
    return lastIndexedRecords;
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Do the query using a SolrQuery//from w w  w  .j  av  a  2 s .  c om
 */
public static QueryResponse spellCheck(SolrServer server, String badQuery) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(badQuery);
    solrQuery.set("collectionName", myCollection);

    // qt=spellcheck || qt=spellchecker
    solrQuery.setRequestHandler("spellcheck");
    return server.query(solrQuery);
}

From source file:com.gdn.x.ui.controller.CommonController.java

protected List searchByQuery(String queryParameter) throws MalformedURLException, SolrServerException {
    List actualResult = new ArrayList<String>();
    try {/*  w ww .jav a  2  s .  co m*/
        //            for solr 5
        HttpSolrClient solr = new HttpSolrClient(solrUrlSearch);
        //for Solr 4
        //            HttpSolrServer solr = new HttpSolrServer("");

        SolrQuery query = new SolrQuery();

        query.setQuery(queryParameter);
        query.setFields("id");
        query.setRequestHandler("/browse");
        query.setStart(0);
        query.setRows(40);
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        int j = 0;
        for (int i = 0; i < results.size(); ++i, j++) {
            actualResult.add(results.get(i).get("id"));
        }
    } catch (IOException ex) {
        Logger.getLogger(RunGoldenListController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return actualResult;
}

From source file:com.gdn.x.ui.controller.Evaluation.CommonControllerEvaluation.java

protected List searchByQueryEvaluation(String queryParameter)
        throws MalformedURLException, SolrServerException {
    List actualResult = new ArrayList<String>();
    try {//w w  w .ja  va2  s.com
        //            for solr 5
        HttpSolrClient solr = new HttpSolrClient(solrUrlSearch);
        //for Solr 4
        //            HttpSolrServer solr = new HttpSolrServer("http://172.17.132.9:8983/solr/collection3");

        SolrQuery query = new SolrQuery();

        query.setQuery(queryParameter);
        query.setFields("id");
        query.setRequestHandler("/browse");
        query.setStart(0);
        query.setRows(24);
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        int j = 0;
        for (int i = 0; i < results.size(); ++i, j++) {
            actualResult.add(results.get(i).get("id"));
        }
        //                System.out.println(results.getNumFound());
        //            System.out.println(actualResult);

    } catch (IOException ex) {
        Logger.getLogger(RunGoldenListController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return actualResult;
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.RankerCreationUtil.java

License:Open Source License

/**
 * Retrieve a {@link CandidateAnswer} with its {@code threadPostId} by querying the /select
 * endpoint with query THREAD_POST_ID:x/* www. j a v  a  2  s.c om*/
 * 
 * @param searcher An initialized {@link RetrieveAndRankSearcher}
 * @param threadPostId The THREAD_POST_ID of the answer thread
 * @return
 * @throws IOException
 * @throws IngestionException
 */
public static CandidateAnswer getCandidateAnswerById(RetrieveAndRankSearcher searcher, String threadPostId)
        throws IOException, IngestionException {

    CandidateAnswer answer = null;
    try {
        SolrQuery featureSolrQuery = new SolrQuery(
                RetrieveAndRankSearcherConstants.ID_FIELD + ":" + threadPostId);

        // specify the request handler for the feature query
        featureSolrQuery.setRequestHandler(RetrieveAndRankSearcherConstants.SELECT_REQUEST_HANDLER);

        // We expect only one response since THREAD_POST_ID is a unique key
        if (featureSolrQuery.size() != 1) {
            throw new IngestionException(threadPostId);
        }
        featureSolrQuery.setRows(1);
        final QueryRequest featureRequest = new QueryRequest(featureSolrQuery);

        QueryResponse featureResponse = null;

        featureResponse = searcher.processSolrRequest(featureRequest);
        for (SolrDocument doc : featureResponse.getResults()) {
            byte[] bin = (byte[]) doc.getFieldValue(IndexDocumentFieldName.SERIALIZED_THREAD.toString());
            answer = StackExchangeThreadSerializer.deserializeThreadFromBinArr(bin);
        }
    } catch (IOException | SolrServerException | InterruptedException e) {
        logger.error(e.getMessage());
    }
    return answer;
}

From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.RetrieveAndRankSearcher.java

License:Open Source License

public Collection<CandidateAnswer> performSearch(String query, int numAns) throws SearchException {

    SolrQuery featureSolrQuery = new SolrQuery(query);

    // Specify the request handler for the feature query
    featureSolrQuery.setRequestHandler(request_handler);
    // Specify parameters for the response
    featureSolrQuery.setParam(RetrieveAndRankSearcherConstants.FIELD_LIST_PARAM,
            RetrieveAndRankSearcherConstants.ID_FIELD + ","
                    + RetrieveAndRankSearcherConstants.FEATURE_VECTOR_FIELD + ","
                    + IndexDocumentFieldName.SERIALIZED_THREAD.toString());

    featureSolrQuery.setRows(numAns);//  www .java 2 s. c o m

    // Make the request
    final QueryRequest featureRequest = new QueryRequest(featureSolrQuery);
    QueryResponse featureResponse = null;
    try {
        featureResponse = processSolrRequest(featureRequest);
    } catch (IOException | SolrServerException | InterruptedException e) {
        log.error(e.toString(), e);
        throw new SearchException(e);
    }
    return responseToCollection(featureResponse);
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.SolrUtils.java

License:Open Source License

/**
 * Create and process a Solr query/*from ww  w . j a  v  a2  s  . com*/
 *
 * @param query the query
 * @param featureVector the feature vector
 * @return the query response
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws SolrServerException the Solr server exception
 * @throws InterruptedException the interrupted exception
 */
private QueryResponse solrRuntimeQuery(String query, boolean featureVector)
        throws IOException, SolrServerException, InterruptedException {
    SolrQuery featureSolrQuery = new SolrQuery(query);
    if (featureVector) {
        featureSolrQuery.setRequestHandler(FCSELECT_REQUEST_HANDLER);
        // add the ranker id - this tells the plugin to re-reank the results in a single pass
        featureSolrQuery.setParam(RANKER_ID, rankerId);

    }

    // bring back the id, score, and featureVector for the feature query
    featureSolrQuery.setParam(FIELD_LIST_PARAM, ID_FIELD, SCORE_FIELD, FEATURE_VECTOR_FIELD);

    // need to ask for enough rows to ensure the correct answer is included in the resultset
    featureSolrQuery.setRows(1000);
    QueryRequest featureRequest = new QueryRequest(featureSolrQuery, METHOD.POST);

    return processSolrRequest(featureRequest);
}

From source file:com.ibm.watson.retrieveandrank.app.rest.RetrieveAndRankProxyResource.java

License:Open Source License

private static QueryResponse solrRuntimeQuery(String query, boolean featureVector)
        throws IOException, SolrServerException, InterruptedException {
    // boolean headersPrinted = false;
    final SolrQuery featureSolrQuery = new SolrQuery(query);
    // specify the fcselect request handler for the feature query

    if (featureVector) {
        featureSolrQuery.setRequestHandler(FCSELECT_REQUEST_HANDLER);
        // add the ranker id - this tells the plugin to re-reank the results in a single pass
        featureSolrQuery.setParam("ranker_id", ranker_id);

    }/*from w ww  .  j  a  v  a 2s . co  m*/

    // bring back the id, score, and featureVector for the feature query
    featureSolrQuery.setParam(FIELD_LIST_PARAM, ID_FIELD, SCORE_FIELD, FEATURE_VECTOR_FIELD);

    // need to ask for enough rows to ensure the correct answer is included in the resultset
    featureSolrQuery.setRows(1000);
    final QueryRequest featureRequest = new QueryRequest(featureSolrQuery, METHOD.POST);

    // this leverages the plugin
    final QueryResponse featureResponse = processSolrRequest(featureRequest);

    return featureResponse;
}

From source file:com.mmj.app.lucene.solr.utils.BaseSolrQueryConvert.java

License:Open Source License

protected static SolrQuery createSearchQuery(List<String> params, List<String> fiters, SearchQuery searchQuery,
        String... searchHandler) {
    SolrQuery solrQuery = new SolrQuery();
    String query = null;/* w w w.  j  a va2 s  . c  o  m*/
    if (Argument.isEmpty(params)) {
        query = ("*:*");
    } else {
        query = StringUtils.join(params, " AND ");
        if (!StringUtils.join(params.toArray()).contains("*")) {
            if (Argument.isNotEmptyArray(searchHandler)) {
                solrQuery.setRequestHandler(
                        StringUtils.isEmpty(searchHandler[0]) ? "/select" : searchHandler[0]);
            }

        }
    }
    if (Argument.isNotEmpty(fiters)) {
        solrQuery.setFilterQueries(StringUtils.join(fiters, " 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.solr_web_application.Wildcard.java

public static void main(String[] args) {
    try {// w w w .  j  av a2s. com
        String url = null;
        HttpSolrServer server = null;
        url = "http://localhost:8983/solr/wiki";
        server = new HttpSolrServer(url);
        server.setMaxRetries(1);
        server.setConnectionTimeout(20000);
        server.setParser(new XMLResponseParser());
        server.setSoTimeout(10000);
        server.setDefaultMaxConnectionsPerHost(100);
        server.setMaxTotalConnections(100);
        server.setFollowRedirects(false);
        server.setAllowCompression(true);

        SolrQuery query = new SolrQuery();
        query.setQuery("indi*");
        query.setRequestHandler("/wildcard");
        query.setFields(new String[] { "id", "name", "contents" });
        query.setHighlight(true); //set other params as needed

        QueryResponse queryResponse = server.query(query);
        System.out.println(queryResponse);
        //response = server.query(solrQuery);

    } catch (Exception e) {
        e.printStackTrace();
    }
}