Subversion Repositories SmartDukaan

Rev

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