Subversion Repositories SmartDukaan

Rev

Rev 12333 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7226 anupam.sin 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.service;
5
 
6
 
7
import in.shop2020.config.ConfigException;
8
import in.shop2020.serving.controllers.SearchController;
9
import in.shop2020.serving.utils.Utils;
10
import in.shop2020.thrift.clients.config.ConfigClient;
11
 
19565 amit.gupta 12
import java.net.URI;
13
import java.net.URL;
14
import java.net.URLDecoder;
7226 anupam.sin 15
import java.util.ArrayList;
16
import java.util.Arrays;
17
import java.util.Collections;
18
import java.util.HashMap;
19
import java.util.HashSet;
20
import java.util.Iterator;
21
import java.util.LinkedHashMap;
22
import java.util.LinkedList;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Set;
26
import java.util.TreeMap;
27
import java.util.regex.Matcher;
28
import java.util.regex.Pattern;
29
 
30
import javax.xml.xpath.XPath;
31
import javax.xml.xpath.XPathConstants;
32
import javax.xml.xpath.XPathExpressionException;
33
import javax.xml.xpath.XPathFactory;
34
 
35
import org.apache.commons.collections.ListUtils;
36
import org.apache.log4j.Logger;
37
import org.w3c.dom.Node;
38
import org.w3c.dom.NodeList;
39
import org.xml.sax.InputSource;
40
 
41
 
42
/**
43
 * @author rajveer
44
 *
45
 */
