| 20210 |
kshitij.so |
1 |
package in.shop2020.dtrapi.services;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.config.ConfigException;
|
|
|
4 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
5 |
|
|
|
6 |
import java.io.IOException;
|
|
|
7 |
import java.io.InputStream;
|
|
|
8 |
import java.net.MalformedURLException;
|
|
|
9 |
import java.net.URISyntaxException;
|
|
|
10 |
import java.net.URL;
|
|
|
11 |
|
|
|
12 |
import org.apache.commons.io.IOUtils;
|
|
|
13 |
import org.apache.http.client.utils.URIBuilder;
|
|
|
14 |
import org.apache.log4j.Logger;
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
public class SolrService{
|
|
|
19 |
|
|
|
20 |
private static URIBuilder autoSuggestUrl;
|
|
|
21 |
private static URIBuilder generalSearchUrl;
|
|
|
22 |
private static String SOLR_URL;
|
|
|
23 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
24 |
|
|
|
25 |
static{
|
| 20214 |
kshitij.so |
26 |
try {
|
|
|
27 |
SOLR_URL = ConfigClient.getClient().get("dtr_solr_url");
|
|
|
28 |
} catch (ConfigException e) {
|
|
|
29 |
log.error("Error while gettting dtr_solr_url param from config service", e);
|
|
|
30 |
SOLR_URL = "http://localhost:8983/solr/collection1/select";
|
| 20210 |
kshitij.so |
31 |
}
|
| 20214 |
kshitij.so |
32 |
try {
|
|
|
33 |
autoSuggestUrl = new URIBuilder(SOLR_URL);
|
|
|
34 |
} catch (URISyntaxException e) {
|
|
|
35 |
log.error("Error while building solr_url "+e);
|
|
|
36 |
}
|
|
|
37 |
autoSuggestUrl.addParameter("fl", "title,subCategoryId,category_id,category,subCategory,score"); //Fields to choose
|
|
|
38 |
autoSuggestUrl.addParameter("wt", "json"); //Output format
|
| 20217 |
kshitij.so |
39 |
autoSuggestUrl.addParameter("rows", "20"); //search limit for each grouped field or query
|
| 20210 |
kshitij.so |
40 |
}
|
|
|
41 |
|
|
|
42 |
public String getSuggestions(String search_text) throws URISyntaxException, IOException{
|
| 20215 |
kshitij.so |
43 |
autoSuggestUrl.setParameter("q", "suggest:("+search_text+")");
|
| 20210 |
kshitij.so |
44 |
URL url = autoSuggestUrl.build().toURL();
|
|
|
45 |
log.info("Search Url "+url.toString());
|
|
|
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 |
|
|
|
57 |
}
|