Subversion Repositories SmartDukaan

Rev

Rev 20234 | Rev 20261 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20234 Rev 20259
Line 7... Line 7...
7
 
7
 
8
import org.apache.log4j.Logger;
8
import org.apache.log4j.Logger;
9
 
9
 
10
import com.eclipsesource.json.Json;
10
import com.eclipsesource.json.Json;
11
import com.eclipsesource.json.JsonArray;
11
import com.eclipsesource.json.JsonArray;
-
 
12
import com.eclipsesource.json.JsonObject;
12
import com.eclipsesource.json.JsonValue;
13
import com.eclipsesource.json.JsonValue;
-
 
14
import java.io.InputStream;
-
 
15
import java.net.URL;
-
 
16
 
-
 
17
import org.apache.commons.io.IOUtils;
-
 
18
import org.apache.http.client.utils.URIBuilder;
13
 
19
 
14
 
20
 
15
 
21
 
16
public class SolrSearchController extends BaseController{
22
public class SolrSearchController extends BaseController{
17
	private static Logger log = Logger.getLogger(Class.class);
23
	private static Logger log = Logger.getLogger(Class.class);
18
 
24
 
19
	private static final long serialVersionUID = 1L;
25
	private static final long serialVersionUID = 1L;
20
	private String search_text;
26
	private String search_text;
21
	private String offset;
27
	private String offset;
-
 
28
	private static final int max_count = 10;
-
 
29
    private static final int max_count_accesories = 5;
-
 
30
    private double max_accessory_score, max_mobile_score, max_tablet_score;
-
 
31
    private int mobile_records, tablet_records;
-
 
32
    private JsonObject result_json;
-
 
33
 
-
 
34
 
22
	private SolrService s =  new SolrService();
35
	private SolrService s =  new SolrService();
23
 
36
 
24
 
37
 
25
	public String getSuggestions() throws URISyntaxException, IOException{
38
	public String getSuggestions() throws URISyntaxException, IOException{
26
		try{
39
		try{
Line 33... Line 46...
33
			e.printStackTrace();
46
			e.printStackTrace();
34
			setResultJson(Json.array().asArray().toString());
47
			setResultJson(Json.array().asArray().toString());
35
		}
48
		}
36
		return "index";
49
		return "index";
37
	}
50
	}
-
 
51
 
-
 
52
	private JsonArray sanatizedResults(){
-
 
53
 
-
 
54
		//Need to re-write this section.This sucks badly
-
 
55
 
-
 
56
		JsonArray output = Json.array().asArray();
-
 
57
		JsonArray temp_mobiles = getResults(3);
-
 
58
		log.info("Temp mobiles "+temp_mobiles.toString());
-
 
59
		JsonArray temp_tablets = getResults(5);
-
 
60
		log.info("Temp tablets "+temp_tablets.toString());
-
 
61
		JsonArray temp_accesories = getResults(6);
-
 
62
		log.info("Temp accessories "+temp_accesories.toString());
-
 
63
 
-
 
64
		if (max_accessory_score > max_mobile_score){
-
 
65
			for (JsonValue j : temp_accesories){
-
 
66
				output.add(j);
-
 
67
			}
-
 
68
		}
-
 
69
 
-
 
70
		int toFetch = temp_accesories.size() == 0 ? 10 : 5;
-
 
71
		int count = 0;
-
 
72
		for (JsonValue j : temp_mobiles){
-
 
73
			if (count > toFetch)
-
 
74
				break;
-
 
75
			output.add(j);
-
 
76
			count++;
-
 
77
		}
-
 
78
		count = 0;
-
 
79
		for (JsonValue j : temp_tablets){
-
 
80
			if (count > toFetch)
-
 
81
				break;
-
 
82
			output.add(j);
-
 
83
			count++;
-
 
84
		}
-
 
85
 
-
 
86
		if (max_accessory_score <= max_mobile_score){
-
 
87
			for (JsonValue j : temp_accesories){
-
 
88
				output.add(j);
-
 
89
			}
-
 
90
		}
-
 
91
 
-
 
92
		log.info("====================================");
-
 
93
		log.info("Final output "+output);
-
 
94
 
-
 
95
		return output;
-
 
96
	}
-
 
97
 
-
 
98
	private JsonArray getResults(int category_id){
-
 
99
 
-
 
100
		JsonArray output = new JsonArray();
-
 
101
		JsonArray suggestion;
-
 
102
		JsonArray subcat_suggestion;
-
 
103
		int count = 0;
-
 
104
 
-
 
105
		switch(category_id){
-
 
106
		case 3:
-
 
107
			suggestion= result_json.get("category_id:3").asObject().get("doclist").asObject().get("docs").asArray();
-
 
108
			mobile_records = result_json.get("category_id:3").asObject().get("doclist").asObject().get("numFound").asInt();
-
 
109
			try{
-
 
110
				max_mobile_score = result_json.get("category_id:3").asObject().get("doclist").asObject().get("maxScore").asDouble();
-
 
111
			}
-
 
112
			catch(Exception e){
-
 
113
				max_mobile_score = 0.0;
-
 
114
			}
-
 
115
			for (JsonValue item : suggestion) {
-
 
116
				if (count == max_count){
-
 
117
					break;
-
 
118
				}
-
 
119
				JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().get("category").asString());
-
 
120
				output.add(temp);
-
 
121
				count++;
-
 
122
			}
-
 
123
		case 5:
-
 
124
			suggestion = result_json.get("category_id:5").asObject().get("doclist").asObject().get("docs").asArray();
-
 
125
			tablet_records = result_json.get("category_id:5").asObject().get("doclist").asObject().get("numFound").asInt();
-
 
126
			try{
-
 
127
				max_tablet_score = result_json.get("category_id:5").asObject().get("doclist").asObject().get("maxScore").asDouble();
-
 
128
			}
-
 
129
			catch(Exception e){
-
 
130
				max_tablet_score = 0.0;
-
 
131
			}
-
 
132
			for (JsonValue item : suggestion) {
-
 
133
				if (count == max_count){
-
 
134
					break;
-
 
135
				}
38
	
136
				JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().get("category").asString());
-
 
137
				output.add(temp);
-
 
138
				count++;
-
 
139
			}
-
 
140
		case 6:
-
 
141
			subcat_suggestion = result_json.get("subCategoryId").asObject().get("groups").asArray();
-
 
142
			max_accessory_score = 0.0;
-
 
143
			for (JsonValue itemList : subcat_suggestion) {
-
 
144
				if (itemList.asObject().get("groupValue").asInt()==0){
-
 
145
					continue;
-
 
146
				}
-
 
147
				suggestion = itemList.asObject().get("doclist").asObject().get("docs").asArray();
-
 
148
				try{
-
 
149
					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;
-
 
150
				}
-
 
151
				catch(Exception e){
-
 
152
					;
-
 
153
				}
-
 
154
				count = 0;
-
 
155
				for (JsonValue item : suggestion) {
-
 
156
					if (count == max_count_accesories){
-
 
157
						break;
-
 
158
					}
-
 
159
					JsonObject temp = Json.object().add("subCategoryId",item.asObject().get("subCategoryId").asInt()).add("category_id",item.asObject().get("category_id").asInt()).add("suggestion",item.asObject().get("title").asString()).add("category_name",item.asObject().getString("subCategory", "Accessories"));
-
 
160
					output.add(temp);
-
 
161
					count++;
-
 
162
				}
-
 
163
			}
-
 
164
		}
-
 
165
		return output;
-
 
166
	}
-
 
167
 
-
 
168
 
39
	public String getSearchResults(){
169
	public String getSearchResults(){
40
		try{
170
		try{
41
			log.info("Calling solr service (search results) for search_text "+search_text);
171
			log.info("Calling solr service (search results) for search_text "+search_text);
42
			String jsonString = s.getSearchResults(search_text.trim(), offset);
172
			String jsonString = s.getSearchResults(search_text.trim(), offset);
43
			JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
173
			JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
Line 64... Line 194...
64
		return search_text;
194
		return search_text;
65
	}
195
	}
66
 
196
 
67
 
197
 
68
	public static void main(String[] args) throws URISyntaxException, IOException{
198
	public static void main(String[] args) throws URISyntaxException, IOException{
69
		SolrSearchController a =  new SolrSearchController();
199
		SolrSearchController a = new SolrSearchController();
70
		a.setSearch_text("iphone tempered");
200
		a.setSearch_text("iphone tempered");
71
		a.getSuggestions();
201
		a.getSuggestions();
72
		System.out.println(a.getResultJson());
202
		System.out.println(a.getResultJson());
73
	}
203
	}
74
 
204