Subversion Repositories SmartDukaan

Rev

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

Rev 5943 Rev 6866
Line 3... Line 3...
3
 */
3
 */
4
package in.shop2020.serving.services;
4
package in.shop2020.serving.services;
5
 
5
 
6
 
6
 
7
import in.shop2020.config.ConfigException;
7
import in.shop2020.config.ConfigException;
-
 
8
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.thrift.clients.config.ConfigClient;
9
import in.shop2020.thrift.clients.config.ConfigClient;
9
import in.shop2020.utils.CategoryManager;
10
import in.shop2020.utils.CategoryManager;
10
 
11
 
-
 
12
import java.util.ArrayList;
11
import java.util.Arrays;
13
import java.util.Arrays;
12
import java.util.Collections;
14
import java.util.Collections;
13
import java.util.HashMap;
15
import java.util.HashMap;
-
 
16
import java.util.HashSet;
14
import java.util.Iterator;
17
import java.util.Iterator;
15
import java.util.LinkedHashMap;
18
import java.util.LinkedHashMap;
16
import java.util.LinkedList;
19
import java.util.LinkedList;
17
import java.util.List;
20
import java.util.List;
18
import java.util.Map;
21
import java.util.Map;
-
 
22
import java.util.Set;
19
import java.util.TreeMap;
23
import java.util.TreeMap;
20
 
-
 
-
 
24
import java.util.regex.Matcher;
-
 
25
import java.util.regex.Pattern;
21
 
26
 
22
import javax.xml.xpath.XPath;
27
import javax.xml.xpath.XPath;
23
import javax.xml.xpath.XPathConstants;
28
import javax.xml.xpath.XPathConstants;
24
import javax.xml.xpath.XPathExpressionException;
29
import javax.xml.xpath.XPathExpressionException;
25
import javax.xml.xpath.XPathFactory;
30
import javax.xml.xpath.XPathFactory;
Line 43... Line 48...
43
	/**
48
	/**
44
	 * 
49
	 * 
45
	 */
50
	 */
46
	public static final String SOLR_URL;
51
	public static final String SOLR_URL;
47
	
52
	
-
 
53
	private static final Pattern FACET_PATTERN = Pattern.compile("(?=(F_\\d{5}))");
-
 
54
	
48
	private static final Map<String, List<String>> SORTED_FACET_VALUE_MAP = Collections.unmodifiableMap(
55
	private static final Map<String, List<String>> SORTED_FACET_VALUE_MAP = Collections.unmodifiableMap(
49
			new HashMap<String, List<String>>(){
56
			new HashMap<String, List<String>>(){
50
				/**
57
				/**
51
				 * 
58
				 * 
52
				 */
59
				 */
Line 81... Line 88...
81
	 */
88
	 */
82
	private InputSource inputSource;
89
	private InputSource inputSource;
83
	
90
	
84
	Map<String,Map<String,Integer>> facetMap;
91
	Map<String,Map<String,Integer>> facetMap;
85
	
92
	
-
 
93
	private String 	query;
-
 
94
	
86
	List<String> resultMap;
95
	List<String> resultMap;
87
	
96
	
88
	long numberOfResults=0;
97
	long numberOfResults=0;
89
	
98
	
90
	String priceFacetName = "F_50002";
99
	String priceFacetName = "F_50002";
Line 93... Line 102...
93
	 * 
102
	 * 
94
	 * @param query
103
	 * @param query
95
	 * @param facetDefinitionIDs
104
	 * @param facetDefinitionIDs
96
	 */
105
	 */
97
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice, long categoryId, String sortOrder, long sourceId) {
106
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice, long categoryId, String sortOrder, long sourceId) {
-
 
107
		this.query = query;
98
		
108
		
-
 
109
		List<String> rootFacetsQueried = new ArrayList<String>();
99
		if(sourceId != -1){
110
		if(sourceId != -1){
100
			priceFacetName = priceFacetName + "_" + sourceId;
111
			priceFacetName = priceFacetName + "_" + sourceId;
101
		}
112
		}
102
		
113
		
103
		this.xpath = XPathFactory.newInstance().newXPath();
114
		this.xpath = XPathFactory.newInstance().newXPath();
Line 131... Line 142...
131
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
142
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
132
			uri += "&sort=" + sortOrder;
143
			uri += "&sort=" + sortOrder;
133
		}
144
		}
134
		
145
		
135
		if(facetqueries != null) {
146
		if(facetqueries != null) {
-
 
147
			//sorting will guarantee all similar facets together so that we can assume or between all similar items without fail.
-
 
148
			Arrays.sort(facetqueries);
-
 
149
			String fq="";
136
			for(int i=0; i<facetqueries.length; i++) {
150
			for(int i=0; i<facetqueries.length; i++) {
137
				if(facetqueries[i].contains(" ") && !(facetqueries[i].contains("F_50002") || facetqueries[i].contains(" OR "))){
151
				String[] tokens = facetqueries[i].split(":");
-
 
152
				if(rootFacetsQueried.contains(tokens[0])) {
-
 
153
					uri += " OR ";
-
 
154
					if(facetqueries[i].contains(" ")){
-
 
155
						uri +=  "\"" + tokens[1] + "\"";
-
 
156
					}else{
138
					String[] tokens = facetqueries[i].split(":");
157
						uri += facetqueries[i];
-
 
158
					}
-
 
159
					
-
 
160
				} else {
-
 
161
					if(Arrays.asList(Utils.facetDefIDs).contains(tokens[0])) {
-
 
162
						fq = "{!tag=dt" + rootFacetsQueried.size() + "}";
-
 
163
						rootFacetsQueried.add(tokens[0]);
-
 
164
						if(facetqueries[i].contains(" ") && !(facetqueries[i].contains(" OR "))){
139
					uri += "&fq=" + tokens[0] + ":\"" + tokens[1] + "\"";
165
							fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
140
				}else{
166
						}else{
141
					uri += "&fq=" + facetqueries[i] + "";
167
							fq += facetqueries[i] + "";
-
 
168
						}
-
 
169
					}
-
 
170
					uri += "&fq=" + fq;
142
				}
171
				}
143
			}
172
			}
144
		}
173
		}
145
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows;
174
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
146
		if(facetDefinitionIDs != null){
175
		if(facetDefinitionIDs != null){
147
			for(int i=0; i<facetDefinitionIDs.length; i++) {
176
			for(int i=0; i<facetDefinitionIDs.length; i++) {
-
 
177
				if(rootFacetsQueried.contains(facetDefinitionIDs[i])){
-
 
178
					uri += "&facet.field={!ex=dt" + rootFacetsQueried.indexOf(facetDefinitionIDs[i])+ "}"+ facetDefinitionIDs[i]; 
-
 
179
				} else {
148
				uri += "&facet.field=" + facetDefinitionIDs[i];
180
					uri += "&facet.field=" + facetDefinitionIDs[i];
-
 
181
				}
149
			}
182
			}
150
		}
183
		}
151
		log.info("uri=" + uri);
184
		log.info("uri=" + uri);
152
		
185
		
153
		this.inputSource = new InputSource(uri);
186
		this.inputSource = new InputSource(uri);
154
 
187
 
155
		this.facetMap = getFacetMap();
188
		this.facetMap = getFacetMap();
156
	}
189
	}
