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

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

Introduction

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

Prototype

public SolrQuery() 

Source Link

Usage

From source file:com.temenos.interaction.commands.solr.SelectCommand.java

License:Open Source License

public Result execute(InteractionContext ctx) {

    MultivaluedMap<String, String> queryParams = ctx.getQueryParameters();
    String entityName = ctx.getCurrentState().getEntityName();

    try {//from   w ww .j ava  2 s .  c  om
        String queryStr = queryParams.getFirst("q");
        SolrQuery query = new SolrQuery();
        query.setQuery(queryStr);

        QueryResponse rsp = solrServer.query(query);
        ctx.setResource(buildCollectionResource(entityName, rsp.getResults()));
        return Result.SUCCESS;
    } catch (SolrServerException e) {
        logger.error("An unexpected error occurred while querying Solr", e);
    }

    return Result.FAILURE;
}

From source file:com.temenos.interaction.commands.solr.SolrSearchCommand.java

License:Open Source License

SolrQuery buildQuery(MultivaluedMap<String, String> queryParams) {
    SolrQuery query = new SolrQuery();

    // Add Number of rows to fetch
    addNumOfRows(query, queryParams);//from   w  w w.  ja  v  a 2 s. c  om

    // Add Shards for Distributed Query support
    addShards(query, queryParams);

    // Build the query string.
    String queryString = buildQueryString(queryParams);
    if (null != queryString) {
        query.setQuery(queryString);
    }

    // Add the filter string (like query but does hard matching).
    addFilter(query, queryParams);

    // If returned fields have been limited by authorization set them
    addSelect(query, queryParams);

    return (query);
}

From source file:com.temenos.interaction.commands.solr.TermsCommand.java

License:Open Source License

public Result execute(InteractionContext ctx) {

    MultivaluedMap<String, String> queryParams = ctx.getQueryParameters();
    String entityName = ctx.getCurrentState().getEntityName();

    try {//  w w  w  .  j a va2 s. com
        String queryStr = queryParams.getFirst("q");
        SolrQuery query = new SolrQuery();
        query.setRequestHandler("/terms");
        //         query.setFields("id", "name", "mnemonic", "address", "postcode");
        query.setQuery(queryStr);
        // TODO make these configurable
        query.addTermsField("name");
        query.addTermsField("mnemonic");
        query.setTermsPrefix(queryStr);
        query.setTermsSortString("count");
        query.setTerms(true);
        query.setTermsLimit(10);

        QueryResponse rsp = solrServer.query(query);
        ctx.setResource(buildCollectionResource(entityName, "name", rsp.getTermsResponse().getTermMap()));
        return Result.SUCCESS;
    } catch (SolrServerException e) {
        logger.error("An unexpected error occurred while querying Solr", e);
    }

    return Result.FAILURE;
}

From source file:com.thinkbiganalytics.search.SolrSearchService.java

License:Apache License

private QueryResponse executeSearch(String query, int size, int start) {
    final String COLLECTION_LIST = "kylo-data," + SearchIndex.DATASOURCES; //Solr admin to configure beforehand
    final String ALL_FIELDS = "*";
    final String BOLD_HIGHLIGHT_START = "<font style='font-weight:bold'>";
    final String BOLD_HIGHLIGHT_END = "</font>";

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("q", query);
    solrQuery.setRows(size);/*ww w  .  j a  v  a  2 s. c  o  m*/
    solrQuery.setStart(start);
    solrQuery.setParam("collection", COLLECTION_LIST);
    solrQuery.setHighlight(true);
    solrQuery.set("hl.fl", ALL_FIELDS);
    solrQuery.setHighlightSimplePre(BOLD_HIGHLIGHT_START);
    solrQuery.setHighlightSimplePost(BOLD_HIGHLIGHT_END);
    solrQuery.setHighlightRequireFieldMatch(false);

    try {
        return client.query("kylo-data", solrQuery);
    } catch (SolrServerException | IOException e) {
        throw new RuntimeException("Error encountered during search.");
    }
}

From source file:com.ushahidi.swiftriver.core.solr.repository.DropDocumentRepositoryImpl.java

License:Open Source License

private SolrQuery getPreparedSolrQuery(String searchTerm, Pageable pageable) {
    // Calculate the start row
    Integer start = pageable.getPageNumber() * pageable.getPageSize();

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("q", searchTerm);
    solrQuery.set("defType", "edismax");
    solrQuery.set("stopwords", true);
    solrQuery.set("lowercaseOperators", true);
    solrQuery.setStart(start);// w ww.  j a v  a  2 s  .  c o  m
    solrQuery.setRows(pageable.getPageSize());

    return solrQuery;
}

From source file:com.welab.x.blacklist.util.SearchEngine.java