46
public class SearchService {
47
    /**
48
     * 
49
     */
50
    private static Logger log = Logger.getLogger(Class.class);
51
    private HashMap<String, Double> dynamicPriceMap = null;
52
 
53
    /**
54
     * 
55
     */
56
    public static final String SOLR_URL;
57
 
58
    private static final Pattern FACET_PATTERN = Pattern.compile("(?=(F_\\d{5}))");
59
 
60
    private static final Map<String, List<String>> SORTED_FACET_VALUE_MAP = Collections.unmodifiableMap(
61
            new HashMap<String, List<String>>(){
62
                /**
63
                 * 
64
                 */
65
                private static final long serialVersionUID = 1L;
66
                {
67
                    put("F_50007", Arrays.asList("Upto 2 Mpx", "2 - 5 Mpx", "5 - 10 Mpx", "10 Mpx and above"));
68
                    put("F_50024", Arrays.asList("Upto 10 Mpx", "10 - 12 Mpx", "12 - 15 Mpx", "15 - 18 Mpx", "18 Mpx and above"));
69
                    put("F_50025", Arrays.asList("Upto 4x", "4 - 6x", "6 - 10x", "10 - 14x", "14 - 18x", "18x and above"));
70
                    put("F_50026", Arrays.asList("Below 2 in.", "2 to 2.9 in.", "3 to 3.9 in.", "4 in. and above"));
71
                    put("F_50032", Arrays.asList("Below 3 in.", "3 to 3.9 in.", "4 to 4.9 in.", "5 in. and above"));
72
                    put("F_50027", Arrays.asList("Upto 10 Mpx", "10 - 15 Mpx", "15 - 20 Mpx", "20 Mpx and above"));
73
                }
74
        });
75
    public static final Map<String, List<String>> CATEGORY_FACET_MAP = Collections.unmodifiableMap(
76
            new HashMap<String, List<String>>(){
77
                /**
78
                 * 
79
                 */
80
                private static final long serialVersionUID = 1L;
81
                //Data Connectivity, Camera Resolution,Operating System,Screen Size
82
                List<String> mobileFacets = Arrays.asList("F_50006", "F_50007", "F_50031", "F_50032");
83
                //Camera Resolution,Operating System,Screen Size
84
                List<String> tabletFacets = Arrays.asList("F_50036", "F_50035", "F_50034");
85
                //Operating System, Processor, Storage, RAM, Screen Size
86
                List<String> laptopFacets = Arrays.asList("F_50013", "F_50014", "F_50015","F_50017", "F_50033");
87
                //Resolution, Optical Zoon, Display Size
88
                List<String> compactCameras = Arrays.asList("F_50024", "F_50025", "F_50026");
89
                //Display Size
90
                List<String> dslrCameras = Arrays.asList("F_50026");
91
                //Capacity, Class
92
                List<String> memoryCards = Arrays.asList("F_50018", "F_50019");
93
                //Capacity
94
                List<String> penDrives = Arrays.asList("F_50020");
95
                //Capacity, Type, Interface
96
                List<String> externalHardDisks = Arrays.asList("F_50021", "F_50022", "F_50023");
97
                {                   
98
                    put(SearchController.getCategoryLabel(10001l), mobileFacets);
12333 amit.gupta 99
                    put(SearchController.getCategoryLabel(10006l), mobileFacets);
7226 anupam.sin 100
                    put(SearchController.getCategoryLabel(10002l), mobileFacets);
101
                    put(SearchController.getCategoryLabel(10003l), mobileFacets);
102
                    put(SearchController.getCategoryLabel(10004l), mobileFacets);
103
                    put(SearchController.getCategoryLabel(10005l), mobileFacets);
12333 amit.gupta 104
                    put(SearchController.getCategoryLabel(10006l), mobileFacets);
7226 anupam.sin 105
                    put(SearchController.getCategoryLabel(10009l), tabletFacets);
106
                    put(SearchController.getCategoryLabel(10010l), tabletFacets);
107
                    put(SearchController.getCategoryLabel(10013l), memoryCards);
108
                    put(SearchController.getCategoryLabel(10017l), penDrives);
109
                    put(SearchController.getCategoryLabel(10049l), laptopFacets);
110
                    put(SearchController.getCategoryLabel(10050l), laptopFacets);
111
                    put(SearchController.getCategoryLabel(10073l), externalHardDisks);
112
                    put(SearchController.getCategoryLabel(11002l), compactCameras);
113
                    put(SearchController.getCategoryLabel(11003l), dslrCameras);
114
                }
115
            });
116
    static {
117
        String solr_url = null;
118
        try {
119
            solr_url = ConfigClient.getClient().get("solr_url");
120
        }catch(ConfigException cex){
121
            log.error("Unable to get the solr URL from the config server. Setting the default value.", cex);
122
            solr_url = "http://localhost:8983/solr/select/";
123
        }
124
        SOLR_URL = solr_url;
125
    }
126
 
127
    /**
128
     * 
129
     */
130
    private XPath xpath;
131
 
132
    /**
133
     * 
134
     */
135
    private InputSource inputSource;
136
 
137
    Map<String,Map<String,Integer>> facetMap;
138
 
139
    private String  query;
140
 
141
    List<String> resultMap;
142
 
143
    long numberOfResults=0;
144
 
145
    String priceFacetName = "F_50002";
146
 
147
    List<String> filtrableFacets;
148
 
149
    /**
150
     * 
151
     * @param query
152
     * @param facetDefinitionIDs
153
     */
154
    public SearchService(String query, String[] facetqueries, long start, long rows,  Double minPrice, Double maxPrice, String sortOrder, long sourceId) {
155
        this.query = query;
156
 
157
        List<String> facetsQueried = new ArrayList<String>();
158
        if(sourceId != -1){
159
            priceFacetName = priceFacetName + "_" + sourceId;
160
        }
161
 
162
        setFilterableFacets(facetqueries);
163
 
164
 
165
        this.xpath = XPathFactory.newInstance().newXPath();
166
 
167
        query = query.trim().replaceAll("\\s+", " ");
168
        log.info("query=" + query);
169
 
170
        String uri = SOLR_URL + "?wt=xml&q=" + query;
171
 
172
        uri += "&stats=on&stats.field=" + priceFacetName;
173
 
174
        if(sortOrder != null){
175
            //replace the price facet name, so that it can pick price for the source.
176
            sortOrder = sortOrder.replace("F_50002", priceFacetName);
177
            uri += "&sort=" + sortOrder;
178
        }
179
 
180
        if(facetqueries != null) {
181
            //sorting will guarantee all similar facets together so that we can assume or between all similar items without fail.
182
            Arrays.sort(facetqueries);
183
            String fq="";
184
            for(int i=0; i<facetqueries.length; i++) {
185
                String[] tokens = facetqueries[i].split(":");
186
                if(facetsQueried.contains(tokens[0])) {
187
                    uri += " OR ";
188
                    if(facetqueries[i].contains(" ")){
189
                        uri +=  "\"" + tokens[1] + "\"";
190
                    }else{
191
                        uri += facetqueries[i];
192
                    }
193
 
194
                } else {
195
                    fq = "{!tag=dt" + facetsQueried.size() + "}";
196
                    facetsQueried.add(tokens[0]);
197
                    if(facetqueries[i].contains(" ") && !(facetqueries[i].contains(" OR "))){
198
                        fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
199
                    }else{
200
                        fq += facetqueries[i] + "";
201
                    }
202
                    uri += "&fq=" + fq;
203
                }
204
            }
205
        }
206
        String minString = "0";
207
        String maxString = "*";  
208
        if(minPrice != null || maxPrice != null){
209
            try {
19565 amit.gupta 210
                dynamicPriceMap = getPriceStatsMap(new InputSource(toUri(uri))); 
7226 anupam.sin 211
            } catch (Exception e){
212
                e.printStackTrace();
213
            }
214
            if(minPrice != null){
215
                minString = minPrice.toString();
216
            }
217
            if(maxPrice != null){
218
                maxString = maxPrice.toString();
219
            }
220
        }
221
        uri += "&fq=" + priceFacetName + ":["+  minString + " " + maxString + "]";
222
        uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
223
        for(String facetDefinitionID : filtrableFacets) {
224
                if(facetsQueried.contains(facetDefinitionID)){
225
                    uri += "&facet.field={!ex=dt" + facetsQueried.indexOf(facetDefinitionID)+ "}"+ facetDefinitionID; 
226
                } else {
227
                    uri += "&facet.field=" + facetDefinitionID;
228
                }
229
        }
230
        log.info("uri=" + uri);
231
 
19565 amit.gupta 232
        this.inputSource = new InputSource(toUri(uri));
7226 anupam.sin 233
 
234
        this.facetMap = getFacetMap();
235
    }
236
 
237
    @SuppressWarnings("unchecked")
238
    private void setFilterableFacets(String[] facetqueries) {
239
        List<String> queriedFacets = getAllMatches(this.query);
240
        if(facetqueries != null) {
241
            String facetString = Arrays.toString(facetqueries);
242
            List<String> filteredFacets = getAllMatches(facetString);
243
            if(filteredFacets.contains("F_50011")){
244
                for(String facetQuery : facetqueries) {
245
                    if(facetQuery.contains("F_50011")){
246
                        String facetVal = facetQuery.split(":")[1];
247
                        if(CATEGORY_FACET_MAP.containsKey(facetVal)){
248
                            this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
249
                            return;
250
                        } else {
251
                            break;
252
                        }
253
                    }
254
                }
255
            }
256
            if(filteredFacets.contains("F_50010")){
257
                for(String facetQuery : facetqueries) {
258
                    if(facetQuery.contains("F_50010")){
259
                        String facetVal = facetQuery.split(":")[1];
260
                        if(CATEGORY_FACET_MAP.containsKey(facetVal)){
261
                            this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
262
                            return;
263
                        } else {
264
                            break;
265
                        }
266
                    }
267
                }
268
            }
269
        }
270
        if(queriedFacets.contains("F_50011")) {
271
            String facetVal = this.query.split("F_50011:")[1];
272
            if (facetVal.contains(" OR ")) {
273
                this.filtrableFacets = Utils.rootfacetDefIDs;
274
                return;
275
            } else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
276
                facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
277
                if(CATEGORY_FACET_MAP.containsKey(facetVal)){
278
                    this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
279
                    return;
280
                }
281
            } 
282
        }
283
        if(queriedFacets.contains("F_50010")){
284
            String facetVal = this.query.split("F_50010:")[1];
285
            if (facetVal.contains(" OR ")) {
286
                this.filtrableFacets = Utils.rootfacetDefIDs;
287
                return;
288
            } else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
289
                facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
290
                if(CATEGORY_FACET_MAP.containsKey(facetVal)){
291
                    this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
292
                    return;
293
                }
294
            }
295
        }
