Subversion Repositories SmartDukaan

Rev

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