| 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.io.InputStream;
|
|
|
7 |
import java.net.URISyntaxException;
|
|
|
8 |
import java.net.URL;
|
|
|
9 |
|
|
|
10 |
import org.apache.commons.io.IOUtils;
|
|
|
11 |
import org.apache.http.client.utils.URIBuilder;
|
|
|
12 |
import org.apache.log4j.Logger;
|
|
|
13 |
|
|
|
14 |
import com.eclipsesource.json.Json;
|
|
|
15 |
import com.eclipsesource.json.JsonArray;
|
|
|
16 |
import com.eclipsesource.json.JsonObject;
|
|
|
17 |
import com.eclipsesource.json.JsonValue;
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
| 20190 |
kshitij.so |
21 |
public class AutoSuggestController extends BaseController{
|
| 20186 |
kshitij.so |
22 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
23 |
|
|
|
24 |
private static final long serialVersionUID = 1L;
|
| 20209 |
kshitij.so |
25 |
private String search_text;
|
|
|
26 |
private JsonObject result_json;
|
|
|
27 |
private static final int max_count = 10;
|
|
|
28 |
private static final int max_count_accesories = 5;
|
|
|
29 |
private double max_accessory_score, max_mobile_score, max_tablet_score;
|
|
|
30 |
private int mobile_records, tablet_records;
|
|
|
31 |
private SolrService s = new SolrService();
|
| 20193 |
kshitij.so |
32 |
|
| 20186 |
kshitij.so |
33 |
|
|
|
34 |
public String index() throws URISyntaxException, IOException{
|
|
|
35 |
try{
|
| 20213 |
kshitij.so |
36 |
log.info("Calling solr service for search_text "+search_text);
|
|
|
37 |
String jsonString = s.getSuggestions(search_text.trim());
|
|
|
38 |
result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
|
|
|
39 |
setResultJson(sanatizedResults().toString());
|
| 20186 |
kshitij.so |
40 |
}
|
| 20209 |
kshitij.so |
41 |
catch(Exception e){
|
|
|
42 |
e.printStackTrace();
|
|
|
43 |
setResultJson(Json.array().asArray().toString());
|
| 20186 |
kshitij.so |
44 |
}
|
| 20209 |
kshitij.so |
45 |
return "index";
|
|
|
46 |
}
|
| 20193 |
kshitij.so |
47 |
|
| 20209 |
kshitij.so |
48 |
private JsonArray sanatizedResults(){
|
| 20193 |
kshitij.so |
49 |
|
| 20209 |
kshitij.so |
50 |
//Need to re-write this section.This sucks badly
|
| 20193 |
kshitij.so |
51 |
|
| 20209 |
kshitij.so |
52 |
JsonArray output = Json.array().asArray();
|
|
|
53 |
JsonArray temp_mobiles = getResults(3);
|
|
|
54 |
log.info("Temp mobiles "+temp_mobiles.toString());
|
|
|
55 |
JsonArray temp_tablets = getResults(5);
|
|
|
56 |
log.info("Temp tablets "+temp_tablets.toString());
|
|
|
57 |
JsonArray temp_accesories = getResults(6);
|
|
|
58 |
log.info("Temp accessories "+temp_accesories.toString());
|
| 20193 |
kshitij.so |
59 |
|
| 20209 |
kshitij.so |
60 |
if (max_accessory_score > max_mobile_score){
|
|
|
61 |
for (JsonValue j : temp_accesories){
|
|
|
62 |
output.add(j);
|
|
|
63 |
}
|
|
|
64 |
}
|
| 20193 |
kshitij.so |
65 |
|
| 20209 |
kshitij.so |
66 |
int toFetch = temp_accesories.size() == 0 ? 10 : 5;
|
| 20186 |
kshitij.so |
67 |
int count = 0;
|
| 20209 |
kshitij.so |
68 |
for (JsonValue j : temp_mobiles){
|
|
|
69 |
if (count > toFetch)
|
| 20186 |
kshitij.so |
70 |
break;
|
| 20209 |
kshitij.so |
71 |
output.add(j);
|
| 20186 |
kshitij.so |
72 |
count++;
|
|
|
73 |
}
|
|
|
74 |
count = 0;
|
| 20209 |
kshitij.so |
75 |
for (JsonValue j : temp_tablets){
|
|
|
76 |
if (count > toFetch)
|
| 20186 |
kshitij.so |
77 |
break;
|
| 20209 |
kshitij.so |
78 |
output.add(j);
|
| 20186 |
kshitij.so |
79 |
count++;
|
|
|
80 |
}
|
| 20193 |
kshitij.so |
81 |
|
| 20209 |
kshitij.so |
82 |
if (max_accessory_score <= max_mobile_score){
|
|
|
83 |
for (JsonValue j : temp_accesories){
|
|
|
84 |
output.add(j);
|
| 20186 |
kshitij.so |
85 |
}
|
| 20209 |
kshitij.so |
86 |
}
|
| 20213 |
kshitij.so |
87 |
|
| 20209 |
kshitij.so |
88 |
log.info("====================================");
|
|
|
89 |
log.info("Final output "+output);
|
| 20213 |
kshitij.so |
90 |
|
| 20209 |
kshitij.so |
91 |
return output;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private JsonArray getResults(int category_id){
|
|
|
95 |
|
|
|
96 |
JsonArray output = new JsonArray();
|
|
|
97 |
JsonArray suggestion;
|
|
|
98 |
JsonArray subcat_suggestion;
|
|
|
99 |
int count = 0;
|
|
|
100 |
|
|
|
101 |
switch(category_id){
|
|
|
102 |
case 3:
|
|
|
103 |
suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
104 |
mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
|
|
105 |
try{
|
|
|
106 |
max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
|
|
107 |
}
|
|
|
108 |
catch(Exception e){
|
|
|
109 |
max_mobile_score = 0.0;
|
|
|
110 |
}
|
| 20186 |
kshitij.so |
111 |
for (JsonValue item : suggestion) {
|
| 20209 |
kshitij.so |
112 |
if (count == max_count){
|
| 20186 |
kshitij.so |
113 |
break;
|
|
|
114 |
}
|
| 20209 |
kshitij.so |
115 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().get("category").asString());
|
|
|
116 |
output.add(temp);
|
| 20186 |
kshitij.so |
117 |
count++;
|
|
|
118 |
}
|
| 20209 |
kshitij.so |
119 |
case 5:
|
|
|
120 |
suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
121 |
tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
|
|
|
122 |
try{
|
|
|
123 |
max_tablet_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
|
|
124 |
}
|
|
|
125 |
catch(Exception e){
|
|
|
126 |
max_tablet_score = 0.0;
|
|
|
127 |
}
|
|
|
128 |
for (JsonValue item : suggestion) {
|
|
|
129 |
if (count == max_count){
|
|
|
130 |
break;
|
|
|
131 |
}
|
|
|
132 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().get("category").asString());
|
|
|
133 |
output.add(temp);
|
|
|
134 |
count++;
|
|
|
135 |
}
|
|
|
136 |
case 6:
|
|
|
137 |
subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
|
|
|
138 |
max_accessory_score = 0.0;
|
|
|
139 |
for (JsonValue itemList : subcat_suggestion) {
|
|
|
140 |
if (itemList.asObject().get("groupValue").asInt()==0){
|
|
|
141 |
continue;
|
|
|
142 |
}
|
|
|
143 |
suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
144 |
try{
|
|
|
145 |
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;
|
|
|
146 |
}
|
|
|
147 |
catch(Exception e){
|
|
|
148 |
;
|
|
|
149 |
}
|
|
|
150 |
count = 0;
|
|
|
151 |
for (JsonValue item : suggestion) {
|
|
|
152 |
if (count == max_count_accesories){
|
|
|
153 |
break;
|
|
|
154 |
}
|
|
|
155 |
JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().getString("subCategory", "Accessories"));
|
|
|
156 |
output.add(temp);
|
|
|
157 |
count++;
|
|
|
158 |
}
|
|
|
159 |
}
|
| 20186 |
kshitij.so |
160 |
}
|
| 20209 |
kshitij.so |
161 |
return output;
|
| 20186 |
kshitij.so |
162 |
}
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
public void setSearch_text(String search_text) {
|
| 20213 |
kshitij.so |
166 |
log.info("Search text "+search_text);
|
| 20186 |
kshitij.so |
167 |
this.search_text = search_text;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
public String getSearch_text() {
|
|
|
172 |
return search_text;
|
|
|
173 |
}
|
| 20193 |
kshitij.so |
174 |
|
| 20209 |
kshitij.so |
175 |
public void setResult_json(JsonObject result_json) {
|
|
|
176 |
this.result_json = result_json;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public JsonObject getResult_json() {
|
|
|
180 |
return result_json;
|
|
|
181 |
}
|
|
|
182 |
|
| 20186 |
kshitij.so |
183 |
public static void main(String[] args) throws URISyntaxException, IOException{
|
| 20191 |
kshitij.so |
184 |
AutoSuggestController a = new AutoSuggestController();
|
| 20209 |
kshitij.so |
185 |
a.setSearch_text("iphone tempered");
|
| 20186 |
kshitij.so |
186 |
a.index();
|
|
|
187 |
System.out.println(a.getResultJson());
|
|
|
188 |
}
|
| 20193 |
kshitij.so |
189 |
|
| 20186 |
kshitij.so |
190 |
}
|