Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sorting when index is missing sort field #4061

Merged
merged 3 commits into from
Jun 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/kibana/components/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ define(function () {
description: 'Options for the lucene query string parser',
type: 'json'
},
'sort:options': {
value: '{ "unmapped_type": "boolean" }',
description: 'Options the Elasticsearch sort parameter',
type: 'json'
},
'dateFormat': {
value: 'MMMM Do YYYY, HH:mm:ss.SSS',
description: 'When displaying a pretty formatted date, use this format',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define(function (require) {
var _ = require('lodash');
return function normalizeSortRequest(config) {
/**
* Decorate queries with default parameters
* @param {query} query object
* @returns {object}
*/
return function (sortObject) {
if (!_.isArray(sortObject)) sortObject = [sortObject];
var defaultSortOptions = config.get('sort:options');

/*
Normalize the sort description to the more verbose format:
{ someField: "desc" } into { someField: { "order": "desc"}}
*/
_.each(sortObject, function (sortable) {
var sortField = _.keys(sortable)[0];
var sortValue = sortable[sortField];
if (_.isString(sortValue)) {
sortValue = sortable[sortField] = { order: sortValue };
}
_.defaults(sortValue, defaultSortOptions);
});
return sortObject;
};
};
});

37 changes: 6 additions & 31 deletions src/kibana/components/courier/data_source/search_source.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
define(function (require) {


return function SearchSourceFactory(Promise, Private) {
var _ = require('lodash');
var SourceAbstract = Private(require('components/courier/data_source/_abstract'));
var SearchRequest = Private(require('components/courier/fetch/request/search'));
var SegmentedRequest = Private(require('components/courier/fetch/request/segmented'));
var normalizeSortRequest = Private(require('components/courier/data_source/_normalize_sort_request'));


_(SearchSource).inherits(SourceAbstract);
function SearchSource(initialState) {
Expand Down Expand Up @@ -92,37 +95,6 @@ define(function (require) {
this._fetchDisabled = false;
};

/**
* Special reader function for sort, which will transform the sort syntax into a simple
* map of `field: dir`
*/
SearchSource.prototype.getNormalizedSort = function () {
var sort = this.get('sort');
if (!sort) return;

var normal = {};

(function read(lvl) {
if (_.isString(lvl)) {
normal[lvl] = 'asc';
}
else if (_.isArray(lvl)) {
_.forEach(lvl, read);
}
else if (_.isObject(lvl)) {
_.forOwn(lvl, function (dir, field) {
if (_.isObject(dir)) {
normal[field] = dir.dir || 'asc';
} else {
normal[field] = String(dir);
}
});
}
}(sort));

return normal;
};

SearchSource.prototype.onBeginSegmentedFetch = function (initFunction) {
var self = this;
return Promise.try(function addRequest() {
Expand Down Expand Up @@ -206,6 +178,9 @@ define(function (require) {
case 'source':
key = '_source';
/* fall through */
case 'sort':
val = normalizeSortRequest(val);
/* fall through */
default:
state.body = state.body || {};
// ignore if we already have a value
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
define(function (require) {
describe('SearchSource#normalizeSortRequest', function () {
require('services/private');
require('angular').module('normalizeSortRequest', ['kibana']);

var normalizeSortRequest;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
normalizeSortRequest = Private(require('components/courier/data_source/_normalize_sort_request'));
}));

var normalizedSort = [{
someField: {
order: 'desc',
unmapped_type: 'boolean'
}
}];

it('make sure sort is an array', function () {
var result = normalizeSortRequest(
{ someField: 'desc'}
);
expect(result).to.be.an(Array);
expect(result).to.eql(normalizedSort);
});

it('makes plain string sort into the more verbose format', function () {
var result = normalizeSortRequest(
[{ someField: 'desc'}]
);
expect(result).to.eql(normalizedSort);
});

it('appends default sort options', function () {
var result = normalizeSortRequest(
[{
someField: {
order: 'desc',
unmapped_type: 'boolean'
}
}]
);
expect(result).to.eql(normalizedSort);
});

});
});