296
        this.filtrableFacets = Utils.rootfacetDefIDs;
297
    }
298
 
299
    public List<String> getFilterableFacets() {
300
        return this.filtrableFacets; 
301
    }
302
 
303
    public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
304
 
305
        Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
306
        Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
307
        for(String facet : facetMap.keySet()){
308
            if(facetMap.get(facet).size() > 0 && !facetsInQuery.contains(facet)){
309
                Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
310
 
311
                for(String facetValueName : facetMap.get(facet).keySet()){
312
                    //if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
313
                        tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
314
                    //}
315
                }
316
                if(!tempMap.isEmpty()){
317
                    tempFacets.put(facet, tempMap);
318
                }
319
            }   
320
        }
321
        /*if(tempFacets.containsKey("F_50010")){
322
            tempFacets.remove("F_50011");
323
        }*/
324
 
325
        return tempFacets;
326
    }
327
 
328
    public Map<String,Integer> getFacetDetails(String facetName){
329
        if(facetMap != null){
330
            return facetMap.get(facetName);
331
        }else{
332
            return null;
333
        }
334
    }
335
 
336
    public Map<String,Map<String,Integer>> getFacetMap() {
337
        facetMap = new TreeMap<String,Map<String,Integer>>();
338
 
339
        String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
340
 
341
        NodeList nodes = null;
342
        try {
343
            nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
344
        }
345
        catch (XPathExpressionException xpee) {
346
            return null;
347
        }
348
 
349
        if(nodes.getLength() == 0) {
350
            return null;
351
        }
352
 
353
        NodeList subNodes = null;
354
 
355
        for(int i=0; i<nodes.getLength(); i++) {
356
            Node node = nodes.item(i);
357
            String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
358
            subNodes = node.getChildNodes();
359
            Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
360
            for(int j=0; j<subNodes.getLength(); j++) {
361
                Node subNode = subNodes.item(j);
362
                facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
363
            }
364
            if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
365
                List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
366
                Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
367
                for (Iterator<String> it = orderedValues.iterator(); it.hasNext();) {
368
                    String val = it.next();
369
                    if(facetValueCountMap.containsKey(val)) {
370
                        sortedMap.put(val, facetValueCountMap.get(val));
371
                    }
372
                }
373
                facetMap.put(facetName, sortedMap);
374
            } else {
375
                facetMap.put(facetName, facetValueCountMap);
376
            }
377
        }
