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

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

Introduction

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

Prototype

public SolrQuery setParam(String name, boolean value) 

Source Link

Usage

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

License:Open Source License

private static void addQueryTextAndOperatorWithoutSynonyms(SimpleSearch simpleSearch, SolrQuery query,
        boolean useDismax) {

    String collectionName = simpleSearch.getCollectionName();
    if (StringUtils.isNotEmpty(collectionName)) {
        if (simpleSearch.getAdvancedSearchRule() == null) {
            String textQuery = getTextQuery(simpleSearch);
            if (StringUtils.isNotEmpty(textQuery)) {
                String searchType = simpleSearch.getSearchType();
                if (SimpleSearch.SEARCH_ALL.equals(textQuery)) {
                    query.setQuery(textQuery);
                    // FIXME : AND ou Operateur par defaut?
                    query.setParam("q.op", "AND");
                    if (useDismax) {
                        // Non valide avec disMax => disMax doit etre desactivee
                        query.setRequestHandler(SolrServices.DEFAULT_DISTANCE_NAME);
                        LOGGER.warning(//  w w w .j a v  a  2 s.  co  m
                                "Dismax is replaced by the default distance since the former does not allow wildcard");
                    }
                } else if (SimpleSearch.AT_LEAST_ONE_WORD.equals(searchType)) {
                    query.setQuery(textQuery);
                    // Operateur OR
                    query.setParam("q.op", "OR");
                    if (useDismax) {
                        query.setParam("mm", "0");
                    }
                } else if (SimpleSearch.EXACT_EXPRESSION.equals(searchType)) {
                    query.setQuery("\"" + textQuery + "\"");
                    if (useDismax) {
                        // FIXME il faut faire quoi avec dismax?
                    }
                } else {
                    if (SimpleSearch.ALL_WORDS.equals(searchType)) {
                        query.setQuery(textQuery);
                        // Operateur AND
                        query.setParam("q.op", "AND");
                        if (useDismax) {
                            query.setParam("mm", "100");
                        }
                    } else {
                        throw new RuntimeException("Invalid searchType " + searchType);
                    }
                }
            }
        } else {
            query.setQuery(simpleSearch.getLuceneQuery());
        }
    }
}

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);/*from   w w w.  j  a va  2s . com*/
    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.frank.search.solr.core.DefaultQueryParser.java

License:Apache License

/**
 * Convert given Query into a SolrQuery executable via
 * {@link org.apache.solr.client.solrj.SolrClient}
 *
 * @param query/*from  w w  w.j  ava 2s. co m*/
 * @return
 */
@Override
public final SolrQuery doConstructSolrQuery(SolrDataQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");
    Assert.notNull(query.getCriteria(), "Query has to have a criteria.");

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.Q, getQueryString(query));
    if (query instanceof Query) {
        processQueryOptions(solrQuery, (Query) query);
    }
    if (query instanceof FacetQuery) {
        processFacetOptions(solrQuery, (FacetQuery) query);
    }
    if (query instanceof HighlightQuery) {
        processHighlightOptions(solrQuery, (HighlightQuery) query);
    }
    return solrQuery;
}

From source file:com.frank.search.solr.core.QueryParserBase.java

License:Apache License

/**
 * Append field list to {@link org.apache.solr.client.solrj.SolrQuery}
 *
 * @param solrQuery//w ww  .  j a  v a2 s.co m
 * @param fields
 */
protected void appendProjectionOnFields(SolrQuery solrQuery, List<Field> fields) {
    if (CollectionUtils.isEmpty(fields)) {
        return;
    }
    List<String> solrReadableFields = new ArrayList<String>();
    for (Field field : fields) {
        if (field instanceof CalculatedField) {
            solrReadableFields.add(createCalculatedFieldFragment((CalculatedField) field));
        } else {
            solrReadableFields.add(field.getName());
        }
    }
    solrQuery.setParam(CommonParams.FL, StringUtils.join(solrReadableFields, ","));
}

From source file:com.frank.search.solr.core.TermsQueryParser.java

License:Apache License

@Override
public SolrQuery doConstructSolrQuery(TermsQuery query) {
    Assert.notNull(query, "Cannot construct solrQuery from null value.");

    SolrQuery solrQuery = new SolrQuery();
    String queryString = getQueryString(query);
    if (StringUtils.hasText(queryString)) {
        solrQuery.setParam(CommonParams.Q, queryString);
    }// w  ww. j  av  a  2s.c om
    appendTermsOptionsToSolrQuery(query.getTermsOptions(), solrQuery);
    processTermsFields(solrQuery, query);
    appendRequestHandler(solrQuery, query.getRequestHandler());
    return solrQuery;
}

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);//from w w  w  .  j ava2s.  co  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// w  w w  .j  a va2  s .co  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);

    }/*from  w w w.ja va2s  . 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.ifactory.press.db.solr.processor.FieldMergingProcessorTest.java

License:Apache License

private List<TermsResponse.Term> getTerms(String field) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setParam(CommonParams.QT, "/terms");
    solrQuery.setParam(TermsParams.TERMS, true);
    solrQuery.setParam(TermsParams.TERMS_LIMIT, "100");
    solrQuery.setParam(TermsParams.TERMS_FIELD, field);
    QueryResponse resp = solr.query(solrQuery);
    return resp.getTermsResponse().getTermMap().get(field);
}

From source file:com.mycompany.solr_web_application.Simple_Query_Solr.java

public static void main(String[] args) {
    try {/*from   w ww.  j a  va  2  s . co m*/
        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("india");
        query.setQuery("india");
        query.setFields(new String[] { "id", "name", "contents" });
        query.setHighlight(true); //set other params as needed
        query.setParam("hl.fl", "name,contents");
        query.setParam("hl.simple.pre", "<em>");
        query.setParam("hl.simple.post", "</em>");
        query.setParam("hl.fragsize", "100");

        QueryResponse queryResponse = server.query(query);
        //System.out.println(queryResponse);
        //response = server.query(solrQuery);
        Iterator<SolrDocument> iter = queryResponse.getResults().iterator();

        while (iter.hasNext()) {
            SolrDocument resultDoc = iter.next();
            String id = (String) resultDoc.getFieldValue("id");
            //System.out.println(id);
            //String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field

            if (queryResponse.getHighlighting().get(id) != null) {
                List<String> highlightSnippets = queryResponse.getHighlighting().get(id).get("name");
                List<String> highlightSnippets1 = queryResponse.getHighlighting().get(id).get("contents");
                // System.out.println("name "+highlightSnippets);
                //  System.out.println("content "+highlightSnippets1);
            }
        }
        //String jsonString = JSONUtil.toJSON(queryResponse);
        //ResponseWrapper data = new Gson().fromJson(jsonString, ResponseWrapper.class);
        System.out.println("Highlighting data " + queryResponse.getHighlighting());

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