License:Open Source License

/**
 * Query result from server.// w w w  .  ja v a 2 s .  co m
 * 
 * @param statement
 * @return SolrDocumentList
 * @throws SolrServerException
 * @throws Exception
 */
public static SolrDocumentList search(String statement) throws SolrServerException, Exception {
    // get current environment information
    String environment = Utility.getCurrentEnvironment();
    // get the connection url for current environment
    String url = Utility.getUrlByEnvironment(environment);

    SolrQuery query = new SolrQuery();
    query.setQuery(statement);
    try {
        QueryResponse rsp = Connection.getConnection(url).query(query);
        return rsp.getResults();
    } catch (SolrServerException se) {
        LOG.error(ErrorMessage.SEARCH_SOLR_ERROR, se);
        throw se;
    }
}

From source file:com.yahoo.ycsb.db.solr.SolrClient.java

License:Open Source License

/**
 * Read a record from the database. Each field/value pair from the result will be stored in a
 * HashMap./* ww w . j  a v  a  2  s .  c  om*/
 *
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to read.
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A HashMap of field/value pairs for the result
 * @return Zero on success, a non-zero error code on error or "not found".
 */
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    try {
        Boolean returnFields = false;
        String[] fieldList = null;
        if (fields != null) {
            returnFields = true;
            fieldList = fields.toArray(new String[fields.size()]);
        }
        SolrQuery query = new SolrQuery();
        query.setQuery("id:" + key);
        if (returnFields) {
            query.setFields(fieldList);
        }
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();
        if ((results != null) && (results.getNumFound() > 0)) {
            for (String field : results.get(0).getFieldNames()) {
                result.put(field, new StringByteIterator(String.valueOf(results.get(0).getFirstValue(field))));
            }
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}

From source file:com.yahoo.ycsb.db.solr.SolrClient.java

License:Open Source License

/**
 * Perform a range scan for a set of records in the database. Each field/value pair from the
 * result will be stored in a HashMap./*w w w.  ja  va2s  . c o  m*/
 *
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
 * @return Zero on success, a non-zero error code on error. See this class's description for a
 *         discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    try {
        Boolean returnFields = false;
        String[] fieldList = null;
        if (fields != null) {
            returnFields = true;
            fieldList = fields.toArray(new String[fields.size()]);
        }
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setParam("fq", "id:[ " + startkey + " TO * ]");
        if (returnFields) {
            query.setFields(fieldList);
        }
        query.setRows(recordcount);
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();

        HashMap<String, ByteIterator> entry;

        for (SolrDocument hit : results) {
            entry = new HashMap<String, ByteIterator>((int) results.getNumFound());
            for (String field : hit.getFieldNames()) {
                entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
            }
            result.add(entry);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}

From source file:com.yahoo.ycsb.db.solr6.SolrClient.java

License:Open Source License

/**
 * Perform a range scan for a set of records in the database. Each field/value pair from the
 * result will be stored in a HashMap.//w  ww.j a  v a2 s. c  om
 *
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
 * @return Zero on success, a non-zero error code on error. See this class's description for a
 *         discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    try {
        Boolean returnFields = false;
        String[] fieldList = null;
        if (fields != null) {
            returnFields = true;
            fieldList = fields.toArray(new String[fields.size()]);
        }
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setParam("fq", "id:[ " + startkey + " TO * ]");
        if (returnFields) {
            query.setFields(fieldList);
        }
        query.setRows(recordcount);
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();

        HashMap<String, ByteIterator> entry;

        for (SolrDocument hit : results) {
            entry = new HashMap<>((int) results.getNumFound());
            for (String field : hit.getFieldNames()) {
                entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
            }
            result.add(entry);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}

From source file:com.yaotrue.learn.solr.SolrjTest.java

License:Apache License

@Test
public void testSuggest() throws SolrServerException, IOException {
    SolrQuery params = new SolrQuery();
    params.set("qt", "/suggest");
    params.set("suggest", true);
    params.set("suggest.dictionary", "mySuggester");
    params.set("suggest.q", "?");
    QueryResponse query = solrClient.query(params);
    NamedList<Object> response = query.getResponse();
    //      response.get("suggest")
    SpellCheckResponse spellCheckResponse = query.getSpellCheckResponse();
    List<Suggestion> suggestions = spellCheckResponse.getSuggestions();
    for (Suggestion suggestion : suggestions) {//JAVA
        List<String> suggestions2 = suggestion.getAlternatives();
        for (String string : suggestions2) {
            System.out.println(string);
        }//from   www.ja  va  2 s. co m
    }
    SolrDocumentList results = query.getResults();
    for (int i = 0; i < results.getNumFound(); i++) {
        System.out.println(results.get(i).get("suggestion"));
    }
}