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

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

Introduction

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

Prototype

public SolrQuery setStart(Integer start) 

Source Link

Usage

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.
 *//*from w w w. j  a v a2s.com*/
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;
    }/*ww w . ja v  a  2 s .co 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;/*from  www .  j av a  2 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.AutocompleteController.java

License:Open Source License

private SolrQuery getQuery(String queryStr, VitroRequest vreq) {

    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;
    }/*ww  w.j av a 2  s. co m*/

    SolrQuery query = new SolrQuery();
    query.setStart(0).setRows(DEFAULT_MAX_HIT_COUNT);

    setNameQuery(query, queryStr, vreq);

    // Filter by type
    String typeParam = (String) vreq.getParameter(PARAM_RDFTYPE);
    String multipleTypesParam = (String) vreq.getParameter(PARAM_MULTIPLE_RDFTYPE);
    if (typeParam != null) {
        addFilterQuery(query, typeParam, multipleTypesParam);
    }

    query.setFields(VitroSearchTermNames.NAME_RAW, VitroSearchTermNames.URI); // fields to retrieve

    // 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.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  a va2 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./*from  w  ww .  j  ava  2  s .  c o  m*/
 */
@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 SolrDocumentList for a given set of search parameters. Search parameters are parsed 
 * and mapped into solr (solrj) query syntax, and solr query is performed.
 * @param queryParams query parameters to map to a solr query
 * @return      the SolrDocumentList for this query
 * @see         SolrDocumentList/*from  w  ww .j ava  2  s  . c  o m*/
 */

private SolrDocumentList doQuery(MultivaluedMap<String, String> queryParams) {
    SolrDocumentList docs = null;
    HttpSolrServer server = null;
    ArrayList<String> queryList = new ArrayList<String>();
    server = SolrServer.getSolrConnection();
    SolrQuery query = new SolrQuery();
    for (String key : queryParams.keySet()) {
        String value = queryParams.getFirst(key);
        System.out.println(key + " : " + queryParams.getFirst(key) + "\n");
        if (key.equals("start")) {
            int startNo = Integer.parseInt(value);
            if (startNo < 0)
                startNo = 0;
            query.setStart(startNo);
        } else if (key.equals("limit")) {
            limit = Integer.parseInt(value);
            query.setRows(limit);
        } else if (key.equals("sort.asc") || key.equals("sort"))
            query.setSort(value, ORDER.asc);
        else if (key.equals("sort.desc"))
            query.setSort(value, ORDER.desc);
        else if (key.startsWith("facet")) {
            query.setFacet(true);
            String[] facetArray = value.split(",");
            for (String f : facetArray) {
                query.addFacetField(f);
            }
        } else {
            if (key.endsWith("_exact"))
                queryList.add(key.replace("_exact", "") + ":\"" + value + "\"");
            else {
                if (value.contains(" "))
                    value = "( " + value.replaceAll(" ", " AND ") + ")";
                if (key.equals("q"))
                    queryList.add("keyword:" + value);
                else
                    queryList.add(key + "_keyword:" + value);
            }
        }
    }

    Iterator<String> it = queryList.iterator();
    String queryStr = "";
    while (it.hasNext()) {
        String qTerm = (String) it.next();
        System.out.print("QT: " + qTerm + "\n");
        queryStr += qTerm;
        System.out.print("QS: " + queryStr + "\n");
        if (it.hasNext())
            queryStr += " AND ";
    }
    System.out.print("queryStr: " + queryStr);
    query.setQuery(queryStr);
    QueryResponse response = null;
    try {
        response = server.query(query);
    } catch (SolrServerException se) {
        log.error(se.getMessage());
        throw new BadParameterException(se.getMessage());
    } catch (RemoteSolrException rse) {
        if (rse.getMessage().contains("SyntaxError")) {
            log.error("solr syntax error");
            throw new BadParameterException("Incorrect query syntax");
        } else {
            String msg = rse.getMessage().replace("_keyword", "");
            log.error(msg);
            throw new BadParameterException("Incorrect query syntax:" + msg);
        }
    }
    List<FacetField> facets = response.getFacetFields();
    facet = null;
    if (facets != null) {
        facet = new Facet();
        List<FacetType> facetTypes = new ArrayList<FacetType>();
        for (FacetField facetField : facets) {
            List<FacetTerm> facetTerms = new ArrayList<FacetTerm>();
            FacetType facetType = new FacetType();
            facetType.setFacetName(facetField.getName());
            List<FacetField.Count> facetEntries = facetField.getValues();
            for (FacetField.Count fcount : facetEntries) {
                if (fcount.getCount() > 0) {
                    FacetTerm facetTerm = new FacetTerm();
                    facetTerm.setTermName(fcount.getName());
                    facetTerm.setTermCount(fcount.getCount());
                    //System.out.println(fcount.getName() + ": " + fcount.getCount());
                    facetTerms.add(facetTerm);
                }
            }
            facetType.setFacetTerms(facetTerms);
            facetTypes.add(facetType);
        }
        facet.setFacetTypes(facetTypes);
    }
    docs = response.getResults();
    return docs;
}

