define(['app', 'underscore'], function (app, _) {

    app.factory('searchService', ['$http', '$q', function ($http, $q) {
        return new function () {
            
            this.list = function(searchQuery){
                
            }
            
            this.group = function(searchQuery){
                
            }
            
            this.facets = function(facetQuery){
                
                if(facetQuery) {
                    
                    var queryFacetsParameters = {
                        'q'             : facetQuery.q,
                        'fl'            : '', 
                        'wt'            : 'json',
                        'rows'          : 0,
                        'facet'         : true,
                        'facet.field'   :facetQuery.f,
                        'facet.limit'   : 512,
                        'facet.mincount': 1
                    };
                   var queryAction =  $http.get('/api/v2013/index/select', { params: queryFacetsParameters });
                   $q.when(queryAction)
                    .then(function (data) {
                        var facets = {};
                        _.each(facetQuery.f, function(facet){
                            facets[facet] = readFacets2(data.data.facet_counts.facet_fields[facet]);
                        });
                        console.log(facets);
                        return facets;
                    })
                }                
           }
           
           
           /////////////////////////////////////
           //       internal functions        //
           ////////////////////////////////////
           function readFacets2(solrArray) {

                var facets = [];
                if(solrArray){
                    for (var i = 0; i < solrArray.length; i += 2) {
    
                        var facet = solrArray[i];
    
                        facets.push({ symbol: facet, title: facet, count: solrArray[i + 1] });
                    }
                }
                return facets;
           };
        }
    }]);
});

@@ -178,59 +178,1070 @@
     
-this.list = function(searchQuery)%7B%0A                
+var searchDefaults = %7B%0A                currentPage     : 0,%0A                rowsPerPage     : 25,%0A                sort            : 'createdDate_dt desc',%0A                fields          : 'title_t, description_t',%0A                query           : '*:*',%0A                %0A                groupSort       :   'government_EN_t',%0A                groupField      :   'government_EN_t asc'%0A            %7D%0A            %0A            this.list = function(searchQuery, queryCanceler)%7B%0A               %0A               _.defaults(searchQuery, searchDefaults);%0A               %0A               var queryListParameters = %7B%0A                    'q': searchQuery.query,%0A                    'sort': searchQuery.sort,%0A                    'fl': searchQuery.fields,%0A                    'wt': 'json',%0A                    'start': searchQuery.currentPage * searchQuery.rowsPerPage,%0A                    'rows': searchQuery.rowsPerPage,%0A                %7D;%0A                %0A                return $http.get('/api/v2013/index/select', %7B params: queryListParameters, timeout: queryCanceler %7D);
 %0A   
@@ -1305,24 +1305,39 @@
 (searchQuery
+, queryCanceler
 )%7B%0A         
@@ -1336,32 +1336,865 @@
                %0A
+               _.defaults(searchQuery, searchDefaults);%0A               %0A               var queryGroupParameters = %7B%0A                    'q': searchQuery.query + ' AND government_s:*',%0A                    'sort': searchQuery.sort,%0A                    'fl': searchQuery.fields,%0A                    'wt': 'json',%0A                    'start': searchQuery.currentPage * searchQuery.rowsPerPage,%0A                    'rows': searchQuery.rowsPerPage,%0A                    'group': true,%0A                    'group.ngroups' : true,%0A                    'group.field': searchQuery.groupField,%0A                    'group.limit': 1000,%0A                    'group.sort': searchQuery.groupSort%0A                %7D;%0A                %0A                return $http.get('/api/v2013/index/select', %7B params: queryGroupParameters, timeout: queryCanceler %7D);%0A
             %7D%0A  
@@ -2261,32 +2261,103 @@
                %0A
+               _.defaults(facetQuery, searchDefaults);%0A               %0A
                 
@@ -2498,16 +2498,20 @@
 tQuery.q
+uery
 ,%0A      
@@ -2747,16 +2747,21 @@
 tQuery.f
+ields
 ,%0A      
@@ -3147,16 +3147,17 @@
 tQuery.f
+l
 , functi

implement group and list search in serive