| 21285 |
kshitij.so |
1 |
package com.spice.profitmandi.web.services;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import java.io.IOException;
|
| 21744 |
ashik.ali |
5 |
|
| 21285 |
kshitij.so |
6 |
import java.io.InputStream;
|
|
|
7 |
import java.net.URISyntaxException;
|
|
|
8 |
import java.net.URL;
|
|
|
9 |
|
|
|
10 |
import org.apache.commons.io.IOUtils;
|
|
|
11 |
import org.apache.http.client.utils.URIBuilder;
|
| 21339 |
kshitij.so |
12 |
import org.slf4j.Logger;
|
|
|
13 |
import org.slf4j.LoggerFactory;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Value;
|
| 21287 |
kshitij.so |
15 |
import org.springframework.stereotype.Component;
|
| 21285 |
kshitij.so |
16 |
|
|
|
17 |
|
| 21287 |
kshitij.so |
18 |
@Component
|
| 21285 |
kshitij.so |
19 |
public class SolrService{
|
|
|
20 |
|
| 21339 |
kshitij.so |
21 |
private static final Logger logger=LoggerFactory.getLogger(SolrService.class);
|
| 21285 |
kshitij.so |
22 |
|
| 21339 |
kshitij.so |
23 |
@Value("${solr.url}")
|
|
|
24 |
private String SOLR_URL;
|
| 21285 |
kshitij.so |
25 |
private static final String limit ="20";
|
|
|
26 |
private static final String groupLimit = "10";
|
|
|
27 |
private static final String autoSuggestField = "title,subCategoryId,category_id,category,subCategory,score";
|
|
|
28 |
private static final String searchResultsField = "title,skuBundleId,id,thumbnail,ids";
|
|
|
29 |
private static final String outputFormat = "json";
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
public String getSuggestions(String search_text) throws URISyntaxException, IOException{
|
| 21517 |
kshitij.so |
34 |
|
| 21285 |
kshitij.so |
35 |
URIBuilder autoSuggestUrl = new URIBuilder(SOLR_URL);
|
|
|
36 |
autoSuggestUrl.addParameter("fl", autoSuggestField); //Fields to choose
|
|
|
37 |
autoSuggestUrl.addParameter("wt", outputFormat); //Output format
|
|
|
38 |
autoSuggestUrl.addParameter("group", "true"); //group data
|
|
|
39 |
autoSuggestUrl.addParameter("group.query", "category_id:3"); //group by category
|
|
|
40 |
autoSuggestUrl.addParameter("group.query", "category_id:5"); //group by category
|
|
|
41 |
autoSuggestUrl.addParameter("group.field", "subCategoryId"); //group by subCategory
|
|
|
42 |
autoSuggestUrl.addParameter("group.limit", groupLimit);
|
|
|
43 |
autoSuggestUrl.addParameter("q", "suggest:("+search_text+")");
|
|
|
44 |
URL url = autoSuggestUrl.build().toURL();
|
|
|
45 |
|
|
|
46 |
InputStream is = url.openStream();
|
|
|
47 |
String jsonString;
|
|
|
48 |
try{
|
|
|
49 |
jsonString = IOUtils.toString(is, "UTF-8");
|
|
|
50 |
}
|
|
|
51 |
finally{
|
|
|
52 |
is.close();
|
|
|
53 |
}
|
|
|
54 |
return jsonString;
|
|
|
55 |
}
|
|
|
56 |
|
| 21287 |
kshitij.so |
57 |
public String getSearchResults(String search_text, int offset) throws URISyntaxException, IOException{
|
| 21517 |
kshitij.so |
58 |
|
| 21285 |
kshitij.so |
59 |
URIBuilder generalSearchUrl = new URIBuilder(SOLR_URL);
|
|
|
60 |
generalSearchUrl.addParameter("q", search_text);
|
|
|
61 |
generalSearchUrl.addParameter("fl", searchResultsField); //Fields to choose
|
|
|
62 |
generalSearchUrl.addParameter("wt", outputFormat); //Output format
|
|
|
63 |
generalSearchUrl.addParameter("rows", limit); //limit
|
| 21288 |
kshitij.so |
64 |
generalSearchUrl.setParameter("start", String.valueOf(offset));
|
| 21285 |
kshitij.so |
65 |
URL url = generalSearchUrl.build().toURL();
|
|
|
66 |
InputStream is = url.openStream();
|
|
|
67 |
String jsonString;
|
|
|
68 |
try{
|
|
|
69 |
jsonString = IOUtils.toString(is, "UTF-8");
|
|
|
70 |
}
|
|
|
71 |
finally{
|
|
|
72 |
is.close();
|
|
|
73 |
}
|
|
|
74 |
return jsonString;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
}
|