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

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

Introduction

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

Prototype

public SolrQuery setRows(Integer rows) 

Source Link

Usage

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);

    // Make the request
    final QueryRequest featureRequest = new QueryRequest(featureSolrQuery);
    QueryResponse featureResponse = null;
    try {/*w  w  w  . j a  va2  s .  c  o m*/
        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//ww w. jav  a2s. c  o  m
 *
 * @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);

    }//www .  j a  va  2  s.c o 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.idealista.solrmeter.model.service.impl.QueryServiceSolrJImpl.java

License:Apache License

protected SolrQuery createQuery(String q, String fq, String qt, boolean highlight, String facetFields,
        String sort, String sortOrder, Integer rows, Integer start, String otherParams) throws QueryException {
    SolrQuery query = new SolrQuery();
    if (q != null) {
        query.setQuery(q);/*from   ww  w  .ja v a 2s . c o  m*/
    }
    if (fq != null) {
        List<String> filterQueries = this.getFilterQueries(fq);
        for (String filterQuery : filterQueries) {
            query.addFilterQuery(filterQuery);
        }
    }

    query.setHighlight(highlight);
    if (facetFields == null || "".equals(facetFields)) {
        query.setFacet(false);
    } else {
        query.setFacet(true);
        List<String> facets = this.getFacets(facetFields);
        for (String facet : facets) {
            query.addFacetField(facet);
        }
    }
    if (sort != null && !"".equals(sort)) {
        query.setSort(sort, ORDER.valueOf(sortOrder));
    }
    if (rows != null && rows < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (rows != null) {
        query.setRows(rows);
    }
    if (start != null && start < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (start != null) {
        query.setStart(start);
    }

    if (otherParams != null) {
        List<String> params = this.getOtherParams(otherParams);
        for (String param : params) {
            query.add(getParamName(param), getParamValue(param));
        }
    }
    return query;
}

From source file:com.ir.qa.QueryProcessor.SpellCorrector.java

License:Apache License

public String topSuggestion(String spelling) throws SolrServerException {
    HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
    SolrQuery query = new SolrQuery();
    query.setFields("name");
    query.setRows(50);
    query.setQuery("wordNGram:" + spelling); //<co id="co.dym.field"/>
    QueryResponse response = solr.query(query);
    SolrDocumentList results = response.getResults();
    Levenshtein levenshtein = new Levenshtein();
    float maxDistance = 0;
    float distance = 0;
    String suggestion = null;/*from w w w .  j a  v  a2 s . com*/
    List<String> nameList = new ArrayList<String>();

    for (int i = 0; i < results.size(); i++) {
        // System.out.println(results.get(i));
        SolrDocument doc = results.get(i);

        nameList = (List<String>) doc.getFieldValue("name");
        for (String name_suggestions : nameList) {
            distance = levenshtein.getSimilarity(name_suggestions, spelling);
            System.out.println(name_suggestions);

            System.out.println(distance);

            if (distance > maxDistance) {
                maxDistance = distance;
                suggestion = name_suggestions;
            }
        }

        if (maxDistance > threshold) { //<co id="co.dym.threshold"/>

            return suggestion;
        }
    }

    return null;
}

From source file:com.lanacion.adminsiteln.config.SolrConnection.java

public int countDocuments(String UserQuery) throws SolrServerException {
    SolrQuery query = new SolrQuery();
    if (UserQuery.length() > 0) {
        query.setQuery(UserQuery);//from w ww .  j  a  va 2  s. com
    } else {
        query.setQuery("*:*");
    }
    //query.addFilterQuery("cat:electronics","store:amazon.com");
    query.setFields("titulo", "cuerpo");
    query.setStart(0);
    query.setRows(10000);
    query.set("defType", "edismax");

    QueryResponse response = solrCore.query(query);
    SolrDocumentList results = response.getResults();

    return results.size();
}

From source file:com.liferay.portal.search.solr.internal.SolrIndexSearcher.java

License:Open Source License

protected void addPagination(SolrQuery solrQuery, int start, int end) {
    solrQuery.setRows(end - start);
    solrQuery.setStart(start);//from   w w  w  .  j  a v  a  2  s .  com
}

From source file:com.liferay.portal.search.solr.internal.SolrIndexSearcher.java

License:Open Source License

protected QueryResponse doSearch(SearchContext searchContext, Query query, int start, int end, boolean count)
        throws Exception {

    QueryConfig queryConfig = query.getQueryConfig();

    SolrQuery solrQuery = new SolrQuery();

    if (!count) {
        addFacets(solrQuery, searchContext);
        addHighlights(solrQuery, queryConfig);
        addPagination(solrQuery, start, end);
        addSelectedFields(solrQuery, queryConfig);
        addSort(solrQuery, searchContext.getSorts());

        solrQuery.setIncludeScore(queryConfig.isScoreEnabled());
    } else {//from   w  w  w .  j av  a  2  s  .c o  m
        solrQuery.setRows(0);
    }

    String queryString = translateQuery(query, searchContext);

    solrQuery.setQuery(queryString);

    String filterQuery = _filterTranslator.translate(query.getPreBooleanFilter(), searchContext);

    solrQuery.setFilterQueries(filterQuery);

    QueryResponse queryResponse = executeSearchRequest(solrQuery);

    if (_log.isInfoEnabled()) {
        _log.info("The search engine processed " + solrQuery.getQuery() + " in "
                + queryResponse.getElapsedTime() + " ms");
    }

    return queryResponse;
}

From source file:com.liferay.portal.search.solr.internal.SolrQuerySuggester.java

License:Open Source License

@Override
public String[] suggestKeywordQueries(SearchContext searchContext, int max) throws SearchException {

    SolrQuery solrQuery = new SolrQuery();

    solrQuery.setFilterQueries(getFilterQueries(searchContext, SuggestionConstants.TYPE_QUERY_SUGGESTION));

    StringBundler sb = new StringBundler(6);

    sb.append("start");

    String keywords = searchContext.getKeywords();

    sb.append(keywords.length());//from   w  w w. jav a 2 s  . c om

    sb.append(StringPool.COLON);
    sb.append(StringPool.QUOTE);
    sb.append(keywords);
    sb.append(StringPool.QUOTE);

    solrQuery.setQuery(sb.toString());

    solrQuery.setRows(max);

    try {
        QueryResponse queryResponse = _solrServer.query(solrQuery);

        SolrDocumentList solrDocumentList = queryResponse.getResults();

        String[] querySuggestions = new String[solrDocumentList.size()];

        for (int i = 0; i < solrDocumentList.size(); i++) {
            SolrDocument solrDocument = solrDocumentList.get(i);

            querySuggestions[i] = (String) solrDocument.getFieldValue(Field.KEYWORD_SEARCH);
        }

        return querySuggestions;
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug("Unable to execute Solr query", e);
        }

        throw new SearchException(e.getMessage());
    }
}