SolrSearchDao implements SearchDao { protected SolrQuery buildSearchRequest( SearchRequest searchRequest, String fieldList) throws IOException, SolrServerException { SolrQuery query = new SolrQuery() .setStart(searchRequest.getFrom()) .setRows(searchRequest.getSize()) .setQuery(searchRequest.getQuery()) .setShowDebugInfo(LOG.isDebugEnabled()); for (SortField sortField : searchRequest.getSort()) { query.addSort(sortField.getField(), getSolrSortOrder(sortField.getSortOrder())); } List<String> fields = searchRequest.getFields(); if (fieldList == null) { fieldList = "*"; if (fields != null) { fieldList = StringUtils.join(fields, ","); } } query.set("fl", fieldList); List<String> facetFields = searchRequest.getFacetFields(); if (facetFields != null) { facetFields.forEach(query::addFacetField); } query.set("collection", getCollections(searchRequest.getIndices())); return query; } SolrSearchDao(SolrClient client, AccessConfig accessConfig); @Override SearchResponse search(SearchRequest searchRequest); SearchResponse search(SearchRequest searchRequest, String fieldList); @Override GroupResponse group(GroupRequest groupRequest);  }
@Test public void buildSearchRequestShouldReturnSolrQuery() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro", "snort")); searchRequest.setSize(5); searchRequest.setFrom(10); searchRequest.setQuery("query"); SortField sortField = new SortField(); sortField.setField("sortField"); sortField.setSortOrder("ASC"); searchRequest.setSort(Collections.singletonList(sortField)); searchRequest.setFields(Arrays.asList("field1", "field2")); searchRequest.setFacetFields(Arrays.asList("facetField1", "facetField2")); SolrQuery exceptedSolrQuery = new SolrQuery() .setStart(10) .setRows(5) .setQuery("query") .addSort("sortField", SolrQuery.ORDER.asc) .addField("field1").addField("field2") .addFacetField("facetField1", "facetField2"); exceptedSolrQuery.set("collection", "bro,snort"); NamedList<Object> namedList = new NamedList<>(); namedList.add("collections", Arrays.asList("bro","snort")); when(client.request(any(), any())).thenReturn(namedList); SolrQuery solrQuery = solrSearchDao.buildSearchRequest(searchRequest, "field1,field2"); assertThat(solrQuery, new SolrQueryMatcher(exceptedSolrQuery).asHamcrestMatcher()); }