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