瀏覽代碼

[search] Add min and max field properties to range facets

Romain Rigaux 11 年之前
父節點
當前提交
0087d49d89

+ 13 - 2
apps/search/src/search/api.py

@@ -54,6 +54,7 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
       
     stats_json = solr_api.stats(collection['name'], [facet_field])
     stat_facet = stats_json['stats']['stats_fields'][facet_field]
+    is_date = False
     
     # to refactor
     if isinstance(stat_facet['min'], numbers.Number):
@@ -79,18 +80,27 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
       if gap < 1:
         gap = 1
     elif 'T' in stat_facet['min']:
+      is_date = True
       stats_min = stat_facet['min']
       stats_max = stat_facet['max']
       if start is None:
         start = stats_min 
       start = re.sub('\.\d\d?\d?Z$', 'Z', start)
-      start_ts = datetime.strptime(start, '%Y-%m-%dT%H:%M:%SZ')
+      try:
+        start_ts = datetime.strptime(start, '%Y-%m-%dT%H:%M:%SZ')
+      except Exception, e:        
+        LOG.error('Bad date: %s' % e)        
+        start_ts = datetime.strptime('1970-01-01T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ')        
       start_ts, _ = _round_date_range(start_ts)
       start = start_ts.strftime('%Y-%m-%dT%H:%M:%SZ')
       if end is None:
         end = stats_max
       end = re.sub('\.\d\d?\d?Z$', 'Z', end)
-      end_ts = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ')
+      try:
+        end_ts = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ')
+      except Exception, e:
+        LOG.error('Bad date: %s' % e)
+        end_ts = datetime.strptime('2050-01-01T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
       _, end_ts = _round_date_range(end_ts)
       end = end_ts.strftime('%Y-%m-%dT%H:%M:%SZ')
       difference = (
@@ -130,6 +140,7 @@ def _guess_range_facet(widget_type, solr_api, collection, facet_field, propertie
       'end': end,
       'gap': gap,
       'canRange': True,
+      'isDate': is_date,
     })
   except Exception, e:
     print e

文件差異過大導致無法顯示
+ 0 - 0
apps/search/src/search/fixtures/initial_search_examples.json


+ 19 - 3
apps/search/src/search/templates/search.mako

@@ -310,7 +310,23 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
 
 <script type="text/html" id="facet-toggle">
     <!-- ko if: type() == 'range' -->
-      <div class="slider-cnt" data-bind="slider: {start: properties.start, end: properties.end, gap: properties.gap, min: properties.min, max: properties.max, handle: 'triangle'}"></div>
+      <!-- ko ifnot: properties.isDate() -->    
+        <div class="slider-cnt" data-bind="slider: {start: properties.start, end: properties.end, gap: properties.gap, min: properties.min, max: properties.max, handle: 'triangle'}"></div>
+      <!-- /ko -->
+      <!-- ko if: properties.isDate() -->
+        ${ _('Start') }:
+        <input type="text" class="input-large" data-bind="value: properties.start" />
+        <br/>
+        ${ _('End') }: <input type="text" class="input-large" data-bind="value: properties.end" />
+        <br/>
+        ${ _('Gap') }: <input type="text" class="input-small" data-bind="value: properties.gap" />
+        <br/>
+        ${ _('Min') }:
+        <input type="text" class="input-medium" data-bind="value: properties.min" />
+        <br/>
+        ${ _('Max') }: <input type="text" class="input-medium" data-bind="value: properties.max" />
+        <br/>
+      <!-- /ko -->
     <!-- /ko -->
     <!-- ko if: type() == 'field' -->
       ${ _('Limit') }: <input type="text" class="input-medium" data-bind="value: properties.limit" />
@@ -865,7 +881,7 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
   <div class="modal-body">
     <a href="javascript: void(0)" data-dismiss="modal" data-bind="click: addFacetDemiModalFieldCancel" class="pull-right"><i class="fa fa-times"></i></a>
     <div style="float: left; margin-right: 10px;text-align: center">
-      <input id="addFacetInput" type="text" data-bind="value: $root.collection.template.fieldsModalFilter, valueUpdate:'afterkeydown'" placeholder="${_('Filter fields')}" class="input" style="float: left" /><br/>
+      <input id="addFacetInput" type="text" data-bind="clearable: $root.collection.template.fieldsModalFilter, valueUpdate:'afterkeydown'" placeholder="${_('Filter fields')}" class="input" style="float: left" /><br/>
     </div>
     <div>
       <ul data-bind="foreach: $root.collection.template.filteredModalFields().sort(function (l, r) { return l.name() > r.name() ? 1 : -1 }), visible: $root.collection.template.filteredModalFields().length > 0"
@@ -875,7 +891,7 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
           </span>
         </li>
       </ul>
-      <div class="alert alert-info inline" data-bind="visible: $root.collection.template.filteredModalFields().length == 0" style="margin-left: 124px;height: 42px;line-height: 42px">
+      <div class="alert alert-info inline" data-bind="visible: $root.collection.template.filteredModalFields().length == 0" style="margin-left: 250px;margin-right: 50px; height: 42px;line-height: 42px">
         ${_('There are no fields matching your search term.')}
       </div>
     </div>

+ 5 - 4
apps/search/src/search/views.py

@@ -382,6 +382,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'stacked': False,
     'limit': 10,
     'mincount': 0,
+    'isDate': False,
     'andUp': False,  # Not used yet
   }
   
@@ -411,7 +412,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
   }
 
 def get_range_facet(request):  
-  result = {'status': -1, 'message': 'Error'}
+  result = {'status': -1, 'message': ''}
 
   try:
     collection = json.loads(request.POST.get('collection', '{}')) # Perms
@@ -435,7 +436,7 @@ def get_range_facet(request):
 
 
 def get_collection(request):  
-  result = {'status': -1, 'message': 'Error'}
+  result = {'status': -1, 'message': ''}
 
   try:
     name = request.POST['name']
@@ -453,7 +454,7 @@ def get_collection(request):
 
 
 def get_collections(request):  
-  result = {'status': -1, 'message': 'Error'}
+  result = {'status': -1, 'message': ''}
 
   try:           
     result['collection'] = SearchController(request.user).get_all_indexes()
@@ -473,7 +474,7 @@ def install_examples(request):
   else:
     try:
       search_setup.Command().handle_noargs()
-      indexer_setup.Command().handle_noargs()
+      #indexer_setup.Command().handle_noargs()
       result['status'] = 0
     except Exception, e:
       LOG.exception(e)

+ 1 - 1
apps/search/static/js/search.ko.js

@@ -432,7 +432,7 @@ var Collection = function (vm, collection) {
       }, function (data) {
         if (data.status == 0) {
           var facet = ko.mapping.fromJS(data.facet);
-          facet.field.subscribe(function () {
+          facet.field.subscribe(function() {
             vm.search();
           });      
           self.facets.push(facet);

部分文件因文件數量過多而無法顯示