| Line 1... |
Line 1... |
| 1 |
package in.shop2020.dtrapi.controllers;
|
1 |
package in.shop2020.dtrapi.controllers;
|
| 2 |
|
2 |
|
| 3 |
|
3 |
|
| - |
|
4 |
import in.shop2020.dtrapi.services.SolrService;
|
| 4 |
import java.io.IOException;
|
5 |
import java.io.IOException;
|
| 5 |
import java.io.InputStream;
|
6 |
import java.io.InputStream;
|
| 6 |
import java.net.URISyntaxException;
|
7 |
import java.net.URISyntaxException;
|
| 7 |
import java.net.URL;
|
8 |
import java.net.URL;
|
| 8 |
|
9 |
|
| Line 19... |
Line 20... |
| 19 |
|
20 |
|
| 20 |
public class AutoSuggestController extends BaseController{
|
21 |
public class AutoSuggestController extends BaseController{
|
| 21 |
private static Logger log = Logger.getLogger(Class.class);
|
22 |
private static Logger log = Logger.getLogger(Class.class);
|
| 22 |
|
23 |
|
| 23 |
private static final long serialVersionUID = 1L;
|
24 |
private static final long serialVersionUID = 1L;
|
| 24 |
|
- |
|
| 25 |
//:8983/solr/collection1/select?q=*apple%20iphone%205s*&fl=title,subCategoryId,category_id&wt=json&group=true&group.query=category_id:3&group.query=category_id:5&group.field=subCategoryId&group.limit=10
|
- |
|
| 26 |
|
- |
|
| 27 |
private String search_text;
|
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();
|
| - |
|
32 |
|
| 28 |
|
33 |
|
| 29 |
public String index() throws URISyntaxException, IOException{
|
34 |
public String index() throws URISyntaxException, IOException{
|
| 30 |
JsonArray outputJson = Json.array().asArray();
|
- |
|
| 31 |
URIBuilder b = new URIBuilder("http://192.168.194.109:8983/solr/collection1/select");
|
- |
|
| 32 |
//suggest:(apple iphone 5s silver)
|
- |
|
| 33 |
b.addParameter("q", "suggest:("+search_text+")");
|
- |
|
| 34 |
b.addParameter("fl", "title,subCategoryId,category_id,category,subCategory");
|
- |
|
| 35 |
b.addParameter("wt", "json");
|
- |
|
| 36 |
b.addParameter("group", "true");
|
- |
|
| 37 |
b.addParameter("group.query", "category_id:3");
|
- |
|
| 38 |
b.addParameter("group.query", "category_id:5");
|
- |
|
| 39 |
b.addParameter("group.field", "subCategoryId");
|
- |
|
| 40 |
b.addParameter("group.limit", "10");
|
- |
|
| 41 |
|
- |
|
| 42 |
|
- |
|
| 43 |
URL url = b.build().toURL();
|
- |
|
| 44 |
|
- |
|
| 45 |
System.out.println(url.toString());
|
- |
|
| 46 |
InputStream is = url.openStream();
|
- |
|
| 47 |
String jsonString;
|
- |
|
| 48 |
try{
|
35 |
try{
|
| 49 |
jsonString = IOUtils.toString(is, "UTF-8");
|
36 |
String jsonString = s.getSuggestions(search_text.trim());
|
| - |
|
37 |
result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
|
| - |
|
38 |
setResultJson(sanatizedResults().toString());
|
| 50 |
}
|
39 |
}
|
| 51 |
finally{
|
40 |
catch(Exception e){
|
| 52 |
is.close();
|
41 |
e.printStackTrace();
|
| - |
|
42 |
setResultJson(Json.array().asArray().toString());
|
| 53 |
}
|
43 |
}
|
| 54 |
JsonObject value = Json.parse(jsonString).asObject().get("grouped").asObject();
|
44 |
return "index";
|
| 55 |
|
45 |
}
|
| 56 |
//Lets check mobile segment
|
- |
|
| 57 |
|
46 |
|
| 58 |
int m = value.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
47 |
private JsonArray sanatizedResults(){
|
| 59 |
|
48 |
|
| 60 |
int toFetch = Math.min(m, 5);
|
49 |
//Need to re-write this section.This sucks badly
|
| 61 |
|
50 |
|
| 62 |
JsonArray mobile_suggestion = value.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
|
51 |
JsonArray output = Json.array().asArray();
|
| - |
|
52 |
JsonArray temp_mobiles = getResults(3);
|
| - |
|
53 |
log.info("Temp mobiles "+temp_mobiles.toString());
|
| - |
|
54 |
JsonArray temp_tablets = getResults(5);
|
| - |
|
55 |
log.info("Temp tablets "+temp_tablets.toString());
|
| - |
|
56 |
JsonArray temp_accesories = getResults(6);
|
| - |
|
57 |
log.info("Temp accessories "+temp_accesories.toString());
|
| - |
|
58 |
|
| - |
|
59 |
if (max_accessory_score > max_mobile_score){
|
| - |
|
60 |
for (JsonValue j : temp_accesories){
|
| - |
|
61 |
output.add(j);
|
| - |
|
62 |
}
|
| - |
|
63 |
}
|
| 63 |
|
64 |
|
| - |
|
65 |
int toFetch = temp_accesories.size() == 0 ? 10 : 5;
|
| 64 |
int count = 0;
|
66 |
int count = 0;
|
| 65 |
for (JsonValue item : mobile_suggestion) {
|
67 |
for (JsonValue j : temp_mobiles){
|
| 66 |
if (count == toFetch){
|
68 |
if (count > toFetch)
|
| 67 |
break;
|
69 |
break;
|
| 68 |
}
|
- |
|
| 69 |
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());
|
70 |
output.add(j);
|
| 70 |
outputJson.add(temp);
|
- |
|
| 71 |
count++;
|
71 |
count++;
|
| 72 |
}
|
72 |
}
|
| 73 |
|
- |
|
| 74 |
m = value.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
|
- |
|
| 75 |
|
- |
|
| 76 |
toFetch = Math.min(m, 5);
|
- |
|
| 77 |
|
- |
|
| 78 |
JsonArray tablet_suggestion = value.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
|
- |
|
| 79 |
|
- |
|
| 80 |
count = 0;
|
73 |
count = 0;
|
| 81 |
for (JsonValue item : tablet_suggestion) {
|
74 |
for (JsonValue j : temp_tablets){
|
| 82 |
if (count == toFetch){
|
75 |
if (count > toFetch)
|
| 83 |
break;
|
76 |
break;
|
| 84 |
}
|
- |
|
| 85 |
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());
|
77 |
output.add(j);
|
| 86 |
outputJson.add(temp);
|
- |
|
| 87 |
count++;
|
78 |
count++;
|
| 88 |
}
|
79 |
}
|
| 89 |
|
80 |
|
| 90 |
JsonArray subcat_suggestion = value.get("subCategoryId").asObject().get("groups").asArray();
|
81 |
if (max_accessory_score <= max_mobile_score){
|
| 91 |
for (JsonValue itemList : subcat_suggestion) {
|
82 |
for (JsonValue j : temp_accesories){
|
| - |
|
83 |
output.add(j);
|
| - |
|
84 |
}
|
| - |
|
85 |
}
|
| - |
|
86 |
|
| 92 |
if (itemList.asObject().get("groupValue").asInt()==0){
|
87 |
log.info("====================================");
|
| - |
|
88 |
log.info("Final output "+output);
|
| - |
|
89 |
|
| 93 |
continue;
|
90 |
return output;
|
| 94 |
}
|
91 |
}
|
| - |
|
92 |
|
| 95 |
m = itemList.asObject().get("doclist").asObject().get("numFound").asInt();
|
93 |
private JsonArray getResults(int category_id){
|
| - |
|
94 |
|
| - |
|
95 |
JsonArray output = new JsonArray();
|
| - |
|
96 |
JsonArray suggestion;
|
| - |
|
97 |
JsonArray subcat_suggestion;
|
| - |
|
98 |
int count = 0;
|
| - |
|
99 |
|
| 96 |
toFetch = Math.min(m, 5);
|
100 |
switch(category_id){
|
| - |
|
101 |
case 3:
|
| 97 |
JsonArray suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
|
102 |
suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
|
| - |
|
103 |
mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
| 98 |
count = 0;
|
104 |
try{
|
| - |
|
105 |
max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
| - |
|
106 |
}
|
| - |
|
107 |
catch(Exception e){
|
| - |
|
108 |
max_mobile_score = 0.0;
|
| - |
|
109 |
}
|
| 99 |
for (JsonValue item : suggestion) {
|
110 |
for (JsonValue item : suggestion) {
|
| 100 |
if (count == toFetch){
|
111 |
if (count == max_count){
|
| 101 |
break;
|
112 |
break;
|
| 102 |
}
|
113 |
}
|
| 103 |
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"));
|
114 |
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());
|
| 104 |
outputJson.add(temp);
|
115 |
output.add(temp);
|
| 105 |
count++;
|
116 |
count++;
|
| 106 |
}
|
117 |
}
|
| - |
|
118 |
case 5:
|
| - |
|
119 |
suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
|
| - |
|
120 |
tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
|
| - |
|
121 |
try{
|
| - |
|
122 |
max_tablet_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
|
| - |
|
123 |
}
|
| - |
|
124 |
catch(Exception e){
|
| - |
|
125 |
max_tablet_score = 0.0;
|
| - |
|
126 |
}
|
| - |
|
127 |
for (JsonValue item : suggestion) {
|
| - |
|
128 |
if (count == max_count){
|
| - |
|
129 |
break;
|
| - |
|
130 |
}
|
| 107 |
|
131 |
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());
|
| - |
|
132 |
output.add(temp);
|
| - |
|
133 |
count++;
|
| - |
|
134 |
}
|
| - |
|
135 |
case 6:
|
| - |
|
136 |
subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
|
| - |
|
137 |
max_accessory_score = 0.0;
|
| - |
|
138 |
for (JsonValue itemList : subcat_suggestion) {
|
| - |
|
139 |
if (itemList.asObject().get("groupValue").asInt()==0){
|
| - |
|
140 |
continue;
|
| - |
|
141 |
}
|
| - |
|
142 |
suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
|
| - |
|
143 |
try{
|
| - |
|
144 |
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;
|
| - |
|
145 |
}
|
| - |
|
146 |
catch(Exception e){
|
| - |
|
147 |
;
|
| - |
|
148 |
}
|
| - |
|
149 |
count = 0;
|
| - |
|
150 |
for (JsonValue item : suggestion) {
|
| - |
|
151 |
if (count == max_count_accesories){
|
| - |
|
152 |
break;
|
| - |
|
153 |
}
|
| - |
|
154 |
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"));
|
| - |
|
155 |
output.add(temp);
|
| - |
|
156 |
count++;
|
| - |
|
157 |
}
|
| - |
|
158 |
}
|
| 108 |
}
|
159 |
}
|
| 109 |
|
- |
|
| 110 |
setResultJson(outputJson.toString());
|
- |
|
| 111 |
return "index";
|
160 |
return output;
|
| 112 |
}
|
161 |
}
|
| 113 |
|
162 |
|
| 114 |
|
163 |
|
| 115 |
public void setSearch_text(String search_text) {
|
164 |
public void setSearch_text(String search_text) {
|
| 116 |
this.search_text = search_text;
|
165 |
this.search_text = search_text;
|
| Line 119... |
Line 168... |
| 119 |
|
168 |
|
| 120 |
public String getSearch_text() {
|
169 |
public String getSearch_text() {
|
| 121 |
return search_text;
|
170 |
return search_text;
|
| 122 |
}
|
171 |
}
|
| 123 |
|
172 |
|
| - |
|
173 |
public void setResult_json(JsonObject result_json) {
|
| - |
|
174 |
this.result_json = result_json;
|
| - |
|
175 |
}
|
| - |
|
176 |
|
| - |
|
177 |
public JsonObject getResult_json() {
|
| - |
|
178 |
return result_json;
|
| - |
|
179 |
}
|
| - |
|
180 |
|
| 124 |
public static void main(String[] args) throws URISyntaxException, IOException{
|
181 |
public static void main(String[] args) throws URISyntaxException, IOException{
|
| 125 |
AutoSuggestController a = new AutoSuggestController();
|
182 |
AutoSuggestController a = new AutoSuggestController();
|
| 126 |
a.setSearch_text("iphone");
|
183 |
a.setSearch_text("iphone tempered");
|
| 127 |
a.index();
|
184 |
a.index();
|
| 128 |
System.out.println(a.getResultJson());
|
185 |
System.out.println(a.getResultJson());
|
| 129 |
}
|
186 |
}
|
| 130 |
|
187 |
|
| 131 |
}
|
188 |
}
|