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(String q) 

Source Link

Document

Create a new SolrQuery

Usage

From source file:de.qaware.javaone.solrspark.SolrSpark.java

License:Apache License

/**
 * This method queries SOLR and processes all found documents. For each
 * document the length of the field 'logRawEntry' is returned (The count of
 * unicode chars).//from w  w w.j av  a  2s .c o m
 *
 * @param query a query string.
 * @param solrRDD the solrRDD Spark abstraction.
 * @param jsc the Spark context.
 * @return the sum of the stringlength of all log messages.
 * @throws SolrServerException When the Solr processing gets into trouble.
 */
private static int queryAndProcess(String query, SolrRDD solrRDD, SparkContext jsc) throws SolrServerException {
    SolrQuery solrQuery = new SolrQuery(query);
    solrQuery.addSort("id", SolrQuery.ORDER.asc);

    // 1. Smart filtering by searching
    JavaRDD<SolrDocument> result = solrRDD.query(jsc, solrQuery);

    // 2. Parallel processing of the search results
    // parallel map in the cluster - calculate the sum of bytes from all log messages
    int totalLength = 0;
    if (!result.isEmpty()) {
        totalLength = result.map((SolrDocument d) -> {
            if (d == null) {
                return 0;
            }
            String logEntry = (String) d.getFieldValue("logRawEntry");
            return logEntry != null ? logEntry.length() : 0;
        }).reduce((Integer i, Integer j) -> {
            return i + j;
        });
    }
    return totalLength;
}

From source file:dk.statsbiblioteket.doms.licensemodule.integrationtest.DomsSolrJClientTest.java

public static ArrayList<String> filterIds(ArrayList<String> ids, String queryPartAccess) throws Exception {
    solrServer.setRequestWriter(new BinaryRequestWriter()); //To avoid http error code 413/414, due to monster URI. (and it is faster)        
    String queryPartStr = AbstractSolrJClient.makeAuthIdPart(ids);

    System.out.println(queryPartStr);
    SolrQuery query = new SolrQuery(queryPartStr);
    query.setFilterQueries(queryPartAccess);
    query.setFields("recordID");
    query.set("facet", "false");
    query.setRows(ids.size());//w ww. ja v a 2  s .  c  om
    QueryResponse response = solrServer.query(query);
    ArrayList<String> filteredIds = AbstractSolrJClient.getIdsFromResponse(response);
    return filteredIds;
}

From source file:dk.statsbiblioteket.doms.licensemodule.solr.AbstractSolrJClient.java

public ArrayList<String> filterIds(ArrayList<String> ids, String queryPartAccess) throws Exception {

    if (ids == null || ids.size() == 0) {
        return new ArrayList<String>();
    }//  ww  w  . j a v  a  2  s .  c o m

    String queryStr = makeAuthIdPart(ids);
    log.debug("query:" + queryStr);

    SolrQuery query = new SolrQuery(queryStr);
    query.setFilterQueries(queryPartAccess);
    log.debug("filter:" + queryPartAccess);

    query.setFields(filterField); //only this field is used from resultset
    query.setRows(Math.min(10000, ids.size() * 200)); // Powerrating... each page can have max 200 segments (rare).. with 20 pages query this is 4000..               
    query.set("facet", "false"); //  Must be parameter set, because this java method does NOT work: query.setFacet(false);          
    QueryResponse response = solrServer.query(query);
    ArrayList<String> filteredIds = getIdsFromResponse(response);

    return filteredIds;

}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.java

License:Open Source License

