View Javadoc
1   package com.gisgraphy.fulltext;
2   
3   import java.util.List;
4   import java.util.Map;
5   
6   import org.apache.solr.client.solrj.response.QueryResponse;
7   import org.apache.solr.client.solrj.response.SpellCheckResponse;
8   import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion;
9   
10  public class FulltextResultDtoBuilder {
11  
12  	/**
13  	 * @param response
14  	 *            The {@link QueryResponse} to build the DTO
15  	 */
16  	public FulltextResultsDto build(QueryResponse response) {
17  		FulltextResultsDto fulltextResultDto = new FulltextResultsDto();
18  		fulltextResultDto.results = SolrUnmarshaller.unmarshall(response);
19  		fulltextResultDto.QTime = response.getQTime();
20  		fulltextResultDto.numFound = response.getResults().getNumFound();
21  		fulltextResultDto.maxScore = response.getResults().getMaxScore();
22  		fulltextResultDto.resultsSize = fulltextResultDto.results == null ? 0 : fulltextResultDto.results.size();
23  		SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
24  		if (spellCheckResponse != null) {
25  			Map<String, Suggestion> suggestionMapInternal = spellCheckResponse.getSuggestionMap();
26  			if (suggestionMapInternal != null) {
27  				for (String key: suggestionMapInternal.keySet()){
28  					Suggestion suggestion = suggestionMapInternal.get(key);
29  					fulltextResultDto.suggestionMap.put(key, suggestion.getAlternatives());
30  					
31  				}
32  			}
33  			if (spellCheckResponse.getCollatedResult() != null) {
34  				fulltextResultDto.collatedResult = spellCheckResponse.getCollatedResult().trim();
35  			}
36  			List<Suggestion> suggestions = spellCheckResponse.getSuggestions();
37  			if (suggestions.size() != 0) {
38  				StringBuffer sb = new StringBuffer();
39  				for (Suggestion suggestion : suggestions) {
40  					sb.append(suggestion.getSuggestions().get(0)).append(" ");
41  				}
42  				fulltextResultDto.spellCheckProposal = sb.toString().trim();
43  			}
44  		}
45  		return fulltextResultDto;
46  
47  	}
48  
49  }