Subversion Repositories SmartDukaan

Rev

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