Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21287 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.io.IOException;
4
import java.net.URISyntaxException;
21297 kshitij.so 5
import java.util.List;
21287 kshitij.so 6
 
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.MediaType;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.stereotype.Controller;
13
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RequestParam;
16
 
17
import com.eclipsesource.json.Json;
18
import com.eclipsesource.json.JsonArray;
19
import com.eclipsesource.json.JsonObject;
20
import com.eclipsesource.json.JsonValue;
21389 kshitij.so 21
import com.google.gson.Gson;
22
import com.google.gson.reflect.TypeToken;
21287 kshitij.so 23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22931 ashik.ali 24
import com.spice.profitmandi.common.web.util.ResponseSender;
21297 kshitij.so 25
import com.spice.profitmandi.web.res.SolrSearchResultResponse;
26
import com.spice.profitmandi.web.res.SolrSuggestionResponse;
21287 kshitij.so 27
import com.spice.profitmandi.web.services.SolrService;
28
 
29
import io.swagger.annotations.ApiImplicitParam;
30
import io.swagger.annotations.ApiImplicitParams;
31
import io.swagger.annotations.ApiOperation;
32
 
33
@Controller
34
public class SolrSearchController {
35
 
36
	private static final Logger logger=LoggerFactory.getLogger(SolrSearchController.class);
37
 
38
	@Autowired
22931 ashik.ali 39
	private SolrService solrService;
40
 
41
	@Autowired
42
	private ResponseSender<?> responseSender;
21287 kshitij.so 43
 
44
	private static final int max_count = 10;
45
	private static final int max_count_accesories = 5;
46
	private double max_accessory_score, max_mobile_score, max_tablet_score;
47
	private int mobile_records, tablet_records;
48
	private JsonObject result_json;
49
 
50
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SEARCH, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
51
	@ApiImplicitParams({
52
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
53
				required = true, dataType = "string", paramType = "header")
54
	})
55
	@ApiOperation(value = "Search Results")
56
	public ResponseEntity<?> getSearchResults(@RequestParam("search_text") String search_text, @RequestParam("offset") int offset){
57
		logger.info("search_text : "+search_text+" offset : "+offset);
58
		String jsonString = null;
59
		try {
60
			jsonString = solrService.getSearchResults(search_text.trim(), offset);
21297 kshitij.so 61
			logger.info("Response from solr "+jsonString);
21287 kshitij.so 62
		} catch (URISyntaxException | IOException e) {
63
			logger.error("Error while gettting search results from solr",e);
23022 ashik.ali 64
			responseSender.internalServerError(e);
21287 kshitij.so 65
		}
66
		JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
67
		for (JsonValue j : result_json ){
68
			j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id").asString());
69
		}
21297 kshitij.so 70
		Gson gson = new Gson();
71
		List<SolrSearchResultResponse> solrSearchResultResponse = gson.fromJson(result_json.toString(), new TypeToken<List<SolrSearchResultResponse>>(){}.getType());
22931 ashik.ali 72
		return responseSender.ok(solrSearchResultResponse);
21287 kshitij.so 73
	}
74
 
75
 
76
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
77
	@ApiImplicitParams({
78
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
79
				required = true, dataType = "string", paramType = "header")
80
	})
81
	@ApiOperation(value = "Auto Suggest")
82
	public ResponseEntity<?> getSuggestions(@RequestParam("search_text") String search_text){
83
		logger.info("Suggestion text : "+search_text);
84
		String jsonString;
85
		try {
86
			jsonString = solrService.getSuggestions(search_text.trim());
87
		} catch (URISyntaxException | IOException e) {
88
			logger.error("Error while getting suggestions from solr",e);
23022 ashik.ali 89
			return responseSender.internalServerError(e);
21287 kshitij.so 90
		}
91
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
21297 kshitij.so 92
		Gson gson = new Gson();
93
		List<SolrSuggestionResponse> solrSearchResultResponse = gson.fromJson(sanatizedResults().toString(), new TypeToken<List<SolrSuggestionResponse>>(){}.getType());
22931 ashik.ali 94
		return responseSender.ok(solrSearchResultResponse);
21287 kshitij.so 95
	}
96
 
97
	private JsonArray sanatizedResults(){
98
		//Need to re-write this section.This sucks badly
99
		JsonArray output = Json.array().asArray();
100
		JsonArray temp_mobiles = getResults(3);
101
		JsonArray temp_tablets = getResults(5);
102
		JsonArray temp_accesories = getResults(6);
103
		if (max_accessory_score > max_mobile_score){
104
			for (JsonValue j : temp_accesories){
105
				output.add(j);
106
			}
107
		}
108
		int toFetch = temp_accesories.size() == 0 ? 5 : 3;
109
		int count = 1;
110
		if (max_mobile_score > max_tablet_score){
111
			for (JsonValue j : temp_mobiles){
112
				if (count > toFetch)
113
					break;
114
				output.add(j);
115
				count++;
116
			}
117
			count = 1;
118
			for (JsonValue j : temp_tablets){
119
				if (count > toFetch)
120
					break;
121
				output.add(j);
122
				count++;
123
			}
124
		}
125
		else{
126
			for (JsonValue j : temp_tablets){
127
				if (count > toFetch)
128
					break;
129
				output.add(j);
130
				count++;
131
			}
132
			count = 1;
133
			for (JsonValue j : temp_mobiles){
134
				if (count > toFetch)
135
					break;
136
				output.add(j);
137
				count++;
138
			}
139
		}
140
		if (max_accessory_score <= max_mobile_score){
141
			for (JsonValue j : temp_accesories){
142
				output.add(j);
143
			}
144
		}
145
		return output;
146
	}
147
 
148
	private JsonArray getResults(int category_id){
149
 
150
		JsonArray output = new JsonArray();
151
		JsonArray suggestion;
152
		JsonArray subcat_suggestion;
153
		int count = 0;
154
 
155
		switch(category_id){
156
		case 3:
157
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
158
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
159
			try{
160
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
161
			}
162
			catch(Exception e){
163
				max_mobile_score = 0.0;
164
			}
165
			for (JsonValue item : suggestion) {
166
				if (count == max_count){
167
					break;
168
				}
169
				JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().get("category").asString());
170
				output.add(temp);
171
				count++;
172
			}
173
		case 5:
174
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
175
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
176
			try{
177
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
178
			}
179
			catch(Exception e){
180
				max_tablet_score = 0.0;
181
			}
182
			for (JsonValue item : suggestion) {
183
				if (count == max_count){
184
					break;
185
				}
186
				JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().get("category").asString());
187
				output.add(temp);
188
				count++;
189
			}
190
		case 6:
191
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
192
			max_accessory_score = 0.0;
193
			for (JsonValue itemList : subcat_suggestion) {
194
				if (itemList.asObject().get("groupValue").asInt()==0){
195
					continue;
196
				}
197
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
198
				try{
199
					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;
200
				}
201
				catch(Exception e){
202
					;
203
				}
204
				count = 0;
205
				for (JsonValue item : suggestion) {
206
					if (count == max_count_accesories){
207
						break;
208
					}
209
					JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("title",item.asObject().get("title").asString()).add("category",item.asObject().getString("subCategory", "Accessories"));
210
					output.add(temp);
211
					count++;
212
				}
213
			}
214
		}
215
		return output;
216
	}
217
 
218
}