| 20186 |
kshitij.so |
1 |
package in.shop2020.dtrapi.controllers;
|
|
|
2 |
|
|
|
3 |
|
| 20209 |
kshitij.so |
4 |
import in.shop2020.dtrapi.services.SolrService;
|
| 20186 |
kshitij.so |
5 |
import java.io.IOException;
|
|
|
6 |
import java.net.URISyntaxException;
|
|
|
7 |
|
|
|
8 |
import org.apache.log4j.Logger;
|
|
|
9 |
|
|
|
10 |
import com.eclipsesource.json.Json;
|
|
|
11 |
import com.eclipsesource.json.JsonArray;
|
| 20259 |
kshitij.so |
12 |
import com.eclipsesource.json.JsonObject;
|
| 20231 |
kshitij.so |
13 |
import com.eclipsesource.json.JsonValue;
|
| 20259 |
kshitij.so |
14 |
import java.io.InputStream;
|
|
|
15 |
import java.net.URL;
|
| 20186 |
kshitij.so |
16 |
|
| 20259 |
kshitij.so |
17 |
import org.apache.commons.io.IOUtils;
|
|
|
18 |
import org.apache.http.client.utils.URIBuilder;
|
| 20186 |
kshitij.so |
19 |
|
|
|
20 |
|
| 20259 |
kshitij.so |
21 |
|
| 20229 |
kshitij.so |
22 |
public class SolrSearchController extends BaseController{
|
| 20186 |
kshitij.so |
23 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
24 |
|
|
|
25 |
private static final long serialVersionUID = 1L;
|
| 20209 |
kshitij.so |
26 |
private String search_text;
|
| 20220 |
kshitij.so |
27 |
private String offset;
|
| 20259 |
kshitij.so |
28 |
private static final int max_count = 10;
|
| 20263 |
kshitij.so |
29 |
private static final int max_count_accesories = 5;
|
|
|
30 |
private double max_accessory_score, max_mobile_score, max_tablet_score;
|
|
|
31 |
private int mobile_records, tablet_records;
|
|
|
32 |
private JsonObject result_json;
|
| 20259 |
kshitij.so |
33 |
|
|
|
34 |
|
| 20209 |
kshitij.so |
35 |
private SolrService s = new SolrService();
|
| 20193 |
kshitij.so |
36 |
|
| 20186 |
kshitij.so |
37 |
|
| 20229 |
kshitij.so |
38 |
public String getSuggestions() throws URISyntaxException, IOException{
|
| 20186 |
kshitij.so |
39 |
try{
|
| 20229 |
kshitij.so |
40 |
log.info("Calling solr service (auto suggest) for search_text "+search_text);
|
|
|
41 |
String jsonString = s.getSuggestions(search_text.trim());
|
| 20261 |
kshitij.so |
42 |
result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
|
| 20263 |
kshitij.so |
43 |
setResultJson(sanatizedResults().toString());
|
| 20186 |
kshitij.so |
44 |
}
|
| 20209 |
kshitij.so |
45 |
catch(Exception e){
|
|
|
46 |
e.printStackTrace();
|
|
|
47 |
setResultJson(Json.array().asArray().toString());
|
| 20186 |
kshitij.so |
48 |
}
|
| 20209 |
kshitij.so |
49 |
return "index";
|
|
|
50 |
}
|
| 20259 |
kshitij.so |
51 |
|
|
|
52 |
private JsonArray sanatizedResults(){
|
|
|
53 |
|
|
|
54 |
//Need to re-write this section.This sucks badly
|
|
|
55 |
|
|
|
56 |
JsonArray output = Json.array().asArray();
|
|
|
57 |
JsonArray temp_mobiles = getResults(3);
|
|
|
58 |
log.info("Temp mobiles "+temp_mobiles.toString());
|
|
|
59 |
JsonArray temp_tablets = getResults(5);
|
|
|
60 |
log.info("Temp tablets "+temp_tablets.toString());
|
|
|
61 |
JsonArray temp_accesories = getResults(6);
|
|
|
62 |
log.info("Temp accessories "+temp_accesories.toString());
|
|
|
63 |
|
|
|
64 |
if (max_accessory_score > max_mobile_score){
|
|
|
65 |
for (JsonValue j : temp_accesories){
|
|
|
66 |
output.add(j);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
| 20264 |
kshitij.so |
70 |
int toFetch = temp_accesories.size() == 0 ? 5 : 3;
|
| 20263 |
kshitij.so |
71 |
int count = 1;
|
|
|
72 |
|
|
|
73 |
if (max_mobile_score > max_tablet_score){
|
|
|
74 |
|
|
|
75 |
for (JsonValue j : temp_mobiles){
|
|
|
76 |
if (count > toFetch)
|
|
|
77 |
break;
|
|
|
78 |
output.add(j);
|
|
|
79 |
count++;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
count = 1;
|
|
|
83 |
|
|
|
84 |
for (JsonValue j : temp_tablets){
|
|
|
85 |
if (count > toFetch)
|
|
|
86 |
break;
|
|
|
87 |
output.add(j);
|
|
|
88 |
count++;
|
|
|
89 |
}
|
| 20259 |
kshitij.so |
90 |
}
|
| 20263 |
kshitij.so |
91 |
|
|
|
92 |
else{
|
|
|
93 |
for (JsonValue j : temp_tablets){
|
|
|
94 |
if (count > toFetch)
|
|
|
95 |
break;
|
|
|
96 |
output.add(j);
|
|
|
97 |
count++;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
count = 1;
|
|
|
101 |
|
|
|
102 |
for (JsonValue j : temp_mobiles){
|
|
|
103 |
if (count > toFetch)
|
|
|
104 |
break;
|
|
|
105 |
output.add(j);
|
|
|
106 |
count++;
|
|
|
107 |
}
|
| 20259 |
kshitij.so |
108 |
}
|
|
|
109 |
|
|
|
110 |
if (max_accessory_score <= max_mobile_score){
|
|
|
111 |
for (JsonValue j : temp_accesories){
|
|
|
112 |
output.add(j);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
log.info("====================================");
|
|
|
117 |
log.info("Final output "+output);
|
|
|
118 |
|
|
|
119 |
return output;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
private JsonArray getResults(int category_id){
|
|
|
123 |
|
|
|
124 |
JsonArray output = new JsonArray();
|
|
|
125 |
JsonArray suggestion;
|
|
|
126 |
JsonArray subcat_suggestion;
|
|
|
127 |
int count = 0;
|
|
|
128 |
|
|
|
129 |
switch(category_id){
|
|
|
130 |
case 3:
|
|
|
131 |
suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
132 |
mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
|
|
133 |
try{
|
|
|
134 |
max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
|
|
135 |
}
|
|
|
136 |
catch(Exception e){
|
|
|
137 |
max_mobile_score = 0.0;
|
|
|
138 |
}
|
|
|
139 |
for (JsonValue item : suggestion) {
|
|
|
140 |
if (count == max_count){
|
|
|
141 |
break;
|
|
|
142 |
}
|
| 20266 |
kshitij.so |
143 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().get("category").asString());
|
| 20259 |
kshitij.so |
144 |
output.add(temp);
|
|
|
145 |
count++;
|
|
|
146 |
}
|
|
|
147 |
case 5:
|
|
|
148 |
suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
149 |
tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
|
|
|
150 |
try{
|
|
|
151 |
max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
|
|
152 |
}
|
|
|
153 |
catch(Exception e){
|
|
|
154 |
max_tablet_score = 0.0;
|
|
|
155 |
}
|
|
|
156 |
for (JsonValue item : suggestion) {
|
|
|
157 |
if (count == max_count){
|
|
|
158 |
break;
|
|
|
159 |
}
|
| 20266 |
kshitij.so |
160 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().get("category").asString());
|
| 20259 |
kshitij.so |
161 |
output.add(temp);
|
|
|
162 |
count++;
|
|
|
163 |
}
|
|
|
164 |
case 6:
|
|
|
165 |
subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
|
|
|
166 |
max_accessory_score = 0.0;
|
|
|
167 |
for (JsonValue itemList : subcat_suggestion) {
|
|
|
168 |
if (itemList.asObject().get("groupValue").asInt()==0){
|
|
|
169 |
continue;
|
|
|
170 |
}
|
|
|
171 |
suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
172 |
try{
|
|
|
173 |
max_accessory_score = max_accessory_score < itemList.asObject().get("doclist").asObject().get("maxScore").asDouble() ? itemList.asObject().get("doclist").asObject().get("maxScore").asDouble() : max_accessory_score;
|
|
|
174 |
}
|
|
|
175 |
catch(Exception e){
|
|
|
176 |
;
|
|
|
177 |
}
|
|
|
178 |
count = 0;
|
|
|
179 |
for (JsonValue item : suggestion) {
|
|
|
180 |
if (count == max_count_accesories){
|
|
|
181 |
break;
|
|
|
182 |
}
|
| 20266 |
kshitij.so |
183 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().getString("subCategory", "Accessories"));
|
| 20259 |
kshitij.so |
184 |
output.add(temp);
|
|
|
185 |
count++;
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
return output;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
|
| 20229 |
kshitij.so |
193 |
public String getSearchResults(){
|
|
|
194 |
try{
|
|
|
195 |
log.info("Calling solr service (search results) for search_text "+search_text);
|
|
|
196 |
String jsonString = s.getSearchResults(search_text.trim(), offset);
|
|
|
197 |
JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
|
| 20231 |
kshitij.so |
198 |
for (JsonValue j : result_json ){
|
| 20277 |
kshitij.so |
199 |
j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id"));
|
| 20231 |
kshitij.so |
200 |
}
|
| 20229 |
kshitij.so |
201 |
setResultJson(result_json.toString());
|
|
|
202 |
}
|
|
|
203 |
catch(Exception e){
|
|
|
204 |
e.printStackTrace();
|
|
|
205 |
setResultJson(Json.array().asArray().toString());
|
|
|
206 |
}
|
|
|
207 |
return "index";
|
|
|
208 |
}
|
| 20193 |
kshitij.so |
209 |
|
|
|
210 |
|
| 20186 |
kshitij.so |
211 |
public void setSearch_text(String search_text) {
|
| 20213 |
kshitij.so |
212 |
log.info("Search text "+search_text);
|
| 20186 |
kshitij.so |
213 |
this.search_text = search_text;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
public String getSearch_text() {
|
|
|
218 |
return search_text;
|
|
|
219 |
}
|
| 20193 |
kshitij.so |
220 |
|
| 20209 |
kshitij.so |
221 |
|
| 20186 |
kshitij.so |
222 |
public static void main(String[] args) throws URISyntaxException, IOException{
|
| 20259 |
kshitij.so |
223 |
SolrSearchController a = new SolrSearchController();
|
| 20209 |
kshitij.so |
224 |
a.setSearch_text("iphone tempered");
|
| 20277 |
kshitij.so |
225 |
a.setOffset("10");
|
|
|
226 |
a.getSearchResults();
|
| 20186 |
kshitij.so |
227 |
System.out.println(a.getResultJson());
|
|
|
228 |
}
|
| 20193 |
kshitij.so |
229 |
|
| 20220 |
kshitij.so |
230 |
|
|
|
231 |
public void setOffset(String offset) {
|
|
|
232 |
this.offset = offset;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
public String getOffset() {
|
|
|
237 |
return offset;
|
|
|
238 |
}
|
|
|
239 |
|
| 20186 |
kshitij.so |
240 |
}
|