@Test public void testCreateQueryWithSortDesc() throws NoSuchMethodException, SecurityException { Method method = SampleRepository.class.getMethod("findByPopularityOrderByTitleDesc", Integer.class); Query query = createQueryForMethodWithArgs(method, new Object[] {1}); Assert.assertNotNull(query.getSort()); Assert.assertEquals(Sort.Direction.DESC, query.getSort().getOrderFor("title").getDirection()); }
private void processQueryOptions(SolrQuery solrQuery, Query query) { appendPagination(solrQuery, query.getPageRequest()); appendProjectionOnFields(solrQuery, query.getProjectionOnFields()); appendGroupByFields(solrQuery, query.getGroupByFields()); appendFilterQuery(solrQuery, query.getFilterQueries()); appendSort(solrQuery, query.getSort()); appendDefaultOperator(solrQuery, query.getDefaultOperator()); appendTimeAllowed(solrQuery, query.getTimeAllowed()); appendDefType(solrQuery, query.getDefType()); appendRequestHandler(solrQuery, query.getRequestHandler()); }
@Test public void testWithSort() throws NoSuchMethodException, SecurityException { Method method = SampleRepository.class.getMethod("findByNameWithSort", String.class, Sort.class); SolrQueryMethod queryMethod = new SolrQueryMethod(method, metadataMock, entityInformationCreatorMock); StringBasedSolrQuery solrQuery = new StringBasedSolrQuery(queryMethod, solrOperationsMock); Sort sort = new Sort(Direction.DESC, "popularity", "price"); org.springframework.data.solr.core.query.Query query = solrQuery.createQuery( new SolrParametersParameterAccessor(queryMethod, new Object[] {"spring", sort})); Assert.assertEquals("name:spring", queryParser.getQueryString(query)); Assert.assertEquals(sort, query.getSort()); }
@Test public void testWithProjectionOnSingleField() throws NoSuchMethodException, SecurityException { Method method = SampleRepository.class.getMethod("findByNameProjectionOnPopularity", String.class); SolrQueryMethod queryMethod = new SolrQueryMethod(method, metadataMock, entityInformationCreatorMock); StringBasedSolrQuery solrQuery = new StringBasedSolrQuery(queryMethod, solrOperationsMock); org.springframework.data.solr.core.query.Query query = solrQuery.createQuery( new SolrParametersParameterAccessor(queryMethod, new Object[] {"christoph"})); Assert.assertEquals("name:christoph*", queryParser.getQueryString(query)); Assert.assertEquals(1, query.getProjectionOnFields().size()); Assert.assertEquals("popularity", query.getProjectionOnFields().get(0).getName()); }
@Override public <T> ScoredPage<T> queryForPage(Query query, Class<T> clazz) { Assert.notNull(query, "Query must not be 'null'."); Assert.notNull(clazz, "Target class must not be 'null'."); QueryResponse response = query(query); List<T> beans = convertQueryResponseToBeans(response, clazz); SolrDocumentList results = response.getResults(); return new SolrResultPage<T>( beans, query.getPageRequest(), results.getNumFound(), results.getMaxScore()); }
@Override public <T> T queryForObject(Query query, Class<T> clazz) { Assert.notNull(query, "Query must not be 'null'."); Assert.notNull(clazz, "Target class must not be 'null'."); query.setPageRequest(new PageRequest(0, 1)); QueryResponse response = query(query); if (response.getResults().size() > 0) { if (response.getResults().size() > 1) { LOGGER.warn( "More than 1 result found for singe result query ('{}'), returning first entry in list"); } return (T) convertSolrDocumentListToBeans(response.getResults(), clazz).get(0); } return null; }