public static long getIndividualCount(List<String> vclassUris, IndividualDao indDao, ServletContext context) {
    SolrQuery query = new SolrQuery(makeMultiClassQuery(vclassUris));
    query.setRows(0);/*from   w  w w .jav a 2 s.co  m*/
    try {
        SolrServer solr = SolrSetup.getSolrServer(context);
        QueryResponse response = null;
        response = solr.query(query);
        return response.getResults().getNumFound();
    } catch (Exception ex) {
        log.error("An error occured in retrieving individual count", ex);
    }
    return 0;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.java

License:Open Source License

/**
 * builds a query with a type clause for each type in vclassUris, NAME_LOWERCASE filetred by
 * alpha, and just the hits for the page for pageSize.
 *///  w ww . jav a 2  s  .c om
private static SolrQuery getQuery(List<String> vclassUris, String alpha, int page, int pageSize) {
    String queryText = "";

    try {
        queryText = makeMultiClassQuery(vclassUris);

        // Add alpha filter if applicable
        if (alpha != null && !"".equals(alpha) && alpha.length() == 1) {
            queryText += VitroSearchTermNames.NAME_LOWERCASE + ":" + alpha.toLowerCase() + "*";
        }

        SolrQuery query = new SolrQuery(queryText);

        //page count starts at 1, row count starts at 0
        int startRow = (page - 1) * pageSize;
        query.setStart(startRow).setRows(pageSize);

        // Need a single-valued field for sorting
        query.setSortField(VitroSearchTermNames.NAME_LOWERCASE_SINGLE_VALUED, SolrQuery.ORDER.asc);

        log.debug("Query is " + query.toString());
        return query;

    } catch (Exception ex) {
        log.error("Could not make Solr query", ex);
        return new SolrQuery();
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.grefine.JSONReconcileServlet.java

License:Open Source License

protected SolrQuery getQuery(String queryStr, String searchType, int limit,
        ArrayList<String[]> propertiesList) {

    if (queryStr == null) {
        log.error("There was no parameter '" + PARAM_QUERY + "' in the request.");
        return null;
    } else if (queryStr.length() > MAX_QUERY_LENGTH) {
        log.debug("The search was too long. The maximum " + "query length is " + MAX_QUERY_LENGTH);
        return null;
    }/*from  ww  w . j a  v a2s . c  o m*/

    /// original
    ///SolrQuery query = new SolrQuery();

    /// test
    SolrQuery query = new SolrQuery(queryStr.toLowerCase());

    // original code:
    // query.setStart(0).setRows(DEFAULT_MAX_HIT_COUNT);  
    // Google Refine specific:
    query.setStart(0).setRows(limit);

    // TODO: works better without using tokenizeNameQuery(), need to investigate a bit more
    /// comment out original: query.setQuery(queryStr);

    // Filter by type
    // e.g. http://xmlns.com/foaf/0.1/Person
    if (searchType != null) {
        query.addFilterQuery(VitroSearchTermNames.RDFTYPE + ":\"" + searchType + "\"");
    }

    // Added score to original code:
    query.setFields(VitroSearchTermNames.NAME_RAW, VitroSearchTermNames.URI, "*", "score"); // fields to retrieve

    // if propertiesList has elements, add extra queries to query
    Iterator<String[]> it = propertiesList.iterator();
    while (it.hasNext()) {
        String[] pvPair = it.next();
        query.addFilterQuery(tokenizeNameQuery(pvPair[1]),
                VitroSearchTermNames.RDFTYPE + ":\"" + pvPair[0] + "\"");
    }

    // Can't sort on multivalued field, so we sort the results in Java when we get them.
    // query.setSortField(VitroSearchTermNames.NAME_LOWERCASE, SolrQuery.ORDER.asc);

    return query;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.IndividualListRdfController.java

License:Open Source License

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

    // Make the query
    String vclassUri = req.getParameter("vclass");
    String queryStr = VitroSearchTermNames.RDFTYPE + ":\"" + vclassUri + "\"";
    SolrQuery query = new SolrQuery(queryStr);
    query.setStart(0).setRows(ENTITY_LIST_CONTROLLER_MAX_RESULTS).setFields(VitroSearchTermNames.URI);
    // For now, we're only displaying the url, so no need to sort.
    //.setSortField(VitroSearchTermNames.NAME_LOWERCASE_SINGLE_VALUED);

    // Execute the query
    SolrServer solr = SolrSetup.getSolrServer(getServletContext());
    QueryResponse response = null;// www .  j  a  v  a2  s  . c  o  m

    try {
        response = solr.query(query);
    } catch (Throwable t) {
        log.error(t, t);
    }

    if (response == null) {
        throw new ServletException("Could not run search in IndividualListRdfController");
    }

    SolrDocumentList docs = response.getResults();

    if (docs == null) {
        throw new ServletException("Could not run search in IndividualListRdfController");
    }

    Model model = ModelFactory.createDefaultModel();
    for (SolrDocument doc : docs) {
        String uri = doc.get(VitroSearchTermNames.URI).toString();
        Resource resource = ResourceFactory.createResource(uri);
        RDFNode node = (RDFNode) ResourceFactory.createResource(vclassUri);
        model.add(resource, RDF.type, node);
    }

    res.setContentType(RDFXML_MIMETYPE);
    model.write(res.getOutputStream(), "RDF/XML");
}

From source file:edu.cornell.mannlib.vitro.webapp.search.controller.PagedSearchController.java

License:Open Source License

private SolrQuery getQuery(String queryText, int hitsPerPage, int startIndex, VitroRequest vreq) {
    // Lowercase the search term to support wildcard searches: Solr applies no text
    // processing to a wildcard search term.
    SolrQuery query = new SolrQuery(queryText);

    query.setStart(startIndex).setRows(hitsPerPage);

    // ClassGroup filtering param
    String classgroupParam = (String) vreq.getParameter(PARAM_CLASSGROUP);

    // rdf:type filtering param
    String typeParam = (String) vreq.getParameter(PARAM_RDFTYPE);

    if (!StringUtils.isBlank(classgroupParam)) {
        // ClassGroup filtering
        log.debug("Firing classgroup query ");
        log.debug("request.getParameter(classgroup) is " + classgroupParam);
        query.addFilterQuery(VitroSearchTermNames.CLASSGROUP_URI + ":\"" + classgroupParam + "\"");

        //with ClassGroup filtering we want type facets
        query.add("facet", "true");
        query.add("facet.limit", "-1");
        query.add("facet.field", VitroSearchTermNames.RDFTYPE);

    } else if (!StringUtils.isBlank(typeParam)) {
        // rdf:type filtering
        log.debug("Firing type query ");
        log.debug("request.getParameter(type) is " + typeParam);
        query.addFilterQuery(VitroSearchTermNames.RDFTYPE + ":\"" + typeParam + "\"");

        //with type filtering we don't have facets.            
    } else {//from  w  w w. j ava2  s . c o  m
        //When no filtering is set, we want ClassGroup facets
        query.add("facet", "true");
        query.add("facet.limit", "-1");
        query.add("facet.field", VitroSearchTermNames.CLASSGROUP_URI);
    }

    log.debug("Query = " + query.toString());
    return query;
}

From source file:edu.cornell.mannlib.vitro.webapp.searchengine.solr.SolrConversionUtils.java

License:Open Source License

/**
 * Convert from a SearchQuery to a SolrQuery, so the Solr server may execute
 * it.//  w ww .  j  a v a  2 s  .  c om
 */
@SuppressWarnings("deprecation")
static SolrQuery convertToSolrQuery(SearchQuery query) {
    SolrQuery solrQuery = new SolrQuery(query.getQuery());
    solrQuery.setStart(query.getStart());

    int rows = query.getRows();
    if (rows >= 0) {
        solrQuery.setRows(rows);
    }

    for (String fieldToReturn : query.getFieldsToReturn()) {
        solrQuery.addField(fieldToReturn);
    }

    Map<String, Order> sortFields = query.getSortFields();
    for (String sortField : sortFields.keySet()) {
        solrQuery.addSortField(sortField, convertToSolrOrder(sortFields.get(sortField)));
    }

    for (String filter : query.getFilters()) {
        solrQuery.addFilterQuery(filter);
    }

    if (!query.getFacetFields().isEmpty()) {
        solrQuery.setFacet(true);
    }

    for (String facetField : query.getFacetFields()) {
        solrQuery.addFacetField(facetField);
    }

    int facetLimit = query.getFacetLimit();
    if (facetLimit >= 0) {
        solrQuery.setFacetLimit(facetLimit);
    }

    int minCount = query.getFacetMinCount();
    if (minCount >= 0) {
        solrQuery.setFacetMinCount(minCount);
    }

    return solrQuery;
}

From source file:edu.harvard.lib.lcloud.ItemDAO.java

License:Open Source License

/**
 * Returns a MODS record for a given recordIdentifier.
 * @param id    a recordIdentifier for a solr document
 * @return      the ModsType for this recordidentifier
 * @see         ModsType//from ww  w. j a v a2  s  .  c  o  m
 */
public ModsType getMods(String id) throws JAXBException {
    SolrDocumentList docs;
    SolrDocument doc;
    ModsType modsType = new ModsType();
    HttpSolrServer server = null;
    try {
        server = SolrServer.getSolrConnection();
        SolrQuery query = new SolrQuery("recordIdentifier:" + id);
        QueryResponse response = server.query(query);
        docs = response.getResults();
        if (docs.size() == 0)
            throw new ResourceNotFoundException("Item " + id + " not found");
        else {
            doc = docs.get(0);
            modsType = getModsType(doc);
        }
    } catch (SolrServerException se) {
        se.printStackTrace();
        log.error(se.getMessage());
    }
    return modsType;
}