Browse Source

[search] Also display default facets

Romain Rigaux 12 years ago
parent
commit
551cfe9

+ 11 - 8
apps/search/src/search/api.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -- coding: utf-8 --
 # Licensed to Cloudera, Inc. under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -41,11 +42,12 @@ class SolrApi(object):
 
   def query(self, solr_query, hue_core):
     try:
-      params = (('q', solr_query['q'] or '*'),
-                ('wt', 'json'),
-                ('rows', solr_query['rows']),
-                ('start', solr_query['start']),
-             )
+      params = (
+          ('q', solr_query['q'] or '*'),
+          ('wt', 'json'),
+          ('rows', solr_query['rows']),
+          ('start', solr_query['start']),
+      )
 
       params += hue_core.get_query()
 
@@ -65,9 +67,10 @@ class SolrApi(object):
 
   def suggest(self, solr_query, hue_core):
     try:
-      params = (('q', solr_query['q']),
-                ('wt', 'json'),
-             )
+      params = (
+          ('q', solr_query['q']),
+          ('wt', 'json'),
+      )
       response = self._root.get('%(core)s/suggest' % solr_query, params)
       return json.loads(response)
     except RestException, e:

+ 20 - 3
apps/search/src/search/models.py

@@ -426,6 +426,8 @@ def augment_solr_response(response, facets):
   augmented['normalized_facets'] = []
 
   normalized_facets = {}
+  default_facets = []
+
   if response and response.get('facet_counts'):
     if response['facet_counts']['facet_fields']:
       for cat in response['facet_counts']['facet_fields']:
@@ -435,7 +437,11 @@ def augment_solr_response(response, facets):
           'label': get_facet_field_label(cat, 'field', facets),
           'counts': response['facet_counts']['facet_fields'][cat],
         }
-        normalized_facets[get_facet_field_uuid(cat, 'field', facets)] = facet
+        uuid = get_facet_field_uuid(cat, 'field', facets)
+        if uuid == '':
+          default_facets.append(facet)
+        else:
+          normalized_facets[uuid] = facet
 
     if response['facet_counts']['facet_ranges']:
       for cat in response['facet_counts']['facet_ranges']:
@@ -448,7 +454,11 @@ def augment_solr_response(response, facets):
           'end': response['facet_counts']['facet_ranges'][cat]['end'],
           'gap': response['facet_counts']['facet_ranges'][cat]['gap'],
         }
-        normalized_facets[get_facet_field_uuid(cat, 'range', facets)] = facet
+        uuid = get_facet_field_uuid(cat, 'range', facets)
+        if uuid == '':
+          default_facets.append(facet)
+        else:
+          normalized_facets[uuid] = facet
 
     if response['facet_counts']['facet_dates']:
       for cat in response['facet_counts']['facet_dates']:
@@ -466,13 +476,20 @@ def augment_solr_response(response, facets):
             counts.append(date)
             counts.append(count)
         facet['counts'] = counts
-        normalized_facets[get_facet_field_uuid(cat, 'date', facets)] = facet
+
+        uuid = get_facet_field_uuid(cat, 'date', facets)
+        if uuid == '':
+          default_facets.append(facet)
+        else:
+          normalized_facets[uuid] = facet
 
   for ordered_uuid in facets.get('order', []):
     try:
       augmented['normalized_facets'].append(normalized_facets[ordered_uuid])
     except:
       pass
+  if default_facets:
+    augmented['normalized_facets'].extend(default_facets)
 
   return augmented
 

+ 13 - 7
apps/search/src/search/templates/admin_core_template.mako