378
        this.numberOfResults  = this.getTotalResults();
379
 
380
        facetMap = removeUnwantedFacets(facetMap, numberOfResults);
381
        return facetMap;
382
    }
383
 
384
    public List<String> getResultMap() {
385
        resultMap = new LinkedList<String>();
386
 
387
        String resultDocsPath = "/response/result/doc";
388
 
389
 
390
        NodeList nodes = null;
391
        try {
392
            nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
393
        }
394
        catch (XPathExpressionException xpee) {
395
            return null;
396
        }
397
 
398
        if(nodes.getLength() == 0) {
399
            return null;
400
        }
401
 
402
        for(int i=0; i<nodes.getLength(); i++) {
403
            Node node = nodes.item(i);
404
            String docID = node.getFirstChild().getTextContent();
405
            resultMap.add(docID);   
406
        }
407
        return resultMap;
408
    }
409
 
410
    public HashMap<String, Double> getPriceStatsMap() {
411
        return this.getPriceStatsMap(this.inputSource);
412
    }
413
 
414
    public HashMap<String, Double> getPriceStatsMap(InputSource inputSource) {
415
        HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
416
 
417
        String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = '" + priceFacetName + "']";
418
 
419
 
420
        NodeList nodes = null;
421
        try {
422
            nodes = (NodeList) this.xpath.evaluate(resultDocsPath, inputSource, XPathConstants.NODESET);
423
        }
424
        catch (XPathExpressionException xpee) {
425
            return null;
426
        }
427
 
428
        if(nodes.getLength() == 0) {
429
            return null;
430
        }
431
 
432
        NodeList subNodes = nodes.item(0).getChildNodes();
433
 
434
        for(int i=0; i<subNodes.getLength(); i++) {
435
            Node node = subNodes.item(i);
436
 
437
            String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
438
            String value = node.getTextContent();
439
            priceStatsMap.put(parameter, Double.parseDouble(value));    
440
        }
441
        return priceStatsMap;
442
    }
443
 
