From 43cca69a35aa6ff65e9995adc43ed501536678b5 Mon Sep 17 00:00:00 2001 From: Matt Bargar Date: Tue, 29 Aug 2017 17:07:03 -0400 Subject: [PATCH] Check sort:options for Discover default sort order (#13708) (#13764) * Check sort:options for Discover default sort order I went back and forth on a lot of different solutions for this. Initially I thought it would make sense to just allow users to set a default saved search in Discover. There were some problems with that approach though. It would change the default workflow in Discover. Instead of starting with an unsaved search, users would be editing a saved search by default. I could see this leading to a lot of unintentional changes to the default. The settings from the default saved search also wouldn't carry over to new searches, which I think would be desirable most of the time. I also considered adding a new advanced setting for specifying a default sort field/direction. This kind of setting would make more sense at the index pattern level though. One field may not be valid across all index patterns. So I ended up going with the simplest solution. It solves the issue identified by the author of the linked issue and nothing more. If a sort order is specified in the existing sort:options advanced setting, we'll use that direction when sorting on the index pattern's timestamp field by default. Fixes https://github.com/elastic/kibana/issues/5164 * Create a new advanced setting instead of re-using sort:options * Just pass a default order --- docs/management/advanced-options.asciidoc | 1 + .../kibana/public/discover/controllers/discover.js | 2 +- src/core_plugins/kibana/ui_setting_defaults.js | 6 ++++++ src/ui/public/doc_table/lib/get_sort.js | 8 ++++---- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/management/advanced-options.asciidoc b/docs/management/advanced-options.asciidoc index 43350d03e15e7..3f9f8319fc4cb 100644 --- a/docs/management/advanced-options.asciidoc +++ b/docs/management/advanced-options.asciidoc @@ -36,6 +36,7 @@ adapt to the interval between measurements. Keys are http://en.wikipedia.org/wik document. `discover:sampleSize`:: The number of rows to show in the Discover table. `discover:aggs:terms:size`:: Determines how many terms will be visualized when clicking the "visualize" button, in the field drop downs, in the discover sidebar. The default value is `20`. +`discover:sort:defaultOrder`:: Controls the default sort direction for time based index patterns in the Discover app. `doc_table:highlight`:: Highlight results in Discover and Saved Searches Dashboard. Highlighting makes request slow when working on big documents. Set this property to `false` to disable highlighting. `courier:maxSegmentCount`:: Kibana splits requests in the Discover app into segments to limit the size of requests sent to diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js index 4d06d1e81317b..b6a2ed65a8ebc 100644 --- a/src/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/core_plugins/kibana/public/discover/controllers/discover.js @@ -254,7 +254,7 @@ function discoverController( function getStateDefaults() { return { query: $scope.searchSource.get('query') || { query: '', language: config.get('search:queryLanguage') }, - sort: getSort.array(savedSearch.sort, $scope.indexPattern), + sort: getSort.array(savedSearch.sort, $scope.indexPattern, config.get('discover:sort:defaultOrder')), columns: savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(), index: $scope.indexPattern.id, interval: 'auto', diff --git a/src/core_plugins/kibana/ui_setting_defaults.js b/src/core_plugins/kibana/ui_setting_defaults.js index 36a8b7f26e6d4..b798aaf101005 100644 --- a/src/core_plugins/kibana/ui_setting_defaults.js +++ b/src/core_plugins/kibana/ui_setting_defaults.js @@ -86,6 +86,12 @@ export function getUiSettingDefaults() { description: 'Determines how many terms will be visualized when clicking the "visualize" ' + 'button, in the field drop downs, in the discover sidebar.' }, + 'discover:sort:defaultOrder': { + value: 'desc', + options: ['desc', 'asc'], + type: 'select', + description: 'Controls the default sort direction for time based index patterns in the Discover app.', + }, 'doc_table:highlight': { value: true, description: 'Highlight results in Discover and Saved Searches Dashboard.' + diff --git a/src/ui/public/doc_table/lib/get_sort.js b/src/ui/public/doc_table/lib/get_sort.js index 9d881abeba195..e4749d8439131 100644 --- a/src/ui/public/doc_table/lib/get_sort.js +++ b/src/ui/public/doc_table/lib/get_sort.js @@ -6,7 +6,7 @@ import _ from 'lodash'; * @param {object} indexPattern used for determining default sort * @returns {object} a sort object suitable for returning to elasticsearch */ -export function getSort(sort, indexPattern) { +export function getSort(sort, indexPattern, defaultSortOrder = 'desc') { const sortObj = {}; let field; let direction; @@ -21,7 +21,7 @@ export function getSort(sort, indexPattern) { direction = sort[1]; } else if (indexPattern.timeFieldName && isSortable(indexPattern.timeFieldName)) { field = indexPattern.timeFieldName; - direction = 'desc'; + direction = defaultSortOrder; } if (field) { @@ -35,7 +35,7 @@ export function getSort(sort, indexPattern) { return sortObj; } -getSort.array = function (sort, indexPattern) { - return _(getSort(sort, indexPattern)).pairs().pop(); +getSort.array = function (sort, indexPattern, defaultSortOrder) { + return _(getSort(sort, indexPattern, defaultSortOrder)).pairs().pop(); };