Example usage for org.apache.solr.client.solrj.impl HttpSolrClient query

List of usage examples for org.apache.solr.client.solrj.impl HttpSolrClient query

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl HttpSolrClient query.

Prototype

public QueryResponse query(String collection, SolrParams params) throws SolrServerException, IOException 

Source Link

Document

Performs a query to the Solr server

Usage

From source file:com.ibm.watson.apis.conversation_enhanced.retrieve_and_rank.Query.java

License:Open Source License

/**
 * Use the Watson Developer Cloud SDK to send the user's query to the retrive and rank service
 * /* w  ww  .  j  a v  a2 s. co  m*/
 * @param userQuery The user's query to be sent to the retrieve and rank service
 * @return The unaltered SOLR query responses obtained from the retrieve and rank service
 * @throws SolrServerException
 * @throws IOException
 */
public QueryResponse query(String userQuery) throws Exception {

    // Configure the Watson Developer Cloud SDK to make a call to the appropriate retrieve and rank
    // service. Specific information is obtained from environment variable and the services
    // associated with the app. See the Query constructor for details.
    RetrieveAndRank service = new RetrieveAndRank();
    HttpSolrClient solrClient = HttpSolrClientUtils.getSolrClient(service.getSolrUrl(CLUSTER_ID), USERNAME,
            PASSWORD);

    logger.info(Messages.getString("Query.PASS_CLUSTER_DETAILS")); //$NON-NLS-1$

    // Setup the query parameters
    final SolrQuery query = new SolrQuery(userQuery)
            // The fields we want in the response object
            .setFields(Constants.SCHEMA_FIELD_ID, Constants.SCHEMA_FIELD_BODY, Constants.SCHEMA_FIELD_TITLE,
                    Constants.SCHEMA_FIELD_CONFIDENCE, Constants.SCHEMA_FIELD_SOURCE_URL)
            // The size of the SOLR snippet that we show as our initial answers
            .setHighlight(true).setHighlightFragsize(150).setHighlightSnippets(1)
            // The field to perform highlighting on
            .setParam("hl.fl", Constants.SCHEMA_FIELD_BODY)
            // The number of answers to return
            .setRows(Constants.RESULTS_TO_FETCH) // $NON-NLS-1$
            // The retrieve and rank endpoint to hit
            .setRequestHandler("/fcselect")
            // The ranker to rank the potential answers
            .setParam("ranker_id", RANKER_ID); //$NON-NLS-1$ //$NON-NLS-2$

    // Send the query to the retrieve and rank service to obtain answers to the user's query
    logger.info(Messages.getString("Query.QUERY_SOLR_RANKER")); //$NON-NLS-1$
    return solrClient.query(COLLECTION_NAME, query);
}

From source file:org.apache.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, String solrIndexName) {
    // create the data model
    Map<String, Object> result = new HashMap<String, Object>();
    HttpSolrClient client = null;
    QueryResponse returnMap = new QueryResponse();
    try {/* ww  w .  j a  v a  2s . co  m*/
        // do the basic query
        client = getHttpSolrClient(solrIndexName);
        // 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 = client.query(solrQuery, METHOD.POST);
        result.put("rows", returnMap);
        result.put("numFound", returnMap.getResults().getNumFound());
    } catch (Exception e) {
        Debug.logError(e.getMessage(), module);
    }
    return result;
}