Subversion Repositories SmartDukaan

Rev

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