Subversion Repositories SmartDukaan

Rev

Rev 20213 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20213 Rev 20216
Line 21... Line 21...
21
public class AutoSuggestController extends BaseController{
21
public class AutoSuggestController extends BaseController{
22
	private static Logger log = Logger.getLogger(Class.class);
22
	private static Logger log = Logger.getLogger(Class.class);
23
 
23
 
24
	private static final long serialVersionUID = 1L;
24
	private static final long serialVersionUID = 1L;
25
	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();
26
	private SolrService s =  new SolrService();
32
 
27
 
33
 
28
 
34
	public String index() throws URISyntaxException, IOException{
29
	public String index() throws URISyntaxException, IOException{
35
		try{
30
		try{
36
			log.info("Calling solr service for search_text "+search_text);
31
			log.info("Calling solr service for search_text "+search_text);
37
			String jsonString = s.getSuggestions(search_text.trim());
32
			String jsonString = s.getSuggestions(search_text.trim());
38
			result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
33
			JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("Docs").asArray();
39
			setResultJson(sanatizedResults().toString());
34
			setResultJson(result_json.toString());
40
		}
35
		}
41
		catch(Exception e){
36
		catch(Exception e){
42
			e.printStackTrace();
37
			e.printStackTrace();
43
			setResultJson(Json.array().asArray().toString());
38
			setResultJson(Json.array().asArray().toString());
44
		}
39
		}
45
		return "index";
40
		return "index";
46
	}
41
	}
47
 
42
 
48
	private JsonArray sanatizedResults(){
-
 
49
 
-
 
50
		//Need to re-write this section.This sucks badly
-
 
51
 
-
 
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());
-
 
59
 
-
 
60
		if (max_accessory_score > max_mobile_score){
-
 
61
			for (JsonValue j : temp_accesories){
-
 
62
				output.add(j);
-
 
63
			}
-
 
64
		}
-
 
65
 
-
 
66
		int toFetch = temp_accesories.size() == 0 ? 10 : 5;
-
 
67
		int count = 0;
-
 
68
		for (JsonValue j : temp_mobiles){
-
 
69
			if (count > toFetch)
-
 
70
				break;
-
 
71
			output.add(j);
-
 
72
			count++;
-
 
73
		}
-
 
74
		count = 0;
-
 
75
		for (JsonValue j : temp_tablets){
-
 
76
			if (count > toFetch)
-
 
77
				break;
-
 
78
			output.add(j);
-
 
79
			count++;
-
 
80
		}
-
 
81
 
-
 
82
		if (max_accessory_score <= max_mobile_score){
-
 
83
			for (JsonValue j : temp_accesories){
-
 
84
				output.add(j);
-
 
85
			}
-
 
86
		}
-
 
87
 
-
 
88
		log.info("====================================");
-
 
89
		log.info("Final output "+output);
-
 
90
 
-
 
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
			}
-
 
111
			for (JsonValue item : suggestion) {
-
 
112
				if (count == max_count){
-
 
113
					break;
-
 
114
				}
-
 
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);
-
 
117
				count++;
-
 
118
			}
-
 
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
			}
-
 
160
		}
-
 
161
		return output;
-
 
162
	}
-
 
163
 
-
 
164
 
43
 
165
	public void setSearch_text(String search_text) {
44
	public void setSearch_text(String search_text) {
166
		log.info("Search text "+search_text);
45
		log.info("Search text "+search_text);
167
		this.search_text = search_text;
46
		this.search_text = search_text;
168
	}
47
	}
Line 170... Line 49...
170
 
49
 
171
	public String getSearch_text() {
50
	public String getSearch_text() {
172
		return search_text;
51
		return search_text;
173
	}
52
	}
174
 
53
 
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
 
54
 
183
	public static void main(String[] args) throws URISyntaxException, IOException{
55
	public static void main(String[] args) throws URISyntaxException, IOException{
184
		AutoSuggestController a =  new AutoSuggestController();
56
		AutoSuggestController a =  new AutoSuggestController();
185
		a.setSearch_text("iphone tempered");
57
		a.setSearch_text("iphone tempered");
186
		a.index();
58
		a.index();