Subversion Repositories SmartDukaan

Rev

Rev 20234 | Rev 20261 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
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;
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());
20218 kshitij.so 42
			JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
20216 kshitij.so 43
			setResultJson(result_json.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
 
70
		int toFetch = temp_accesories.size() == 0 ? 10 : 5;
71
		int count = 0;
72
		for (JsonValue j : temp_mobiles){
73
			if (count > toFetch)
74
				break;
75
			output.add(j);
76
			count++;
77
		}
78
		count = 0;
79
		for (JsonValue j : temp_tablets){
80
			if (count > toFetch)
81
				break;
82
			output.add(j);
83
			count++;
84
		}
85
 
86
		if (max_accessory_score <= max_mobile_score){
87
			for (JsonValue j : temp_accesories){
88
				output.add(j);
89
			}
90
		}
91
 
92
		log.info("====================================");
93
		log.info("Final output "+output);
94
 
95
		return output;
96
	}
97
 
98
	private JsonArray getResults(int category_id){
99
 
100
		JsonArray output = new JsonArray();
101
		JsonArray suggestion;
102
		JsonArray subcat_suggestion;
103
		int count = 0;
104
 
105
		switch(category_id){
106
		case 3:
107
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
108
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
109
			try{
110
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
111
			}
112
			catch(Exception e){
113
				max_mobile_score = 0.0;
114
			}
115
			for (JsonValue item : suggestion) {
116
				if (count == max_count){
117
					break;
118
				}
119
				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());
120
				output.add(temp);
121
				count++;
122
			}
123
		case 5:
124
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
125
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
126
			try{
127
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
128
			}
129
			catch(Exception e){
130
				max_tablet_score = 0.0;
131
			}
132
			for (JsonValue item : suggestion) {
133
				if (count == max_count){
134
					break;
135
				}
136
				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());
137
				output.add(temp);
138
				count++;
139
			}
140
		case 6:
141
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
142
			max_accessory_score = 0.0;
143
			for (JsonValue itemList : subcat_suggestion) {
144
				if (itemList.asObject().get("groupValue").asInt()==0){
145
					continue;
146
				}
147
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
148
				try{
149
					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;
150
				}
151
				catch(Exception e){
152
					;
153
				}
154
				count = 0;
155
				for (JsonValue item : suggestion) {
156
					if (count == max_count_accesories){
157
						break;
158
					}
159
					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"));
160
					output.add(temp);
161
					count++;
162
				}
163
			}
164
		}
165
		return output;
166
	}
167
 
168
 
20229 kshitij.so 169
	public String getSearchResults(){
170
		try{
171
			log.info("Calling solr service (search results) for search_text "+search_text);
172
			String jsonString = s.getSearchResults(search_text.trim(), offset);
173
			JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
20231 kshitij.so 174
			for (JsonValue j : result_json ){
20234 kshitij.so 175
				j.asObject().add("productUrl", j.asObject().get("id").asString()+"/"+j.asObject().get("skuBundleId").asInt());
20231 kshitij.so 176
			}
20229 kshitij.so 177
			setResultJson(result_json.toString());
178
		}
179
		catch(Exception e){
180
			e.printStackTrace();
181
			setResultJson(Json.array().asArray().toString());
182
		}
183
		return "index";
184
	}
20193 kshitij.so 185
 
186
 
20186 kshitij.so 187
	public void setSearch_text(String search_text) {
20213 kshitij.so 188
		log.info("Search text "+search_text);
20186 kshitij.so 189
		this.search_text = search_text;
190
	}
191
 
192
 
193
	public String getSearch_text() {
194
		return search_text;
195
	}
20193 kshitij.so 196
 
20209 kshitij.so 197
 
20186 kshitij.so 198
	public static void main(String[] args) throws URISyntaxException, IOException{
20259 kshitij.so 199
		SolrSearchController a = new SolrSearchController();
20209 kshitij.so 200
		a.setSearch_text("iphone tempered");
20229 kshitij.so 201
		a.getSuggestions();
20186 kshitij.so 202
		System.out.println(a.getResultJson());
203
	}
20193 kshitij.so 204
 
20220 kshitij.so 205
 
206
	public void setOffset(String offset) {
207
		this.offset = offset;
208
	}
209
 
210
 
211
	public String getOffset() {
212
		return offset;
213
	}
214
 
20186 kshitij.so 215
}