List of usage examples for org.apache.solr.client.solrj SolrQuery getSortField
public String getSortField()
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
/** * Process the {@see org.apache.solr.client.solrj.response.QueryResponse} from a SOLR search and return * a {@link au.org.ala.biocache.dto.SearchResultDTO} * * @param qr//from www .j a v a2s.c om * @param solrQuery * @return */ private SearchResultDTO processSolrResponse(SearchRequestParams params, QueryResponse qr, SolrQuery solrQuery, Class resultClass) { SearchResultDTO searchResult = new SearchResultDTO(); SolrDocumentList sdl = qr.getResults(); // Iterator it = qr.getResults().iterator() // Use for download List<FacetField> facets = qr.getFacetFields(); List<FacetField> facetDates = qr.getFacetDates(); Map<String, Integer> facetQueries = qr.getFacetQuery(); if (facetDates != null) { if (logger.isDebugEnabled()) { logger.debug("Facet dates size: " + facetDates.size()); } facets.addAll(facetDates); } List<OccurrenceIndex> results = qr.getBeans(resultClass); //facet results searchResult.setTotalRecords(sdl.getNumFound()); searchResult.setStartIndex(sdl.getStart()); searchResult.setPageSize(solrQuery.getRows()); //pageSize searchResult.setStatus("OK"); String[] solrSort = StringUtils.split(solrQuery.getSortField(), " "); // e.g. "taxon_name asc" if (logger.isDebugEnabled()) { logger.debug("sortField post-split: " + StringUtils.join(solrSort, "|")); } searchResult.setSort(solrSort[0]); // sortField searchResult.setDir(solrSort[1]); // sortDirection searchResult.setQuery(params.getUrlParams()); //this needs to be the original URL>>>> searchResult.setOccurrences(results); List<FacetResultDTO> facetResults = buildFacetResults(facets); //all belong to uncertainty range for now if (facetQueries != null && !facetQueries.isEmpty()) { Map<String, String> rangeMap = rangeBasedFacets.getRangeMap("uncertainty"); List<FieldResultDTO> fqr = new ArrayList<FieldResultDTO>(); for (String value : facetQueries.keySet()) { if (facetQueries.get(value) > 0) fqr.add(new FieldResultDTO(rangeMap.get(value), facetQueries.get(value), value)); } facetResults.add(new FacetResultDTO("uncertainty", fqr)); } //handle all the range based facets if (qr.getFacetRanges() != null) { for (RangeFacet rfacet : qr.getFacetRanges()) { List<FieldResultDTO> fqr = new ArrayList<FieldResultDTO>(); if (rfacet instanceof Numeric) { Numeric nrfacet = (Numeric) rfacet; List<RangeFacet.Count> counts = nrfacet.getCounts(); //handle the before if (nrfacet.getBefore().intValue() > 0) { fqr.add(new FieldResultDTO("[* TO " + getUpperRange(nrfacet.getStart().toString(), nrfacet.getGap(), false) + "]", nrfacet.getBefore().intValue())); } for (RangeFacet.Count count : counts) { String title = getRangeValue(count.getValue(), nrfacet.getGap()); fqr.add(new FieldResultDTO(title, count.getCount())); } //handle the after if (nrfacet.getAfter().intValue() > 0) { fqr.add(new FieldResultDTO("[" + nrfacet.getEnd().toString() + " TO *]", nrfacet.getAfter().intValue())); } facetResults.add(new FacetResultDTO(nrfacet.getName(), fqr)); } } } //update image URLs for (OccurrenceIndex oi : results) { updateImageUrls(oi); } searchResult.setFacetResults(facetResults); // The query result is stored in its original format so that all the information // returned is available later on if needed searchResult.setQr(qr); return searchResult; }
From source file:org.ambraproject.search.service.SolrSearchService.java
License:Apache License
/** * Add a <i>sort</i> (on a single field) clause to the <code>query</code> parameter. If the * <code>SearchParameters.sort</code> variable contains a single value (no white space), * then that value is assumed to be a field name. If the <code>SearchParameters.sort</code> * variable contains two values (separated by whitespace), then the first is assumed to be a * field name and the second is assumed to be a <i>sort direction</i>, * one of <strong>desc</strong> or <strong>asc</strong>. * <p/>/*from ww w. j a v a 2 s .co m*/ * If there is only one value in the <code>SearchParameters.sort</code> variable or if the second * value is not (non-case-sensitive) <strong>asc</strong>, then the <i>sort direction</i> defaults * to <strong>desc</strong>. * * @param query The SolrQuery which will have a <i>sort</i> clause attached * @param sp The SearchParameters DTO which contains the <code>sort</code> field used by this method */ private void setSort(SolrQuery query, SearchParameters sp) throws ApplicationException { if (log.isDebugEnabled()) { log.debug("SearchParameters.sort = " + sp.getSort()); } if (sp.getSort().length() > 0) { String sortKey = sp.getSort(); String sortValue = (String) validSorts.get(sortKey); if (sortValue == null) { throw new ApplicationException("Invalid sort of '" + sp.getSort() + "' specified."); } //First tokenize up defined sorts into tokens on comma: "," StringTokenizer sortTokens = new StringTokenizer(sortValue, ","); while (sortTokens.hasMoreTokens()) { //Now tokenize each sort command on space StringTokenizer curSort = new StringTokenizer(sortTokens.nextToken()); String fieldName = curSort.nextToken(); // First token String sortDirection = null; if (curSort.hasMoreTokens()) { sortDirection = curSort.nextToken(); // Second token } if (sortDirection == null || !sortDirection.toLowerCase().equals("asc")) { query.addSortField(fieldName, SolrQuery.ORDER.desc); } else { query.addSortField(fieldName, SolrQuery.ORDER.asc); } } } if (query.getSortField() == null || query.getSortField().length() == 0) { //Always default to score if it's not defined query.addSortField("score", SolrQuery.ORDER.desc); //If two articles are ranked the same, give the one with a more recent publish date a bump query.addSortField("publication_date", SolrQuery.ORDER.desc); //If everything else is equal, order by id query.addSortField("id", SolrQuery.ORDER.desc); } }
From source file:org.ambraproject.service.feed.FeedServiceImpl.java
License:Apache License
private void setSort(SolrQuery query, FeedSearchParameters sp) throws ApplicationException { if (log.isDebugEnabled()) { log.debug("SearchParameters.sort = " + sp.getSort()); }/* www .j a va 2 s. com*/ if (sp.getSort().length() > 0) { String sortKey = sp.getSort(); String sortValue = (String) validSorts.get(sortKey); if (sortValue == null) { throw new ApplicationException("Invalid sort of '" + sp.getSort() + "' specified."); } String[] sortOptions = SORT_OPTION_PATTERN.split(sortValue); for (String sortOption : sortOptions) { sortOption = sortOption.trim(); int index = sortOption.lastIndexOf(" "); String fieldName = sortOption; String sortDirection = null; if (index != -1) { fieldName = sortOption.substring(0, index); sortDirection = sortOption.substring(index + 1).trim(); } if (sortDirection == null || !sortDirection.toLowerCase().equals("asc")) { query.addSortField(fieldName, SolrQuery.ORDER.desc); } else { query.addSortField(fieldName, SolrQuery.ORDER.asc); } } } if (query.getSortField() == null || query.getSortField().length() == 0) { //Always default to score if it's not defined query.addSortField("score", SolrQuery.ORDER.desc); //If two articles are ranked the same, give the one with a more recent publish date a bump query.addSortField("publication_date", SolrQuery.ORDER.desc); //If everything else is equal, order by id query.addSortField("id", SolrQuery.ORDER.desc); } }
From source file:org.ambraproject.service.search.SolrSearchService.java
License:Apache License
/** * Add a <i>sort</i> (on a single field) clause to the <code>query</code> parameter. If the * <code>SearchParameters.sort</code> variable contains a single value (no white space), then that value is assumed to * be a field name. If the <code>SearchParameters.sort</code> variable contains two values (separated by whitespace), * then the first is assumed to be a field name and the second is assumed to be a <i>sort direction</i>, one of * <strong>desc</strong> or <strong>asc</strong>. * <p/>// w ww . j a v a 2 s. com * If there is only one value in the <code>SearchParameters.sort</code> variable or if the second value is not * (non-case-sensitive) <strong>asc</strong>, then the <i>sort direction</i> defaults to <strong>desc</strong>. * * @param query The SolrQuery which will have a <i>sort</i> clause attached * @param sp The SearchParameters DTO which contains the <code>sort</code> field used by this method */ private void setSort(SolrQuery query, SearchParameters sp) throws ApplicationException { if (log.isDebugEnabled()) { log.debug("SearchParameters.sort = " + sp.getSortKey()); } if (sp.getSortKey().length() > 0 || (sp.getSortValue() != null && sp.getSortValue().length() > 0)) { String sortKey = sp.getSortKey(); String sortValue = (String) validSorts.get(sortKey); //This bit allows a consumer of the method to explicitly set the sort instead of specifying it by key if (sp.getSortValue() != null && sp.getSortValue().length() > 0) { sortValue = sp.getSortValue(); } else { if (sortValue == null) { throw new ApplicationException("Invalid sort key of '" + sp.getSortKey() + "' specified."); } } String[] sortOptions = SORT_OPTION_PATTERN.split(sortValue); for (String sortOption : sortOptions) { sortOption = sortOption.trim(); int index = sortOption.lastIndexOf(" "); String fieldName = sortOption; String sortDirection = null; if (index != -1) { fieldName = sortOption.substring(0, index); sortDirection = sortOption.substring(index + 1).trim(); } if (sortDirection == null || !sortDirection.toLowerCase().equals("asc")) { query.addSortField(fieldName, SolrQuery.ORDER.desc); } else { query.addSortField(fieldName, SolrQuery.ORDER.asc); } } } if (query.getSortField() == null || query.getSortField().length() == 0) { //Always default to score if it's not defined query.addSortField("score", SolrQuery.ORDER.desc); //If two articles are ranked the same, give the one with a more recent publish date a bump query.addSortField("publication_date", SolrQuery.ORDER.desc); //If everything else is equal, order by id query.addSortField("id", SolrQuery.ORDER.desc); } }
From source file:org.mycore.solr.search.MCRConditionTransformer.java
License:Open Source License
public static SolrQuery getSolrQuery(@SuppressWarnings("rawtypes") MCRCondition condition, List<MCRSortBy> sortBy, int maxResults) { String queryString = getQueryString(condition); SolrQuery q = applySortOptions(new SolrQuery(queryString), sortBy); q.setIncludeScore(true);/* w w w. ja v a 2 s .co m*/ q.setRows(maxResults == 0 ? Integer.MAX_VALUE : maxResults); String sort = q.getSortField(); LOGGER.info("Legacy Query transformed to: " + q.getQuery() + (sort != null ? " " + sort : "")); return q; }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testWithNullSort() { SimpleStringCriteria criteria = new SimpleStringCriteria("field_1:value_1"); Query query = new SimpleQuery(criteria); query.addSort(null); // do this explicitly SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertNull(solrQuery.getSortField()); Assert.assertTrue(solrQuery.getSorts().isEmpty()); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testWithSortAscOnSingleField() { SimpleStringCriteria criteria = new SimpleStringCriteria("field_1:value_1"); Query query = new SimpleQuery(criteria); query.addSort(new Sort("field_2")); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals("field_2 asc", solrQuery.getSortField()); Assert.assertEquals(1, solrQuery.getSorts().size()); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testWithSortDescOnSingleField() { SimpleStringCriteria criteria = new SimpleStringCriteria("field_1:value_1"); Query query = new SimpleQuery(criteria); query.addSort(new Sort(Sort.Direction.DESC, "field_2")); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals("field_2 desc", solrQuery.getSortField()); Assert.assertEquals(1, solrQuery.getSorts().size()); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testWithSortAscMultipleFields() { SimpleStringCriteria criteria = new SimpleStringCriteria("field_1:value_1"); Query query = new SimpleQuery(criteria); query.addSort(new Sort("field_2", "field_3")); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals("field_2 asc,field_3 asc", solrQuery.getSortField()); Assert.assertEquals(2, solrQuery.getSorts().size()); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testWithSortDescMultipleFields() { SimpleStringCriteria criteria = new SimpleStringCriteria("field_1:value_1"); Query query = new SimpleQuery(criteria); query.addSort(new Sort(Sort.Direction.DESC, "field_2", "field_3")); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals("field_2 desc,field_3 desc", solrQuery.getSortField()); Assert.assertEquals(2, solrQuery.getSorts().size()); }