Skip to content

Commit

Permalink
add support for terms_stats facet and more dynamic association of fac…
Browse files Browse the repository at this point in the history
…ets to data series
  • Loading branch information
richard-jones committed Nov 14, 2014
1 parent ed1fbc8 commit 9d98a1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
8 changes: 7 additions & 1 deletion es.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function optionsFromQuery(query) {
}
}

// FIXME: support for statistical facet
// FIXME: support for statistical facet and terms_stats facet
}

if (qs) {
Expand Down Expand Up @@ -299,6 +299,8 @@ function elasticSearchQuery(params) {
facet["geo_distance"]["ranges"] = ranges
} else if (defn.type === "statistical") {
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"]}
}
qs["facets"][defn["field"]] = facet
}
Expand Down Expand Up @@ -387,6 +389,10 @@ function elasticSearchSuccess(callback) {
// handle statistical facets
} else if (facet["_type"] === "statistical") {
resultobj["facets"][item] = facet
// handle terms_stats
} else if (facet["_type"] === "terms_stats") {
var terms = facet["terms"]
resultobj["facets"][item] = terms
}
}
}
Expand Down
45 changes: 31 additions & 14 deletions jquery.reportview.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,22 @@ function renderHorizontalMultiBar(params) {
{
"field" : "<elasticsearch field>" // field upon which to facet
"display" : "<display name>", // display name for the UI
"type": "term|range|geo_distance", // the kind of facet this will be
// terms facet only
"type": "term|range|geo_distance|statistical|terms_stats", // the kind of facet this will be
"facet_label_field" : "<field to use as the label for the value>" // so in a term facet, this would be "term"
"facet_value_field" : "<field to use as the value>" // in a term facet this would be "count", but in a terms_stats facet it could be "total"
"series_function" : <function> // function which takes the facet and returns one or more series
// terms and terms_stats facets only
"size" : <num>, // how many terms should the facet limit to
"order" : "count|reverse_count|term|reverse_term", // which standard ordering to use for facet values
"value_function" : <function>, // function to be called on each value before display
// terms_stats facets only
"value_field" : "<elasticsearch field>" // field on which to compute the statistics
// range facet only
"range" : [ // list of ranges (in order) which define the filters
Expand Down Expand Up @@ -359,6 +367,8 @@ function renderHorizontalMultiBar(params) {
"default_distance_unit" : "km",
"default_distance_lat" : 51.4768, // Greenwich meridian (give or take a few decimal places)
"default_distance_lon" : 0.0, //
"default_facet_label_field" : "term",
"default_facet_value_field" : "count",

// list of filters that will be added to the "must" boolean filter for every request
// should take the form of a set of query elements that can be appended directly
Expand Down Expand Up @@ -409,6 +419,8 @@ function renderHorizontalMultiBar(params) {
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 (!("facet_label_field" in facet)) { facet["facet_label_field"] = provided_options.default_facet_label_field }
if (!("facet_value_field" in facet)) { facet["facet_value_field"] = provided_options.default_facet_value_field }
}

return provided_options
Expand Down Expand Up @@ -464,18 +476,23 @@ function renderHorizontalMultiBar(params) {
facet["values"] = records.slice(0, size)

// now convert the facet values into the data series
var series = {}
series["key"] = facet["display"]
series["values"] = []
for (var i = 0; i < facet["values"].length; i++) {
var result = facet["values"][i]
var display = result.term
if (facet.value_function) {
display = facet.value_function(display)
if (!facet.series_function) {
var series = {};
series["key"] = facet["display"];
series["values"] = [];
for (var i = 0; i < facet["values"].length; i++) {
var result = facet["values"][i]
var display = result[facet.facet_label_field]
if (facet.value_function) {
display = facet.value_function(display)
}
series.values.push({"label": display, "value": result[facet.facet_value_field]})
}
series.values.push({"label" : display, "value" : result.count})
data_series.push(series)
} else {
var custom_series = facet.series_function(options, facet);
data_series = data_series.concat(custom_series)
}
data_series.push(series)
}

// finally, hit the callback
Expand Down

0 comments on commit 9d98a1b

Please sign in to comment.