444
    public HashMap<String,Integer> getRangeQueryResultMap() {
445
        HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
446
 
447
        String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
448
 
449
 
450
        NodeList nodes = null;
451
        try {
452
            nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
453
        }
454
        catch (XPathExpressionException xpee) {
455
            return null;
456
        }
457
 
458
        if(nodes.getLength() == 0) {
459
            return null;
460
        }
461
 
462
 
463
        for(int i=0; i<nodes.getLength(); i++) {
464
            Node node = nodes.item(i);
465
 
466
            String query = node.getAttributes().getNamedItem("name").getNodeValue();
467
            String docCount = node.getTextContent();
468
 
469
            rangeQueryResultMap.put(query,Integer.parseInt(docCount));  
470
        }
471
        return rangeQueryResultMap;
472
 
473
    }
474
 
475
    /**
476
     * 
477
     */
478
    public long getTotalResults(){
479
        String resultDocsPath = "/response/result";
480
        NodeList nodes = null;
481
        try {
482
            nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
483
        }
484
        catch (XPathExpressionException xpee) {
485
            return 0;
486
        }
487
 
488
        Node node = nodes.item(0);
489
 
490
        return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
491
 
492
    }
493
        /**
494
     * 
495
     * @return
496
     */
497
    public long[] getResultEntityIDs() {
498
        String expression = "/response/result/doc/long";
499
 
500
        NodeList nodes = null;
501
        try {
502
            nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
503
                    XPathConstants.NODESET);
504
        } 
505
        catch(XPathExpressionException xpee) {
506
            return null;
507
        }
508
 
509
        if(nodes.getLength() == 0) {
510
            return null;
511
        }
512
 
513
        long[] values = new long[nodes.getLength()];
514
        for(int i=0; i<nodes.getLength(); i++) {
515
            Node node = nodes.item(i);
516
            String value = node.getTextContent();
517
            values[i] = Long.parseLong(value);
518
        }
519
 
520
        return values;
521
    }
522
 
523
    /**
524
     * 
525
     * @return
526
     */
527
    public String[] getResultCategoryNames() {
528
        String expression = "/response/lst/lst[@name = 'facet_fields']/";
529
        expression += "lst[@name = 'Category']/int/@name";
530
 
531
        NodeList nodes = null;
532
        try {
533
            nodes = (NodeList) this.xpath.evaluate(expression, 
534
                this.inputSource, XPathConstants.NODESET);
535
        }
536
        catch (XPathExpressionException xpee) {
537
            return null;
538
        }
539
 
540
        if(nodes.getLength() == 0) {
541
            return null;
542
        }
543
 
544
        String[] values = new String[nodes.getLength()];
545
        for(int i=0; i<nodes.getLength(); i++) {
546
            Node node = nodes.item(i);
547
            values[i] = node.getTextContent();
548
        }
549
 
550
        return values;
551
    }
552
 
553
    /**
554
     * 
555
     * @return
556
     */
557
    public int[] getResultCategoryCounts() {
558
        String expression = "/response/lst/lst[@name = 'facet_fields']/";
559
        expression += "lst[@name = 'Category']/int";
560
 
561
        NodeList nodes = null;
562
        try {
563
            nodes = (NodeList) this.xpath.evaluate(expression, 
564
                this.inputSource, XPathConstants.NODESET);
565
        }
566
        catch (XPathExpressionException xpee) {
567
            return null;
568
        }
569
 
570
        if(nodes.getLength() == 0) {
571
            return null;
572
        }
573
 
574
        int[] values = new int[nodes.getLength()];
575
        for(int i=0; i<nodes.getLength(); i++) {
576
            Node node = nodes.item(i);
577
            values[i] = Integer.parseInt(node.getTextContent());
578
        }
579
 
580
        return values;
581
    }
582
 
583
    /**
584
     * 
585
     * @return
586
     */
587
    public String[]  getResultEntityNames() {
588
        String expression = "/response/result/doc/str";
589
 
590
        NodeList nodes = null;
591
        try {
592
            nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
593
                    XPathConstants.NODESET);
594
        } 
595
        catch(XPathExpressionException xpee) {
596
            return null;
597
        }
598
 
599
        if(nodes.getLength() == 0) {
600
            return null;
601
        }
602
 
603
        String[] values = new String[nodes.getLength()];
