| 20186 |
kshitij.so |
1 |
package in.shop2020.dtrapi.controllers;
|
|
|
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;
|
|
|
11 |
import org.apache.log4j.Logger;
|
|
|
12 |
|
|
|
13 |
import com.eclipsesource.json.Json;
|
|
|
14 |
import com.eclipsesource.json.JsonArray;
|
|
|
15 |
import com.eclipsesource.json.JsonObject;
|
|
|
16 |
import com.eclipsesource.json.JsonValue;
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
| 20190 |
kshitij.so |
20 |
public class AutoSuggestController extends BaseController{
|
| 20186 |
kshitij.so |
21 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
22 |
|
|
|
23 |
private static final long serialVersionUID = 1L;
|
| 20193 |
kshitij.so |
24 |
|
| 20186 |
kshitij.so |
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;
|
|
|
28 |
|
|
|
29 |
public String index() throws URISyntaxException, IOException{
|
|
|
30 |
JsonArray outputJson = Json.array().asArray();
|
| 20187 |
kshitij.so |
31 |
URIBuilder b = new URIBuilder("http://192.168.194.109:8983/solr/collection1/select");
|
| 20194 |
kshitij.so |
32 |
String[] search_terms_array = search_text.split("\\s+");
|
| 20193 |
kshitij.so |
33 |
//iphone AND apple AND 5s
|
|
|
34 |
|
|
|
35 |
StringBuilder sb = new StringBuilder();
|
|
|
36 |
|
| 20194 |
kshitij.so |
37 |
int c = 1;
|
| 20193 |
kshitij.so |
38 |
for (String s : search_terms_array){
|
| 20194 |
kshitij.so |
39 |
|
| 20193 |
kshitij.so |
40 |
if (c==search_terms_array.length){
|
| 20196 |
kshitij.so |
41 |
sb.append("*"+s.trim()+"*");
|
| 20193 |
kshitij.so |
42 |
}
|
|
|
43 |
else{
|
| 20195 |
kshitij.so |
44 |
sb.append("*"+s.trim()+"*"+" AND ");
|
| 20193 |
kshitij.so |
45 |
}
|
|
|
46 |
c++;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
|
| 20194 |
kshitij.so |
50 |
b.addParameter("q", sb.toString());
|
| 20186 |
kshitij.so |
51 |
b.addParameter("fl", "title,subCategoryId,category_id,category,subCategory");
|
|
|
52 |
b.addParameter("wt", "json");
|
|
|
53 |
b.addParameter("group", "true");
|
|
|
54 |
b.addParameter("group.query", "category_id:3");
|
|
|
55 |
b.addParameter("group.query", "category_id:5");
|
|
|
56 |
b.addParameter("group.field", "subCategoryId");
|
|
|
57 |
b.addParameter("group.limit", "10");
|
| 20193 |
kshitij.so |
58 |
|
|
|
59 |
|
| 20186 |
kshitij.so |
60 |
URL url = b.build().toURL();
|
| 20193 |
kshitij.so |
61 |
|
| 20192 |
kshitij.so |
62 |
System.out.println(url.toString());
|
| 20186 |
kshitij.so |
63 |
InputStream is = url.openStream();
|
|
|
64 |
String jsonString;
|
|
|
65 |
try{
|
|
|
66 |
jsonString = IOUtils.toString(is, "UTF-8");
|
|
|
67 |
}
|
|
|
68 |
finally{
|
|
|
69 |
is.close();
|
|
|
70 |
}
|
|
|
71 |
JsonObject value = Json.parse(jsonString).asObject().get("grouped").asObject();
|
| 20193 |
kshitij.so |
72 |
|
| 20186 |
kshitij.so |
73 |
//Lets check mobile segment
|
| 20193 |
kshitij.so |
74 |
|
| 20186 |
kshitij.so |
75 |
int m = value.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
| 20193 |
kshitij.so |
76 |
|
| 20186 |
kshitij.so |
77 |
int toFetch = Math.min(m, 5);
|
| 20193 |
kshitij.so |
78 |
|
| 20186 |
kshitij.so |
79 |
JsonArray mobile_suggestion = value.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
|
| 20193 |
kshitij.so |
80 |
|
| 20186 |
kshitij.so |
81 |
int count = 0;
|
|
|
82 |
for (JsonValue item : mobile_suggestion) {
|
|
|
83 |
if (count == toFetch){
|
|
|
84 |
break;
|
|
|
85 |
}
|
| 20188 |
kshitij.so |
86 |
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());
|
| 20186 |
kshitij.so |
87 |
outputJson.add(temp);
|
|
|
88 |
count++;
|
|
|
89 |
}
|
| 20193 |
kshitij.so |
90 |
|
| 20186 |
kshitij.so |
91 |
m = value.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
|
| 20193 |
kshitij.so |
92 |
|
| 20186 |
kshitij.so |
93 |
toFetch = Math.min(m, 5);
|
| 20193 |
kshitij.so |
94 |
|
| 20186 |
kshitij.so |
95 |
JsonArray tablet_suggestion = value.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
|
| 20193 |
kshitij.so |
96 |
|
| 20186 |
kshitij.so |
97 |
count = 0;
|
|
|
98 |
for (JsonValue item : tablet_suggestion) {
|
|
|
99 |
if (count == toFetch){
|
|
|
100 |
break;
|
|
|
101 |
}
|
| 20188 |
kshitij.so |
102 |
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());
|
| 20186 |
kshitij.so |
103 |
outputJson.add(temp);
|
|
|
104 |
count++;
|
|
|
105 |
}
|
| 20193 |
kshitij.so |
106 |
|
| 20186 |
kshitij.so |
107 |
JsonArray subcat_suggestion = value.get("subCategoryId").asObject().get("groups").asArray();
|
|
|
108 |
for (JsonValue itemList : subcat_suggestion) {
|
|
|
109 |
if (itemList.asObject().get("groupValue").asInt()==0){
|
|
|
110 |
continue;
|
|
|
111 |
}
|
|
|
112 |
m = itemList.asObject().get("doclist").asObject().get("numFound").asInt();
|
|
|
113 |
toFetch = Math.min(m, 5);
|
|
|
114 |
JsonArray suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
|
|
|
115 |
count = 0;
|
|
|
116 |
for (JsonValue item : suggestion) {
|
|
|
117 |
if (count == toFetch){
|
|
|
118 |
break;
|
|
|
119 |
}
|
| 20188 |
kshitij.so |
120 |
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"));
|
| 20186 |
kshitij.so |
121 |
outputJson.add(temp);
|
|
|
122 |
count++;
|
|
|
123 |
}
|
| 20193 |
kshitij.so |
124 |
|
| 20186 |
kshitij.so |
125 |
}
|
| 20193 |
kshitij.so |
126 |
|
| 20186 |
kshitij.so |
127 |
setResultJson(outputJson.toString());
|
|
|
128 |
return "index";
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
public void setSearch_text(String search_text) {
|
|
|
133 |
this.search_text = search_text;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
public String getSearch_text() {
|
|
|
138 |
return search_text;
|
|
|
139 |
}
|
| 20193 |
kshitij.so |
140 |
|
| 20186 |
kshitij.so |
141 |
public static void main(String[] args) throws URISyntaxException, IOException{
|
| 20191 |
kshitij.so |
142 |
AutoSuggestController a = new AutoSuggestController();
|
| 20194 |
kshitij.so |
143 |
a.setSearch_text("iphone");
|
| 20186 |
kshitij.so |
144 |
a.index();
|
|
|
145 |
System.out.println(a.getResultJson());
|
|
|
146 |
}
|
| 20193 |
kshitij.so |
147 |
|
| 20186 |
kshitij.so |
148 |
}
|