Subversion Repositories SmartDukaan

Rev

Rev 35435 | 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
35458 amit 34
@org.springframework.transaction.annotation.Transactional(rollbackFor = Throwable.class)
21287 kshitij.so 35
public class SolrSearchController {
36
 
23568 govind 37
	private static final Logger logger=LogManager.getLogger(SolrSearchController.class);
21287 kshitij.so 38
 
39
	@Autowired
22931 ashik.ali 40
	private SolrService solrService;
41
 
42
	@Autowired
43
	private ResponseSender<?> responseSender;
21287 kshitij.so 44
 
45
	private static final int max_count = 10;
46
	private static final int max_count_accesories = 5;
47
	private double max_accessory_score, max_mobile_score, max_tablet_score;
48
	private int mobile_records, tablet_records;
49
	private JsonObject result_json;
50
 
51
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SEARCH, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
52
	@ApiImplicitParams({
35435 amit 53
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
21287 kshitij.so 54
				required = true, dataType = "string", paramType = "header")
55
	})
56
	@ApiOperation(value = "Search Results")
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")
24995 amit.gupta 83
	public ResponseEntity<?> getSuggestions(@RequestParam("search_text") String searchText){
84
		logger.info("Suggestion text : "+searchText);
21287 kshitij.so 85
		String jsonString;
86
		try {
24995 amit.gupta 87
			jsonString = solrService.getSuggestions(searchText.trim());
21287 kshitij.so 88
		} catch (URISyntaxException | IOException e) {
89
			logger.error("Error while getting suggestions from solr",e);
23022 ashik.ali 90
			return responseSender.internalServerError(e);
21287 kshitij.so 91
		}
92
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
21297 kshitij.so 93
		Gson gson = new Gson();
94
		List<SolrSuggestionResponse> solrSearchResultResponse = gson.fromJson(sanatizedResults().toString(), new TypeToken<List<SolrSuggestionResponse>>(){}.getType());
22931 ashik.ali 95
		return responseSender.ok(solrSearchResultResponse);
21287 kshitij.so 96
	}
97
 
98
	private JsonArray sanatizedResults(){
99
		//Need to re-write this section.This sucks badly
100
		JsonArray output = Json.array().asArray();
101
		JsonArray temp_mobiles = getResults(3);
102
		JsonArray temp_tablets = getResults(5);
103
		JsonArray temp_accesories = getResults(6);
104
		if (max_accessory_score > max_mobile_score){
105
			for (JsonValue j : temp_accesories){
106
				output.add(j);
107
			}
108
		}
109
		int toFetch = temp_accesories.size() == 0 ? 5 : 3;
110
		int count = 1;
111
		if (max_mobile_score > max_tablet_score){
112
			for (JsonValue j : temp_mobiles){
113
				if (count > toFetch)
114
					break;
115
				output.add(j);
116
				count++;
117
			}
118
			count = 1;
119
			for (JsonValue j : temp_tablets){
120
				if (count > toFetch)
121
					break;
122
				output.add(j);
123
				count++;
124
			}
125
		}
126
		else{
127
			for (JsonValue j : temp_tablets){
128
				if (count > toFetch)
129
					break;
130
				output.add(j);
131
				count++;
132
			}
133
			count = 1;
134
			for (JsonValue j : temp_mobiles){
135
				if (count > toFetch)
136
					break;
137
				output.add(j);
138
				count++;
139
			}
140
		}
141
		if (max_accessory_score <= max_mobile_score){
142
			for (JsonValue j : temp_accesories){
143
				output.add(j);
144
			}
145
		}
146
		return output;
147
	}
148
 
149
	private JsonArray getResults(int category_id){
150
 
151
		JsonArray output = new JsonArray();
152
		JsonArray suggestion;
153
		JsonArray subcat_suggestion;
154
		int count = 0;
155
 
156
		switch(category_id){
157
		case 3:
158
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
159
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
160
			try{
161
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
162
			}
163
			catch(Exception e){
164
				max_mobile_score = 0.0;
165
			}
166
			for (JsonValue item : suggestion) {
167
				if (count == max_count){
168
					break;
169
				}
170
				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());
171
				output.add(temp);
172
				count++;
173
			}
174
		case 5:
175
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
176
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
177
			try{
178
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
179
			}
180
			catch(Exception e){
181
				max_tablet_score = 0.0;
182
			}
183
			for (JsonValue item : suggestion) {
184
				if (count == max_count){
185
					break;
186
				}
187
				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());
188
				output.add(temp);
189
				count++;
190
			}
191
		case 6:
192
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
193
			max_accessory_score = 0.0;
194
			for (JsonValue itemList : subcat_suggestion) {
195
				if (itemList.asObject().get("groupValue").asInt()==0){
196
					continue;
197
				}
198
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
199
				try{
200
					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;
201
				}
202
				catch(Exception e){
203
					;
204
				}
205
				count = 0;
206
				for (JsonValue item : suggestion) {
207
					if (count == max_count_accesories){
208
						break;
209
					}
210
					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"));
211
					output.add(temp);
212
					count++;
213
				}
214
			}
215
		}
216
		return output;
217
	}
218
 
219
}