604
        for(int i=0; i<nodes.getLength(); i++) {
605
            Node node = nodes.item(i);
606
            String value = node.getTextContent();
607
            values[i] = value;
608
        }
609
 
610
        return values;
611
    }
612
 
613
    /**
614
     * 
615
     * @param facetDefinitionID
616
     * @return
617
     */
618
    public String[] getFacetValues(String facetDefinitionID) {
619
        String expression = "/response/lst/lst[@name = 'facet_fields']/";
620
        expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
621
 
622
        NodeList nodes = null;
623
        try {
624
            nodes = (NodeList) this.xpath.evaluate(expression, 
625
                this.inputSource, XPathConstants.NODESET);
626
        }
627
        catch (XPathExpressionException xpee) {
628
            return null;
629
        }
630
 
631
        if(nodes.getLength() == 0) {
632
            return null;
633
        }
634
 
635
        String[] values = new String[nodes.getLength()];
636
        for(int i=0; i<nodes.getLength(); i++) {
637
            Node node = nodes.item(i);
638
            values[i] = node.getTextContent();
639
        }
640
 
641
        return values;
642
    }
643
 
644
    /**
645
     * 
646
     * @param facetDefinitionID
647
     * @return
648
     */
649
    public String[] getFacetCounts(String facetDefinitionID) {
650
        String expression = "/response/lst/lst[@name = 'facet_fields']/";
651
        expression += "lst[@name = '" + facetDefinitionID + "']/int";
652
 
653
        NodeList nodes = null;
654
        try {
655
            nodes = (NodeList) this.xpath.evaluate(expression, 
656
                this.inputSource, XPathConstants.NODESET);
657
        }
658
        catch (XPathExpressionException xpee) {
659
            return null;
660
        }
661
 
662
        if(nodes.getLength() == 0) {
663
            return null;
664
        }
665
 
666
        String[] values = new String[nodes.getLength()];
667
        for(int i=0; i<nodes.getLength(); i++) {
668
            Node node = nodes.item(i);
669
            values[i] = node.getTextContent();
670
        }
671
 
672
        return values;
673
    }
674
 
675
    public static void main(String[] args){
676
        /*
677
        // Hard coded for now
678
        String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
679
 
680
        // Hard-coded for now
681
        String[] facetLabels = new String[] {
682
            "Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
683
            "Data Connectivity", "Camera Resolution", "Built-in Memory", 
684
            "Talk time"
685
        };
686
 
687
         */
688
        String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
689
        //String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"    };
690
 
691
 
692
        String[] fqrys = {};
693
        SearchService search = new SearchService("nokia", fqrys, 0 , 20, null, null, null, -1);
694
 
695
        long[] entityIDs = search.getResultEntityIDs();
696
        log.info("entityIDs=" + Arrays.toString(entityIDs));
697
 
698
        String[] entityNames = search.getResultEntityNames();
699
        log.info("entityNames=" + Arrays.toString(entityNames));
700
        search.getFacetMap();
701
 
702
        search.getResultMap();
703
        search.getRangeQueryResultMap();
704
        search.getPriceStatsMap(new InputSource());
705
        search.getTotalResults();
706
        for (int i=0; i<facetDefIDs.length; i++) {
707
            search.getFacetCounts(facetDefIDs[i]);
708
            search.getFacetValues(facetDefIDs[i]);
709
        }
710
 
711
    }
712
 
713
 
714
    public static List<String> getAllMatches(String text) {
715
        List<String> matches = new ArrayList<String>();
716
        Matcher m = FACET_PATTERN.matcher(text);
717
        while(m.find()) {
718
            matches.add(m.group(1));
719
        }
720
        return matches;
721
    }
722
 
723
    public Map<String, Double> getDynamicPriceMap() {
724
        return this.dynamicPriceMap;
725
    }
726
 
19565 amit.gupta 727
	public String toUri(String string){
728
	    try {
729
	        String decodedURL = URLDecoder.decode(string, "UTF-8");
730
	        URL url = new URL(decodedURL);
731
	        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
732
	        return uri.toASCIIString(); 
733
	    } catch (Exception ex) {
734
	        ex.printStackTrace();
735
	        return null;
736
	    }
737
	}
738
 
7226 anupam.sin 739
}