157
 
190
 
158
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
191
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
-
 
192
		
-
 
193
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
159
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
194
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
160
		for(String facet : facetMap.keySet()){
195
		for(String facet : facetMap.keySet()){
161
			if(facetMap.get(facet).size() > 1){
196
			if(facetMap.get(facet).size() > 0 && !facetsInQuery.contains(facet)){
162
				Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
197
				Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
163
				
198
				
164
				for(String facetValueName : facetMap.get(facet).keySet()){
199
				for(String facetValueName : facetMap.get(facet).keySet()){
165
					if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
200
					//if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
166
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
201
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
167
					}
202
					//}
168
				}
203
				}
169
				if(!tempMap.isEmpty()){
204
				if(!tempMap.isEmpty()){
170
					tempFacets.put(facet, tempMap);
205
					tempFacets.put(facet, tempMap);
171
				}
206
				}
172
			}	
207
			}	
173
		}
208
		}
174
		if(tempFacets.containsKey("F_50010")){
209
		/*if(tempFacets.containsKey("F_50010")){
175
			tempFacets.remove("F_50011");
210
			tempFacets.remove("F_50011");
176
		}
211
		}*/
177
 
212
 
178
		return tempFacets;
213
		return tempFacets;
179
	}
214
	}
180
	
215
	
181
	public Map<String,Integer> getFacetDetails(String facetName){
216
	public Map<String,Integer> getFacetDetails(String facetName){
Line 210... Line 245...
210
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
245
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
211
			subNodes = node.getChildNodes();
246
			subNodes = node.getChildNodes();
212
			Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
247
			Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
213
			for(int j=0; j<subNodes.getLength(); j++) {
248
			for(int j=0; j<subNodes.getLength(); j++) {
214
				Node subNode = subNodes.item(j);
249
				Node subNode = subNodes.item(j);
215
				if(Integer.parseInt(subNode.getTextContent())==0)
-
 
216
						continue;
-
 
217
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
250
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
218
			}
251
			}
219
			if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
252
			if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
220
				List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
253
				List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
221
				Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
254
				Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
Line 558... Line 591...
558
       		search.getFacetCounts(facetDefIDs[i]);
591
       		search.getFacetCounts(facetDefIDs[i]);
559
       		search.getFacetValues(facetDefIDs[i]);
592
       		search.getFacetValues(facetDefIDs[i]);
560
       	}
593
       	}
561
       	
594
       	
562
	}
595
	}
-
 
596
	
-
 
597
 
-
 
598
    public static List<String> getAllMatches(String text) {
-
 
599
        List<String> matches = new ArrayList<String>();
-
 
600
        Matcher m = FACET_PATTERN.matcher(text);
-
 
601
        while(m.find()) {
-
 
602
            matches.add(m.group(1));
-
 
603
        }
-
 
604
        return matches;
-
 
605
    }
563
}
606
}