| 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{
|
|
|
26 |
synchronized(SolrService.class){
|
|
|
27 |
try {
|
|
|
28 |
SOLR_URL = ConfigClient.getClient().get("dtr_solr_url");
|
|
|
29 |
} catch (ConfigException e) {
|
|
|
30 |
log.error("Error while gettting dtr_solr_url param from config service", e);
|
|
|
31 |
SOLR_URL = "http://localhost:8983/solr/collection1/select";
|
|
|
32 |
}
|
|
|
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("group", "true"); //group data
|
|
|
41 |
autoSuggestUrl.addParameter("group.query", "category_id:3"); //group by category
|
|
|
42 |
autoSuggestUrl.addParameter("group.query", "category_id:5"); //group by category
|
|
|
43 |
autoSuggestUrl.addParameter("group.field", "subCategoryId"); //group by subCategory
|
|
|
44 |
autoSuggestUrl.addParameter("group.limit", "10"); //search limit for each grouped field or query
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public String getSuggestions(String search_text) throws URISyntaxException, IOException{
|
|
|
49 |
autoSuggestUrl.addParameter("q", "suggest:("+search_text+")");
|
|
|
50 |
URL url = autoSuggestUrl.build().toURL();
|
|
|
51 |
log.info("Search Url "+url.toString());
|
|
|
52 |
InputStream is = url.openStream();
|
|
|
53 |
String jsonString;
|
|
|
54 |
try{
|
|
|
55 |
jsonString = IOUtils.toString(is, "UTF-8");
|
|
|
56 |
}
|
|
|
57 |
finally{
|
|
|
58 |
is.close();
|
|
|
59 |
}
|
|
|
60 |
return jsonString;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
}
|