diff --git a/bootstrap2.facetview.theme.js b/bootstrap2.facetview.theme.js index c40efa5..9da534a 100644 --- a/bootstrap2.facetview.theme.js +++ b/bootstrap2.facetview.theme.js @@ -189,6 +189,8 @@ function facetList(options) { thefilters += options.render_range_facet(facet, options) } else if (type === "geo_distance") { thefilters += options.render_geo_facet(facet, options) + } else if (type == "date_histogram") { + thefilters += options.render_date_histogram_facet(facet, options) } // FIXME: statistical facet and terms_stats facet? }; @@ -210,19 +212,27 @@ function renderTermsFacet(facet, options) { * id: facetview_or_ - id of anchor for changing AND/OR operator * * each anchor must also have href="" + * + * should (not must) respect the following config + * + * facet.controls - whether the size/sort/bool controls should be shown */ // full template for the facet - we'll then go on and do some find and replace var filterTmpl = ' \ \ + '; + } + + filterTmpl += ' \
{{FILTER_DISPLAY}} \ - \ -
'; // put the name of the field into FILTER_NAME and FILTER_EXACT @@ -297,6 +307,36 @@ function renderGeoFacet(facet, options) { return filterTmpl } +function renderDateHistogramFacet(facet, options) { + /***************************************** + * overrides must provide the following classes and ids + * + * id: facetview_filter_ - table for the specific filter + * + * each anchor must also have href="" + */ + + // full template for the facet - we'll then go on and do some find and replace + var filterTmpl = ' \ + \ +
{{FILTER_DISPLAY}} \ + \ +
'; + + // put the name of the field into FILTER_NAME and FILTER_EXACT + filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']); + + // set the display name of the facet in FILTER_DISPLAY + if ('display' in facet) { + filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']); + } else { + filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']); + }; + + return filterTmpl +} + function renderTermsFacetValues(options, facet) { /***************************************** * overrides must provide the following classes and ids @@ -403,7 +443,7 @@ function renderRangeFacetValues(options, facet) { // render the active filter if there is one if (options.selected_filters_in_facet && selected_range) { var range = getRangeForValue(selected_range, facet) - already_selected = true + var already_selected = true var data_to = range.to ? " data-to='" + range.to + "' " : "" var data_from = range.from ? " data-from='" + range.from + "' " : "" @@ -487,7 +527,7 @@ function renderGeoFacetValues(options, facet) { // render the active filter if there is one if (options.selected_filters_in_facet && selected_geo) { var range = getRangeForValue(selected_geo, facet) - already_selected = true + var already_selected = true var data_to = range.to ? " data-to='" + range.to + "' " : "" var data_from = range.from ? " data-from='" + range.from + "' " : "" @@ -518,6 +558,76 @@ function renderGeoFacetValues(options, facet) { return frag } +function renderDateHistogramValues(options, facet) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filtervalue - wrapper element for any value included in the list + * class: facetview_filterselected - for any anchors around selected filters + * class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value) + * class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field + * + * should (not must) respect the following config + * + * options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom) + * options.render_facet_result - function which renders the individual facets + */ + + var selected_range = options.active_filters[facet.field]; + var frag = ""; + + // render the active filter if there is one + if (options.selected_filters_in_facet && selected_range) { + var from = selected_range.from; + var data_from = " data-from='" + from + "' "; + var display = from; + if (facet.value_function) { + display = facet.value_function(display); + } + + var sf = ''; + sf += "" + display + " "; + sf += ''; + sf += ""; + frag += sf; + + // if a range is already selected, we don't render any more + return frag + } + + // then render the remaining selectable facets if necessary + + // get the facet values in the right order for display + var values = facet["values"]; + if (facet.sort === "desc") { + values.reverse() + } + + for (var i = 0; i < values.length; i++) { + var f = values[i]; + if (f) { + if (f.count === 0 && facet.hide_empty_date_bin) { + continue + } + var next = false; + if (facet.sort === "asc") { + if (i + 1 < values.length) { + next = values[i + 1] + } + } else if (facet.sort === "desc") { + if (i - 1 >= 0) { + next = values[i - 1]; + } + } + + var append = options.render_date_histogram_result(options, facet, f, next); + frag += append + } + } + + return frag +} + function renderTermsFacetResult(options, facet, result, selected_filters) { /***************************************** * overrides must provide the following classes and ids @@ -573,6 +683,28 @@ function renderGeoFacetResult(options, facet, result, range) { return append } +function renderDateHistogramResult(options, facet, result, next) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filtervalue - tags the top level element as being a facet result + * class: facetview_filterchoice - tags the anchor wrapped around the name of the field + */ + + var data_from = result.time ? " data-from='" + result.time + "' " : ""; + var data_to = next ? " data-to='" + next.time + "' " : ""; + + var display = result.time; + if (facet.value_function) { + display = facet.value_function(display) + } + + var append = '' + display + '' + + ' (' + result.count + ')'; + return append +} + function searchingNotification(options) { return "SEARCHING..." } @@ -907,6 +1039,47 @@ function renderActiveGeoFilter(options, facet, field, value) { return frag } +function renderActiveDateHistogramFilter(options, facet, field, value) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filterselected - anchor tag for any clickable filter selection + * class: facetview_clear - anchor tag for any link which will remove the filter (should also provide data-value and data-field) + * class: facetview_inactive_link - any link combined with facetview_filterselected which should not execute when clicked + * + * should (not must) respect the config + * + * options.show_filter_field - whether to include the name of the field the filter is active on + */ + + var clean = safeId(field); + var display = facet.display ? facet.display : facet.field; + + var frag = "
"; + + if (options.show_filter_field) { + frag += ''; + frag += '' + display + ''; + frag += "" + } + + var data_from = value.from ? " data-from='" + value.from + "' " : ""; + + var valdisp = value.from; + if (facet.value_function) { + valdisp = facet.value_function(valdisp); + } + + frag += '' + frag += '' + valdisp + ' ' + frag += "" + + frag += "
" + + return frag +} + ///// behaviour functions ////////////////////////// // called when searching begins. Use it to show the loading bar, or something @@ -1042,6 +1215,8 @@ function setUISelectedFilters(options, context) { frag += options.render_active_range_filter(options, facet, field, filter_list) } else if (facet.type === "geo_distance") { frag += options.render_active_geo_filter(options, facet, field, filter_list) + } else if (facet.type === "date_histogram") { + frag += options.render_active_date_histogram_filter(options, facet, field, filter_list) } // FIXME: statistical facet? } diff --git a/bootstrap3.facetview.theme.js b/bootstrap3.facetview.theme.js index 543c327..856c50f 100644 --- a/bootstrap3.facetview.theme.js +++ b/bootstrap3.facetview.theme.js @@ -202,13 +202,15 @@ function facetList(options) { thefilters += options.render_range_facet(facet, options) } else if (type === "geo_distance") { thefilters += options.render_geo_facet(facet, options) + } else if (type == "date_histogram") { + thefilters += options.render_date_histogram_facet(facet, options) } // FIXME: statistical facet and terms_stats facet? - }; + } return thefilters - }; + } return "" -}; +} function renderTermsFacet(facet, options) { /***************************************** @@ -223,14 +225,20 @@ function renderTermsFacet(facet, options) { * id: facetview_or_ - id of anchor for changing AND/OR operator * * each anchor must also have href="" + * + * should (not must) respect the following config + * + * facet.controls - whether the size/sort/bool controls should be shown */ // full template for the facet - we'll then go on and do some find and replace var filterTmpl = ' \ \ + '; + } + + filterTmpl += ' \
{{FILTER_DISPLAY}} \ - \ - \ + '; + + if (facet.controls) { + filterTmpl += '\
\
\ \ @@ -238,8 +246,10 @@ function renderTermsFacet(facet, options) { \
\
\ -
\ -
'; // put the name of the field into FILTER_NAME and FILTER_EXACT @@ -314,6 +324,36 @@ function renderGeoFacet(facet, options) { return filterTmpl } +function renderDateHistogramFacet(facet, options) { + /***************************************** + * overrides must provide the following classes and ids + * + * id: facetview_filter_ - table for the specific filter + * + * each anchor must also have href="" + */ + + // full template for the facet - we'll then go on and do some find and replace + var filterTmpl = ' \ + \ +
{{FILTER_DISPLAY}} \ + \ +
'; + + // put the name of the field into FILTER_NAME and FILTER_EXACT + filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']); + + // set the display name of the facet in FILTER_DISPLAY + if ('display' in facet) { + filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']); + } else { + filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']); + }; + + return filterTmpl +} + function renderTermsFacetValues(options, facet) { /***************************************** * overrides must provide the following classes and ids @@ -509,9 +549,62 @@ function renderGeoFacetValues(options, facet) { var data_to = range.to ? " data-to='" + range.to + "' " : ""; var data_from = range.from ? " data-from='" + range.from + "' " : ""; + var sf = '' + sf += "" + range.display + " " + sf += '' + sf += "" + frag += sf + + // if a range is already selected, we don't render any more + return frag + } + + // then render the remaining selectable facets if necessary + for (var i=0; i < facet["distance"].length; i=i+1) { + var r = facet["distance"][i] + var f = getValueForRange(r, facet["values"]) + if (f) { + if (f.count === 0 && facet.hide_empty_distance) { + continue + } + var append = options.render_geo_facet_result(options, facet, f, r) + frag += append + } + } + + return frag +} + +function renderDateHistogramValues(options, facet) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filtervalue - wrapper element for any value included in the list + * class: facetview_filterselected - for any anchors around selected filters + * class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value) + * class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field + * + * should (not must) respect the following config + * + * options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom) + * options.render_facet_result - function which renders the individual facets + */ + + var selected_range = options.active_filters[facet.field]; + var frag = ""; + + // render the active filter if there is one + if (options.selected_filters_in_facet && selected_range) { + var from = selected_range.from; + var data_from = " data-from='" + from + "' "; + var display = from; + if (facet.value_function) { + display = facet.value_function(display); + } + var sf = ''; - sf += "" + range.display + " "; - sf += ''; + sf += "" + display + " "; + sf += ''; sf += ""; frag += sf; @@ -520,14 +613,31 @@ function renderGeoFacetValues(options, facet) { } // then render the remaining selectable facets if necessary - for (var i=0; i < facet["distance"].length; i=i+1) { - var r = facet["distance"][i]; - var f = getValueForRange(r, facet["values"]); + + // get the facet values in the right order for display + var values = facet["values"]; + if (facet.sort === "desc") { + values.reverse() + } + + for (var i = 0; i < values.length; i++) { + var f = values[i]; if (f) { - if (f.count === 0 && facet.hide_empty_distance) { + if (f.count === 0 && facet.hide_empty_date_bin) { continue } - var append = options.render_geo_facet_result(options, facet, f, r); + var next = false; + if (facet.sort === "asc") { + if (i + 1 < values.length) { + next = values[i + 1] + } + } else if (facet.sort === "desc") { + if (i - 1 >= 0) { + next = values[i - 1]; + } + } + + var append = options.render_date_histogram_result(options, facet, f, next); frag += append } } @@ -590,6 +700,28 @@ function renderGeoFacetResult(options, facet, result, range) { return append } +function renderDateHistogramResult(options, facet, result, next) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filtervalue - tags the top level element as being a facet result + * class: facetview_filterchoice - tags the anchor wrapped around the name of the field + */ + + var data_from = result.time ? " data-from='" + result.time + "' " : ""; + var data_to = next ? " data-to='" + next.time + "' " : ""; + + var display = result.time; + if (facet.value_function) { + display = facet.value_function(display) + } + + var append = '' + display + '' + + ' (' + result.count + ')'; + return append +} + function searchingNotification(options) { return "SEARCHING..." } @@ -898,6 +1030,45 @@ function renderActiveGeoFilter(options, facet, field, value) { } } + var clean = safeId(field) + var display = facet.display ? facet.display : facet.field + + var frag = "
" + + if (options.show_filter_field) { + frag += '" + } + + var range = getRangeForValue(value, facet) + + var data_to = value.to ? " data-to='" + value.to + "' " : "" + var data_from = value.from ? " data-from='" + value.from + "' " : "" + + frag += '" + + frag += "
" + + return frag +} + +function renderActiveDateHistogramFilter(options, facet, field, value) { + /***************************************** + * overrides must provide the following classes and ids + * + * class: facetview_filterselected - anchor tag for any clickable filter selection + * class: facetview_clear - anchor tag for any link which will remove the filter (should also provide data-value and data-field) + * class: facetview_inactive_link - any link combined with facetview_filterselected which should not execute when clicked + * + * should (not must) respect the config + * + * options.show_filter_field - whether to include the name of the field the filter is active on + */ + var clean = safeId(field); var display = facet.display ? facet.display : facet.field; @@ -909,17 +1080,19 @@ function renderActiveGeoFilter(options, facet, field, value) { frag += "" } - var range = getRangeForValue(value, facet); - - var data_to = value.to ? " data-to='" + value.to + "' " : ""; var data_from = value.from ? " data-from='" + value.from + "' " : ""; - frag += '"; + var valdisp = value.from; + if (facet.value_function) { + valdisp = facet.value_function(valdisp); + } - frag += ""; + frag += '" + + frag += "" return frag } @@ -1059,6 +1232,8 @@ function setUISelectedFilters(options, context) { frag += options.render_active_range_filter(options, facet, field, filter_list) } else if (facet.type === "geo_distance") { frag += options.render_active_geo_filter(options, facet, field, filter_list) + } else if (facet.type === "date_histogram") { + frag += options.render_active_date_histogram_filter(options, facet, field, filter_list) } // FIXME: statistical facet? } diff --git a/es.js b/es.js index 3233819..6bcd4db 100644 --- a/es.js +++ b/es.js @@ -55,7 +55,7 @@ function optionsFromQuery(query) { // FIXME: note that fields are not supported here // from position - if (query.from) { opts["from"] = query.from } + if (query.hasOwnProperty("from")) { opts["from"] = query.from } // page size if (query.size) { opts["page_size"] = query.size } @@ -112,14 +112,22 @@ function optionsFromQuery(query) { } } - // could be a range query + // could be a range query (which may in turn be a range or a date histogram facet) if ("range" in clause) { - for (var field=0; field < clause.range.length; field=field+1) { - var rq = clause.range[field]; + // get the field that we're ranging on + var r = clause.range; + var fields = Object.keys(r); + var field = false; + if (fields.length > 0) { + field = fields[0]; + } + + if (field) { + var rparams = r[field]; var range = {}; - if (rq.lt) { range["to"] = rq.lt } - if (rq.gte) { range["from"] = rq.gte } - opts["_active_filters"][field] = range + if ("lt" in rparams) { range["to"] = rparams.lt } + if ("gte" in rparams) { range["from"] = rparams.gte } + opts["_active_filters"][field] = range; } } @@ -208,6 +216,14 @@ function getFilters(params) { return gq } + function dateHistogramFilter(facet, value) { + var rq = {"range" : {}}; + rq["range"][facet.field] = {}; + if (value.to) { rq["range"][facet.field]["lt"] = value.to } + if (value.from) { rq["range"][facet.field]["gte"] = value.from } + return rq + } + // function to make the relevant filters from the filter definition function makeFilters(filter_definition) { var filters = []; @@ -220,8 +236,10 @@ function getFilters(params) { filters.push(termsFilter(facet, filter_list)) } else if (facet.type === "range") { filters.push(rangeFilter(facet, filter_list)) - } else if (facet.type == "geo_distance") { + } else if (facet.type === "geo_distance") { filters.push(geoFilter(facet, filter_list)) + } else if (facet.type == "date_histogram") { + filters.push(dateHistogramFilter(facet, filter_list)) } } } @@ -338,6 +356,8 @@ function elasticSearchQuery(params) { facet["statistical"] = {"field" : defn["field"]} } else if (defn.type === "terms_stats") { facet["terms_stats"] = {key_field : defn["field"], value_field: defn["value_field"], size : size, order : defn["order"]} + } else if (defn.type === "date_histogram") { + facet["date_histogram"] = {field : defn["field"], interval : defn["interval"]} } qs["facets"][defn["field"]] = facet } @@ -458,6 +478,9 @@ function elasticSearchSuccess(callback) { } else if (facet["_type"] === "terms_stats") { var terms = facet["terms"]; resultobj["facets"][item] = terms + } else if (facet["_type"] === "date_histogram") { + var entries = facet["entries"] + resultobj["facets"][item] = entries } } } diff --git a/jquery.facetview2.js b/jquery.facetview2.js index 4dd7896..8fd4ad8 100644 --- a/jquery.facetview2.js +++ b/jquery.facetview2.js @@ -270,7 +270,7 @@ function getUrlVars() { { "field" : "" // field upon which to facet "display" : "", // display name for the UI - "type": "term|range|geo_distance|statistical", // the kind of facet this will be + "type": "term|range|geo_distance|statistical|date_histogram", // the kind of facet this will be "open" : true|false, // whether the facet should be open or closed (initially) "hidden" : true|false // whether the facet should be displayed at all (e.g. you may just want the data for a callback) @@ -282,6 +282,7 @@ function getUrlVars() { "deactivate_threshold" : , // number of facet terms below which the facet is disabled "hide_inactive" : true|false, // whether to hide or just disable the facet if below deactivate threshold "value_function" : , // function to be called on each value before display + "controls" : true|false // should the facet sort/size/bool controls be shown? // range facet only @@ -299,7 +300,12 @@ function getUrlVars() { "unit" : "" // unit to calculate distances in (e.g. km or mi) "lat" : // latitude from which to measure distances "lon" : // longitude from which to measure distances - + + // date histogram facet only + "interval" : "year, quarter, month, week, day, hour, minute ,second" // period to use for date histogram + "sort" : "asc|desc", // which ordering to use for date histogram + "hide_empty_date_bin" : true|false // whether to suppress display of date range with no values + // admin use only "values" : // the values associated with a successful query on this facet @@ -321,12 +327,17 @@ function getUrlVars() { "default_facet_order" : "count", "default_facet_hide_inactive" : false, "default_facet_deactivate_threshold" : 0, // equal to or less than this number will deactivate the facet + "default_facet_controls" : true, "default_hide_empty_range" : true, "default_hide_empty_distance" : true, "default_distance_unit" : "km", "default_distance_lat" : 51.4768, // Greenwich meridian (give or take a few decimal places) "default_distance_lon" : 0.0, // - + "default_date_histogram_interval" : "year", + "default_hide_empty_date_bin" : true, + "default_date_histogram_sort" : "asc", + + ///// search bar configuration ///////////////////////////// // list of options by which the search results can be sorted @@ -432,6 +443,11 @@ function getUrlVars() { "render_geo_facet" : renderGeoFacet, // overall framework for a geo distance facet "render_geo_facet_values" : renderGeoFacetValues, // the list of geo distance facet values "render_geo_facet_result" : renderGeoFacetResult, // individual geo distance facet values + + // render the date histogram facet + "render_date_histogram_facet" : renderDateHistogramFacet, + "render_date_histogram_values" : renderDateHistogramValues, + "render_date_histogram_result" : renderDateHistogramResult, // render any searching notification (which will then be shown/hidden as needed) "render_searching_notification" : searchingNotification, @@ -453,6 +469,9 @@ function getUrlVars() { // render a geo distance filter interface component (e.g. the filter name and the human readable description of the selected range) "render_active_geo_filter" : renderActiveGeoFilter, + + // render a date histogram/range interface component (e.g. the filter name and the human readable description of the selected range) + "render_active_date_histogram_filter" : renderActiveDateHistogramFilter, ///// configs for standard render functions ///////////////////////////// @@ -605,18 +624,22 @@ function getUrlVars() { var facet = provided_options.facets[i]; if (!("type" in facet)) { facet["type"] = provided_options.default_facet_type } if (!("open" in facet)) { facet["open"] = provided_options.default_facet_open } - if (!("hidden" in facet)) { facet["hiddel"] = provided_options.default_facet_hidden } + if (!("hidden" in facet)) { facet["hidden"] = provided_options.default_facet_hidden } if (!("size" in facet)) { facet["size"] = provided_options.default_facet_size } if (!("logic" in facet)) { facet["logic"] = provided_options.default_facet_operator } if (!("order" in facet)) { facet["order"] = provided_options.default_facet_order } if (!("hide_inactive" in facet)) { facet["hide_inactive"] = provided_options.default_facet_hide_inactive } if (!("deactivate_threshold" in facet)) { facet["deactivate_threshold"] = provided_options.default_facet_deactivate_threshold } + if (!("controls" in facet)) { facet["controls"] = provided_options.default_facet_controls } if (!("hide_empty_range" in facet)) { facet["hide_empty_range"] = provided_options.default_hide_empty_range } if (!("hide_empty_distance" in facet)) { facet["hide_empty_distance"] = provided_options.default_hide_empty_distance } if (!("unit" in facet)) { facet["unit"] = provided_options.default_distance_unit } if (!("lat" in facet)) { facet["lat"] = provided_options.default_distance_lat } if (!("lon" in facet)) { facet["lon"] = provided_options.default_distance_lon } if (!("value_function" in facet)) { facet["value_function"] = function(value) { return value } } + if (!("interval" in facet)) { facet["interval"] = provided_options.default_date_histogram_interval } + if (!("hide_empty_date_bin" in facet)) { facet["hide_empty_date_bin"] = provided_options.default_hide_empty_date_bin } + if (!("sort" in facet)) { facet["sort"] = provided_options.default_date_histogram_sort } } return provided_options @@ -896,6 +919,8 @@ function getUrlVars() { frag = options.render_range_facet_values(options, facet) } else if (facet.type === "geo_distance") { frag = options.render_geo_facet_values(options, facet) + } else if (facet.type === "date_histogram") { + frag = options.render_date_histogram_values(options, facet) } // FIXME: how to display statistical facet? if (frag) { @@ -934,6 +959,11 @@ function getUrlVars() { var to = $(this).attr("data-to"); if (from) { value["from"] = from } if (to) { value["to"] = to } + } else if (facet.type === "date_histogram") { + var from = $(this).attr("data-from"); + var to = $(this).attr("data-to"); + if (from) { value["from"] = from } + if (to) { value["to"] = to } } // FIXME: how to handle clicks on statistical facet (if that even makes sense) or terms_stats facet @@ -968,7 +998,11 @@ function getUrlVars() { } else if (facet.type === "geo_distance") { // NOTE: we are implicitly stating that geo distance range filters cannot be OR'd options.active_filters[field] = value + } else if (facet.type === "date_histogram") { + // NOTE: we are implicitly stating that date histogram filters cannot be OR'd + options.active_filters[field] = value } + // FIXME: statistical facet support here? } @@ -985,6 +1019,8 @@ function getUrlVars() { delete options.active_filters[field] } else if (facet.type === "geo_distance") { delete options.active_filters[field] + } else if (facet.type === "date_histogram") { + delete options.active_filters[field] } // FIXME: statistical facet support? } @@ -1011,6 +1047,8 @@ function getUrlVars() { var to = $(this).attr("data-to"); if (from) { value["from"] = from } if (to) { value["to"] = to } + } else if (facet.type == "date_histogram") { + value = $(this).attr("data-from"); } // FIXMe: statistical facet @@ -1040,8 +1078,8 @@ function getUrlVars() { } else if (facet.type === "range") { // range facet becomes deactivated if there is a count of 0 in every value var view = false; - for (var i=0; i < facet.values.length; i=i+1) { - var val = facet.values[i]; + for (var i=0; i < values.length; i=i+1) { + var val = values[i]; if (val.count > 0) { view = true; break @@ -1051,8 +1089,19 @@ function getUrlVars() { } else if (facet.type === "geo_distance") { // distance facet becomes deactivated if there is a count of 0 in every value var view = false; - for (var i=0; i < facet.values.length; i=i+1) { - var val = facet.values[i]; + for (var i=0; i < values.length; i=i+1) { + var val = values[i]; + if (val.count > 0) { + view = true; + break + } + } + visible = view + } else if (facet.type === "date_histogram") { + // date histogram facet becomes deactivated if there is a count of 0 in every value + var view = false; + for (var i=0; i < values.length; i=i+1) { + var val = values[i]; if (val.count > 0) { view = true; break @@ -1154,17 +1203,21 @@ function getUrlVars() { // get the facet, the field name and the size var facet = options.facets[each]; var field = facet['field']; - var size = facet["size"] ? facet["size"] : options.default_facet_size + var size = facet.hasOwnProperty("size") ? facet["size"] : options.default_facet_size; // get the records to be displayed, limited by the size and record against // the options object var records = results["facets"][field]; - // special rule for handling statistical facets - if (records.hasOwnProperty("_type") && records["_type"] == "statistical") { + // special rule for handling statistical, range and histogram facets + if (records.hasOwnProperty("_type") && records["_type"] === "statistical") { facet["values"] = records } else { if (!records) { records = [] } - facet["values"] = records.slice(0, size) + if (size) { // this means you can set the size of a facet to something false (like, false, or 0, and size will be ignored) + facet["values"] = records.slice(0, size) + } else { + facet["values"] = records + } } // set the results on the page @@ -1223,7 +1276,7 @@ function getUrlVars() { } // augment the URL bar if possible, and the share/save link - urlFromOptions() + urlFromOptions(); // issue the query to elasticsearch doElasticSearchQuery({