Subversion Repositories SmartDukaan

Rev

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