| Line 1... |
Line 1... |
| 1 |
package in.shop2020.dtrapi.services;
|
1 |
package in.shop2020.dtrapi.services;
|
| 2 |
|
2 |
|
| - |
|
3 |
import freemarker.template.utility.DeepUnwrap;
|
| 3 |
import in.shop2020.config.ConfigException;
|
4 |
import in.shop2020.config.ConfigException;
|
| 4 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
5 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 5 |
|
6 |
|
| 6 |
import java.io.IOException;
|
7 |
import java.io.IOException;
|
| 7 |
import java.io.InputStream;
|
8 |
import java.io.InputStream;
|
| Line 15... |
Line 16... |
| 15 |
|
16 |
|
| 16 |
|
17 |
|
| 17 |
|
18 |
|
| 18 |
public class SolrService{
|
19 |
public class SolrService{
|
| 19 |
|
20 |
|
| 20 |
private static URIBuilder autoSuggestUrl;
|
- |
|
| 21 |
private static URIBuilder generalSearchUrl;
|
- |
|
| 22 |
private static String SOLR_URL;
|
21 |
private static String SOLR_URL;
|
| - |
|
22 |
private static final String limit ="20";
|
| - |
|
23 |
private static final String autoSuggestField = "title,subCategoryId,category_id,category,subCategory,score";
|
| - |
|
24 |
private static final String searchResultsField = "fl,title,skuBundleId,id,thumbnail";
|
| - |
|
25 |
private static final String outputFormat = "json";
|
| 23 |
private static Logger log = Logger.getLogger(Class.class);
|
26 |
private static Logger log = Logger.getLogger(Class.class);
|
| 24 |
|
27 |
|
| 25 |
static{
|
28 |
static{
|
| 26 |
synchronized(SolrService.class){
|
29 |
synchronized(SolrService.class){
|
| 27 |
try {
|
30 |
try {
|
| 28 |
SOLR_URL = ConfigClient.getClient().get("dtr_solr_url");
|
31 |
SOLR_URL = ConfigClient.getClient().get("dtr_solr_url");
|
| 29 |
} catch (ConfigException e) {
|
32 |
} catch (ConfigException e) {
|
| 30 |
log.error("Error while gettting dtr_solr_url param from config service", e);
|
33 |
log.error("Error while gettting dtr_solr_url param from config service", e);
|
| 31 |
SOLR_URL = "http://localhost:8983/solr/collection1/select";
|
34 |
SOLR_URL = "http://localhost:8983/solr/collection1/select";
|
| 32 |
}
|
35 |
}
|
| 33 |
try {
|
- |
|
| 34 |
autoSuggestUrl = new URIBuilder(SOLR_URL);
|
- |
|
| 35 |
} catch (URISyntaxException e) {
|
- |
|
| 36 |
log.error("Error while building solr_url "+e);
|
- |
|
| 37 |
}
|
- |
|
| 38 |
autoSuggestUrl.addParameter("fl", "title,subCategoryId,category_id,category,subCategory,score"); //Fields to choose
|
- |
|
| 39 |
autoSuggestUrl.addParameter("wt", "json"); //Output format
|
- |
|
| 40 |
autoSuggestUrl.addParameter("rows", "20"); //search limit for each grouped field or query
|
- |
|
| 41 |
|
- |
|
| 42 |
generalSearchUrl.addParameter("fl", "title,skuBundleId,id,thumbnail");
|
- |
|
| 43 |
generalSearchUrl.addParameter("wt", "json");
|
- |
|
| 44 |
autoSuggestUrl.addParameter("rows", "20");
|
- |
|
| 45 |
}
|
36 |
}
|
| 46 |
}
|
37 |
}
|
| 47 |
|
38 |
|
| 48 |
public String getSuggestions(String search_text) throws URISyntaxException, IOException{
|
39 |
public String getSuggestions(String search_text) throws URISyntaxException, IOException{
|
| - |
|
40 |
URIBuilder autoSuggestUrl = new URIBuilder(SOLR_URL);
|
| - |
|
41 |
autoSuggestUrl.addParameter("fl", autoSuggestField); //Fields to choose
|
| - |
|
42 |
autoSuggestUrl.addParameter("wt", outputFormat); //Output format
|
| - |
|
43 |
autoSuggestUrl.addParameter("rows", limit); //search limit for each grouped field or query
|
| 49 |
autoSuggestUrl.setParameter("q", "suggest:("+search_text+")");
|
44 |
autoSuggestUrl.addParameter("q", "suggest:("+search_text+")");
|
| 50 |
URL url = autoSuggestUrl.build().toURL();
|
45 |
URL url = autoSuggestUrl.build().toURL();
|
| 51 |
log.info("Search Url "+url.toString());
|
46 |
log.info("Search Url Autosuggest"+url.toString());
|
| 52 |
InputStream is = url.openStream();
|
47 |
InputStream is = url.openStream();
|
| 53 |
String jsonString;
|
48 |
String jsonString;
|
| 54 |
try{
|
49 |
try{
|
| 55 |
jsonString = IOUtils.toString(is, "UTF-8");
|
50 |
jsonString = IOUtils.toString(is, "UTF-8");
|
| 56 |
}
|
51 |
}
|
| Line 59... |
Line 54... |
| 59 |
}
|
54 |
}
|
| 60 |
return jsonString;
|
55 |
return jsonString;
|
| 61 |
}
|
56 |
}
|
| 62 |
|
57 |
|
| 63 |
public String getSearchResults(String search_text, String offset) throws URISyntaxException, IOException{
|
58 |
public String getSearchResults(String search_text, String offset) throws URISyntaxException, IOException{
|
| - |
|
59 |
URIBuilder generalSearchUrl = new URIBuilder(SOLR_URL);
|
| 64 |
generalSearchUrl.setParameter("q", search_text);
|
60 |
generalSearchUrl.addParameter("q", search_text);
|
| - |
|
61 |
generalSearchUrl.addParameter("fl", autoSuggestField); //Fields to choose
|
| - |
|
62 |
generalSearchUrl.addParameter("wt", outputFormat); //Output format
|
| - |
|
63 |
generalSearchUrl.addParameter("rows", limit); //limit
|
| 65 |
if (offset!=null && !offset.isEmpty()){
|
64 |
if (offset!=null && !offset.isEmpty()){
|
| 66 |
generalSearchUrl.setParameter("start", offset);
|
65 |
generalSearchUrl.setParameter("start", offset);
|
| 67 |
}
|
66 |
}
|
| 68 |
URL url = generalSearchUrl.build().toURL();
|
67 |
URL url = generalSearchUrl.build().toURL();
|
| 69 |
log.info("Search Url "+url.toString());
|
68 |
log.info("Search Url Search Results"+url.toString());
|
| 70 |
InputStream is = url.openStream();
|
69 |
InputStream is = url.openStream();
|
| 71 |
String jsonString;
|
70 |
String jsonString;
|
| 72 |
try{
|
71 |
try{
|
| 73 |
jsonString = IOUtils.toString(is, "UTF-8");
|
72 |
jsonString = IOUtils.toString(is, "UTF-8");
|
| 74 |
}
|
73 |
}
|