Subversion Repositories SmartDukaan

Rev

Rev 21297 | Rev 21389 | 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
		//Need to re-write this section.This sucks badly
105
		JsonArray output = Json.array().asArray();
106
		JsonArray temp_mobiles = getResults(3);
107
		JsonArray temp_tablets = getResults(5);
108
		JsonArray temp_accesories = getResults(6);
109
		if (max_accessory_score > max_mobile_score){
110
			for (JsonValue j : temp_accesories){
111
				output.add(j);
112
			}
113
		}
114
		int toFetch = temp_accesories.size() == 0 ? 5 : 3;
115
		int count = 1;
116
		if (max_mobile_score > max_tablet_score){
117
			for (JsonValue j : temp_mobiles){
118
				if (count > toFetch)
119
					break;
120
				output.add(j);
121
				count++;
122
			}
123
			count = 1;
124
			for (JsonValue j : temp_tablets){
125
				if (count > toFetch)
126
					break;
127
				output.add(j);
128
				count++;
129
			}
130
		}
131
		else{
132
			for (JsonValue j : temp_tablets){
133
				if (count > toFetch)
134
					break;
135
				output.add(j);
136
				count++;
137
			}
138
			count = 1;
139
			for (JsonValue j : temp_mobiles){
140
				if (count > toFetch)
141
					break;
142
				output.add(j);
143
				count++;
144
			}
145
		}
146
		if (max_accessory_score <= max_mobile_score){
147
			for (JsonValue j : temp_accesories){
148
				output.add(j);
149
			}
150
		}
151
		return output;
152
	}
153
 
154
	private JsonArray getResults(int category_id){
155
 
156
		JsonArray output = new JsonArray();
157
		JsonArray suggestion;
158
		JsonArray subcat_suggestion;
159
		int count = 0;
160
 
161
		switch(category_id){
162
		case 3:
163
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
164
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
165
			try{
166
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
167
			}
168
			catch(Exception e){
169
				max_mobile_score = 0.0;
170
			}
171
			for (JsonValue item : suggestion) {
172
				if (count == max_count){
173
					break;
174
				}
175
				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());
176
				output.add(temp);
177
				count++;
178
			}
179
		case 5:
180
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
181
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
182
			try{
183
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
184
			}
185
			catch(Exception e){
186
				max_tablet_score = 0.0;
187
			}
188
			for (JsonValue item : suggestion) {
189
				if (count == max_count){
190
					break;
191
				}
192
				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());
193
				output.add(temp);
194
				count++;
195
			}
196
		case 6:
197
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
198
			max_accessory_score = 0.0;
199
			for (JsonValue itemList : subcat_suggestion) {
200
				if (itemList.asObject().get("groupValue").asInt()==0){
201
					continue;
202
				}
203
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
204
				try{
205
					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;
206
				}
207
				catch(Exception e){
208
					;
209
				}
210
				count = 0;
211
				for (JsonValue item : suggestion) {
212
					if (count == max_count_accesories){
213
						break;
214
					}
215
					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"));
216
					output.add(temp);
217
					count++;
218
				}
219
			}
220
		}
221
		return output;
222
	}
223
 
224
}