@@ -180,9 +180,9 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                   <option title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" value="{{#datetime}} {{/datetime}}">{{#datetime}}</option>
                   <option title="${ _('Formats a date in the full format') }" value="{{#fulldate}} {{/fulldate}}">{{#fulldate}}</option>
                   <option title="${ _('Formats a date as a Unix timestamp') }" value="{{#timestamp}} {{/timestamp}}">{{#timestamp}}</option>
-                  <option title="${ _('Shows the relative time') }" value="{{#fromnow}} {{/fromnow}}">{{#fromnow}}</option>
-                  <option title="${ _('Downloads the linked file') }" value="{{#downloadfile}} {{/downloadfile}}">{{#downloadfile}}</option>
-                  <option title="${ _('Links to the file') }" value="{{#viewfile}} {{/viewfile}}">{{#viewfile}}</option>
+                  <option title="${ _('Downloads and embed the file in the browser') }" value="{{#embeddeddownload}} {{/embeddeddownload}}">{{#embeddeddownload}}</option>
+                  <option title="${ _('Downloads the linked file') }" value="{{#download}} {{/download}}">{{#download}}</option>
+                  <option title="${ _('Preview file in File Browser') }" value="{{#preview}} {{/preview}}">{{#preview}}</option>
                 </select>
                 <a title="${ _('Click on this button to add the field') }" class="btn btn-small" data-bind="click: $root.addFunctionToVisual">
                   <i class="icon-plus"></i>
@@ -231,8 +231,9 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                   <option title="${ _('Formats a date in the full format') }" value="{{#fulldate}} {{/fulldate}}">{{#fulldate}}</option>
                   <option title="${ _('Formats a date as a Unix timestamp') }" value="{{#timestamp}} {{/timestamp}}">{{#timestamp}}</option>
                   <option title="${ _('Shows the relative time') }" value="{{#fromnow}} {{/fromnow}}">{{#fromnow}}</option>
-                  <option title="${ _('Downloads the linked file') }" value="{{#downloadfile}} {{/downloadfile}}">{{#downloadfile}}</option>
-                  <option title="${ _('Links to the file') }" value="{{#viewfile}} {{/viewfile}}">{{#viewfile}}</option>
+                  <option title="${ _('Downloads and embed the file in the browser') }" value="{{#embeddeddownload}} {{/embeddeddownload}}">{{#embeddeddownload}}</option>
+                  <option title="${ _('Downloads the linked file') }" value="{{#download}} {{/download}}">{{#download}}</option>
+                  <option title="${ _('Preview file in File Browser') }" value="{{#preview}} {{/preview}}">{{#preview}}</option>
                 </select>
                 <a title="${ _('Click on this button to add the field') }" class="btn btn-small" data-bind="click: $root.addFunctionToSource">
                   <i class="icon-plus"></i>
@@ -559,16 +560,21 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
       if ($(e.target).attr("href") == "#preview") {
         $("#preview-container").empty();
         $(samples).each(function (cnt, item) {
-          item.viewfile = function () {
+          item.preview = function () {
             return function (val) {
               return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
             }
           };
-          item.downloadfile = function () {
+          item.embeddeddownload = function () {
             return function (val) {
               return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
             }
           };
+          item.download = function () {
+            return function (val) {
+              return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '>' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
           item.date = function () {
             return function (val) {
               return genericFormatDate(val, item, "DD-MM-YYYY");

+ 24 - 18
apps/search/src/search/templates/search.mako

@@ -69,22 +69,22 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
     <div class="span2">
       <ul class="facet-list">
         % for fld in response['normalized_facets']:
-        % if fld['type'] == 'date':
+          % if fld['type'] == 'date':
             <li class="nav-header dateFacetHeader">${fld['label']}</li>
-        % else:
+          % else:
             <li class="nav-header">${fld['label']}</li>
-        % endif
+          % endif
 
-        <%
+          <%
           found_value = ""
           for fq in solr_query['fq'].split('|'):
             if fq and fq.split(':')[0] == fld['field']:
-              found_value = fq[fq.find(":")+1:]
+              found_value = fq[fq.find(":") + 1:]
               remove_list = solr_query['fq'].split('|')
               remove_list.remove(fq)
-        %>
+          %>
           % for group, count in macros.pairwise(fld['counts']):
-            %if count > 0 and group != "" and found_value == "":
+            % if count > 0 and group != "" and found_value == "":
               % if fld['type'] == 'field':
                 <li><a href='?collection=${ current_core }&query=${ solr_query['q'] }&fq=${ solr_query['fq'] }|${ fld['field'] }:"${ urllib.quote_plus(group.encode('ascii', 'xmlcharrefreplace')) }"${solr_query.get("sort") and '&sort=' + solr_query.get("sort") or ''}'>${group}</a> <span class="counter">(${ count })</span></li>
               % endif
@@ -94,29 +94,30 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
               % if fld['type'] == 'date':
                 <li class="dateFacetItem"><a href='?collection=${ current_core }&query=${ solr_query['q'] }&fq=${ solr_query['fq'] }|${ fld['field'] }:"${ group }"${solr_query.get("sort") and '&sort=' + solr_query.get("sort") or ''}'><span class="dateFacet">${ group }</span> (${ count })</a></li>
               % endif
-            %endif
+            % endif
             % if found_value != "":
               % if fld['type'] == 'field' and '"' + group + '"' == found_value:
                 <li><strong>${ group }</strong> <a href="?collection=${ current_core }&query=${ solr_query['q'] }&fq=${'|'.join(remove_list)}${solr_query.get("sort") and '&sort=' + solr_query.get("sort") or ''}"><i class="icon-remove"></i></a></li>
-              %endif
+              % endif
               % if fld['type'] == 'range' and '["' + group + '" TO "' + str(int(group) + int(fld['gap']) - 1) + '"]' == found_value:
                 <li><strong>${ group }</strong> <a href="?collection=${ current_core }&query=${ solr_query['q'] }&fq=${'|'.join(remove_list)}${solr_query.get("sort") and '&sort=' + solr_query.get("sort") or ''}"><i class="icon-remove"></i></a></li>
-              %endif
+              % endif
               % if fld['type'] == 'date' and '"' + group + '"' == found_value:
                 <li><strong><span class="dateFacet">${group}</span></strong> <a href="?collection=${ current_core }&query=${ solr_query['q'] }&fq=${'|'.join(remove_list)}${solr_query.get("sort") and '&sort=' + solr_query.get("sort") or ''}"><i class="icon-remove"></i></a></li>
-              %endif
-            %endif
+              % endif
+            % endif
           % endfor
         % endfor
       </ul>
     </div>
     % endif
+
     % if response and response['response']['docs'] and len(response['response']['docs']) > 0:
-      %if response['normalized_facets']:
+      % if response['normalized_facets']:
       <div class="span10">
-      %else:
+      % else:
       <div class="span12">
-      %endif
+      % endif
       <ul class="breadcrumb">
         <li class="pull-right">
           <select class="sort-by">
@@ -129,7 +130,7 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
           if end_record > int(response['response']['numFound']):
             end_record = response['response']['numFound'];
         %>
-        ${_('Showing %s - %s of %s results') % (int(solr_query["start"])+1, end_record, response['response']['numFound'])}
+        ${_('Showing %s - %s of %s results') % (int(solr_query["start"]) + 1, end_record, response['response']['numFound'])}
         </li>
       </ul>
 
@@ -158,16 +159,21 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
         }
 
         $.each(${ json.dumps([result for result in docs]) | n,unicode }, function (index, item) {
-          item.viewfile = function () {
+          item.preview = function () {
             return function (val) {
               return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
             }
           };
-          item.downloadfile = function () {
+          item.embeddeddownload = function () {
             return function (val) {
               return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
             }
           };
+          item.download = function () {
+            return function (val) {
+              return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '>' + $.trim(Mustache.render(val, item)) + '</a>';
+            }
+          };
           item.date = function () {
             return function (val) {
               return genericFormatDate(val, item, "DD-MM-YYYY");