From source file:edu.harvard.liblab.ecru.rs.ResourceUtils.java

License:Open Source License

public static SolrQuery createSolrQuery(String q, List<String> fqs, String fl, String start, String rows,
        HashMap<String, List<String>> facetMap, String sort) {
    SolrQuery query = new SolrQuery();
    if (q == null || q.trim().isEmpty()) {
        q = QUERY_ALL;/*from  w  ww. j  a va 2  s. c  om*/
    }
    query.setQuery(q);
    if (fqs != null && !fqs.isEmpty()) {
        query.setFilterQueries(fqs.toArray(new String[fqs.size()]));
    }
    try {
        Integer s = new Integer(start);
        query.setStart(s);

    } catch (NumberFormatException e) {
        if (start != null)
            System.out.println("start not integer: " + start);
    }
    try {
        Integer r = new Integer(rows);
        query.setRows(r);

    } catch (NumberFormatException e) {
        if (rows != null)
            System.out.println("rows not integer: " + rows);
    }
    if (fl != null && !fl.isEmpty()) {
        if (fl.indexOf("type") < 0) {
            fl += ",type";
        }
        String[] fields = fl.split(",");
        for (int i = 0; i < fields.length; i++) {
            fields[i] = fields[i].trim();
        }

        query.setFields(fields);

    }
    if (sort != null && !sort.trim().isEmpty()) {
        String[] sortArr = sort.split(",");
        ArrayList<SortClause> clauses = new ArrayList<SortClause>();
        for (String s : sortArr) {
            if (!s.isEmpty()) {
                String[] sc = s.split(" ");
                String v = sc[0];
                String order = "asc";
                if (sc.length == 2) {
                    order = sc[1];
                }
                clauses.add(new SortClause(v, (order.equals("asc") ? ORDER.asc : ORDER.desc)));
            }
        }
        query.setSorts(clauses);
    }
    query = addFacetsToQuery(query, facetMap);
    return query;
}

From source file:edu.tamu.tcat.trc.digires.books.hathitrust.HTFilesSearchService.java

License:Apache License

@Override
public CopySearchResult find(ContentQuery query) throws ResourceAccessException {
    if (solrServerFuture == null)
        throw new IllegalStateException("Service is disposed");

    // HACK hard coded. Should be provided to the service
    try {//from   w  w w .j  a v a  2  s .  c o m
        HttpSolrClient solrServer = solrServerFuture.get(2, TimeUnit.MINUTES);
        Objects.requireNonNull(solrServer, "No active connection to Solr Server");

        String queryString = formatQueryString(query);

        SolrQuery solrQuery = new SolrQuery(queryString);
        solrQuery.setRows(Integer.valueOf(query.getLimit()));
        solrQuery.setStart(Integer.valueOf(query.getOffset()));
        //         solrQuery.addFilterQuery(buildDateFilter(query));
        QueryResponse response = solrServer.query(solrQuery);
        return getSearchResults(response);
    } catch (Exception ex) {
        throw new ResourceAccessException("", ex);
    }

}

From source file:edu.unc.lib.dl.search.solr.service.SolrSearchService.java

License:Apache License

/**
 * Constructs a SolrQuery object from the search state specified within a SearchRequest object. The request may
 * optionally request to retrieve facet results in addition to search results.
 * // w ww  .  j  av a2 s  . c  o  m
 * @param searchRequest
 * @param isRetrieveFacetsRequest
 * @return
 */
