Subversion Repositories SmartDukaan

Rev

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