Subversion Repositories SmartDukaan

Rev

Rev 21290 | Go to most recent revision | Details | 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);
58
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new JsonArray());
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
		}
65
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, result_json);
66
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
67
	}
68
 
69
 
70
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
71
	@ApiImplicitParams({
72
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
73
				required = true, dataType = "string", paramType = "header")
74
	})
75
	@ApiOperation(value = "Auto Suggest")
76
	public ResponseEntity<?> getSuggestions(@RequestParam("search_text") String search_text){
77
		logger.info("Suggestion text : "+search_text);
78
		String jsonString;
79
		try {
80
			jsonString = solrService.getSuggestions(search_text.trim());
81
		} catch (URISyntaxException | IOException e) {
82
			logger.error("Error while getting suggestions from solr",e);
83
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new JsonArray());
84
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
85
		}
86
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
87
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, sanatizedResults());
88
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
89
	}
90
 
91
	private JsonArray sanatizedResults(){
92
 
93
		//Need to re-write this section.This sucks badly
94
 
95
		JsonArray output = Json.array().asArray();
96
		JsonArray temp_mobiles = getResults(3);
97
		logger.info("Temp mobiles "+temp_mobiles.toString());
98
		JsonArray temp_tablets = getResults(5);
99
		logger.info("Temp tablets "+temp_tablets.toString());
100
		JsonArray temp_accesories = getResults(6);
101
		logger.info("Temp accessories "+temp_accesories.toString());
102
 
103
		if (max_accessory_score > max_mobile_score){
104
			for (JsonValue j : temp_accesories){
105
				output.add(j);
106
			}
107
		}
108
 
109
		int toFetch = temp_accesories.size() == 0 ? 5 : 3;
110
		int count = 1;
111
 
112
		if (max_mobile_score > max_tablet_score){
113
 
114
			for (JsonValue j : temp_mobiles){
115
				if (count > toFetch)
116
					break;
117
				output.add(j);
118
				count++;
119
			}
120
 
121
			count = 1;
122
 
123
			for (JsonValue j : temp_tablets){
124
				if (count > toFetch)
125
					break;
126
				output.add(j);
127
				count++;
128
			}
129
		}
130
 
131
		else{
132
			for (JsonValue j : temp_tablets){
133
				if (count > toFetch)
134
					break;
135
				output.add(j);
136
				count++;
137
			}
138
 
139
			count = 1;
140
 
141
			for (JsonValue j : temp_mobiles){
142
				if (count > toFetch)
143
					break;
144
				output.add(j);
145
				count++;
146
			}
147
		}
148
 
149
		if (max_accessory_score <= max_mobile_score){
150
			for (JsonValue j : temp_accesories){
151
				output.add(j);
152
			}
153
		}
154
 
155
		logger.info("====================================");
156
		logger.info("Final output "+output);
157
 
158
		return output;
159
	}
160
 
161
	private JsonArray getResults(int category_id){
162
 
163
		JsonArray output = new JsonArray();
164
		JsonArray suggestion;
165
		JsonArray subcat_suggestion;
166
		int count = 0;
167
 
168
		switch(category_id){
169
		case 3:
170
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
171
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
172
			try{
173
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
174
			}
175
			catch(Exception e){
176
				max_mobile_score = 0.0;
177
			}
178
			for (JsonValue item : suggestion) {
179
				if (count == max_count){
180
					break;
181
				}
182
				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());
183
				output.add(temp);
184
				count++;
185
			}
186
		case 5:
187
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
188
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
189
			try{
190
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
191
			}
192
			catch(Exception e){
193
				max_tablet_score = 0.0;
194
			}
195
			for (JsonValue item : suggestion) {
196
				if (count == max_count){
197
					break;
198
				}
199
				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());
200
				output.add(temp);
201
				count++;
202
			}
203
		case 6:
204
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
205
			max_accessory_score = 0.0;
206
			for (JsonValue itemList : subcat_suggestion) {
207
				if (itemList.asObject().get("groupValue").asInt()==0){
208
					continue;
209
				}
210
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
211
				try{
212
					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;
213
				}
214
				catch(Exception e){
215
					;
216
				}
217
				count = 0;
218
				for (JsonValue item : suggestion) {
219
					if (count == max_count_accesories){
220
						break;
221
					}
222
					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"));
223
					output.add(temp);
224
					count++;
225
				}
226
			}
227
		}
228
		return output;
229
	}
230
 
231
}