protected SolrQuery generateSearch(SearchRequest searchRequest) {
    SearchState searchState = (SearchState) searchRequest.getSearchState();
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder termQuery = new StringBuilder();

    // Generate search term query string
    addSearchFields(searchState, termQuery);

    // Add range Fields to the query
    addRangeFields(searchState, termQuery);

    // No query terms given, make it an everything query
    StringBuilder query = new StringBuilder();
    if (termQuery.length() == 0) {
        query.append("*:* ");
    } else {
        query.append('(').append(termQuery).append(')');
    }

    // Add access restrictions to query
    try {
        addAccessRestrictions(query, searchRequest.getAccessGroups());
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.debug("User had no access groups", e);
        return null;
    }

    // Add query
    solrQuery.setQuery(query.toString());

    if (searchState.getResultFields() != null) {
        for (String field : searchState.getResultFields()) {
            String solrFieldName = solrSettings.getFieldName(field);
            if (solrFieldName != null)
                solrQuery.addField(solrFieldName);
        }
    }

    if (searchState.getRollup() != null && searchState.getRollup()) {
        solrQuery.set(GroupParams.GROUP, true);
        if (searchState.getRollupField() == null)
            solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(SearchFieldKeys.ROLLUP_ID.name()));
        else
            solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(searchState.getRollupField()));

        solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true);
        if (searchState.getFacetsToRetrieve() != null && searchState.getFacetsToRetrieve().size() > 0) {
            solrQuery.set(GroupParams.GROUP_FACET, true);
        }
    }

    // Add sort parameters
    List<SearchSettings.SortField> sortFields = searchSettings.sortTypes.get(searchState.getSortType());
    if (sortFields != null) {
        for (int i = 0; i < sortFields.size(); i++) {
            SearchSettings.SortField sortField = sortFields.get(i);
            SolrQuery.ORDER sortOrder = SolrQuery.ORDER.valueOf(sortField.getSortOrder());
            if (!searchState.getSortNormalOrder())
                sortOrder = sortOrder.reverse();
            solrQuery.addSort(solrSettings.getFieldName(sortField.getFieldName()), sortOrder);
        }
    }

    // Set requested resource types
    String resourceTypeFilter = this.getResourceTypeFilter(searchState.getResourceTypes());
    if (resourceTypeFilter != null) {
        solrQuery.addFilterQuery(resourceTypeFilter);
    }

    // Turn on faceting
    if (searchRequest.isRetrieveFacets()) {
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        if (searchState.getBaseFacetLimit() != null)
            solrQuery.setFacetLimit(searchState.getBaseFacetLimit());

        if (searchState.getFacetsToRetrieve() != null) {
            // Add facet fields
            for (String facetName : searchState.getFacetsToRetrieve()) {
                String facetField = solrSettings.getFieldName(facetName);
                if (facetField != null)
                    solrQuery.addFacetField(solrSettings.getFieldName(facetName));
            }
        }
    }

    // Override the base facet limit if overrides are given.
    if (searchState.getFacetLimits() != null) {
        for (Entry<String, Integer> facetLimit : searchState.getFacetLimits().entrySet()) {
            solrQuery.add("f." + solrSettings.getFieldName(facetLimit.getKey()) + ".facet.limit",
                    facetLimit.getValue().toString());
        }
    }

    // Add facet limits
    Map<String, Object> facets = searchState.getFacets();
    if (facets != null) {
        Iterator<Entry<String, Object>> facetIt = facets.entrySet().iterator();
        while (facetIt.hasNext()) {
            Entry<String, Object> facetEntry = facetIt.next();

            if (facetEntry.getValue() instanceof String) {
                LOG.debug("Adding facet " + facetEntry.getKey() + " as a String");
                // Add Normal facets
                solrQuery.addFilterQuery(solrSettings.getFieldName(facetEntry.getKey()) + ":\""
                        + SolrSettings.sanitize((String) facetEntry.getValue()) + "\"");
            } else {
                LOG.debug("Adding facet " + facetEntry.getKey() + " as a "
                        + facetEntry.getValue().getClass().getName());
                facetFieldUtil.addToSolrQuery(facetEntry.getValue(), solrQuery);
            }
        }
    }

    // Scope hierarchical facet results to the highest tier selected within the facet tree
    if (searchRequest.isRetrieveFacets() && searchRequest.isApplyCutoffs()
            && searchState.getFacetsToRetrieve() != null) {
        Set<String> facetsQueried = searchState.getFacets().keySet();
        // Apply closing cutoff to all cutoff facets that are being retrieved but not being queried for
        for (String fieldKey : searchState.getFacetsToRetrieve()) {
            if (!facetsQueried.contains(fieldKey)) {
                facetFieldUtil.addDefaultFacetPivot(fieldKey, solrQuery);
            }
        }

        // Add individual facet field sorts if they are present.
        if (searchState.getFacetSorts() != null) {
            for (Entry<String, String> facetSort : searchState.getFacetSorts().entrySet()) {
                solrQuery.add("f." + solrSettings.getFieldName(facetSort.getKey()) + ".facet.sort",
                        facetSort.getValue());
            }
        }
    }

    // Set Navigation options
    if (searchState.getStartRow() != null)
        solrQuery.setStart(searchState.getStartRow());
    if (searchState.getRowsPerPage() != null)
        solrQuery.setRows(searchState.getRowsPerPage());

    return solrQuery;
}