revert changes to get_sort

and also remove the scripted sorting tests
This commit is contained in:
Joe Fleming 2015-06-10 14:41:53 -07:00
parent 48b139ec6b
commit b4c4d7ee20
2 changed files with 15 additions and 63 deletions

View file

@ -5,57 +5,39 @@ define(function (require) {
* Take a sorting array and make it into an object
* @param {array} 2 item array [fieldToSort, directionToSort]
* @param {object} indexPattern used for determining default sort
* @param {boolean} asDefaultSort always return an object as { fieldName: direction }
* @returns {object} a sort object suitable for returning to elasticsearch
*/
function getSort(sort, indexPattern, asDefaultSort) {
function getSort(sort, indexPattern) {
var sortObj = {};
var field, direction;
function useTimeField() {
var timeField = (indexPattern.timeFieldName && indexPattern.fields.byName[indexPattern.timeFieldName]);
if (timeField && timeField.sortable) {
field = timeField;
direction = 'desc';
} else {
field = direction = undefined;
}
function isSortable(field) {
return (indexPattern.fields.byName[field] && indexPattern.fields.byName[field].sortable);
}
if (_.isArray(sort) && sort.length === 2) {
field = indexPattern.fields.byName[sort[0]];
if (field && field.sortable) {
// At some point we need to refact the sorting logic, this array sucks.
direction = sort[1];
} else {
useTimeField();
}
} else {
useTimeField();
if (_.isArray(sort) && sort.length === 2 && isSortable(sort[0])) {
// At some point we need to refact the sorting logic, this array sucks.
field = sort[0];
direction = sort[1];
} else if (indexPattern.timeFieldName && isSortable(indexPattern.timeFieldName)) {
field = indexPattern.timeFieldName;
direction = 'desc';
}
if (field) {
// sorting on a scripted field requires the script value
if (field.scripted && !asDefaultSort) {
sortObj._script = {
script: field.script,
type: field.type,
order: direction
};
} else {
sortObj[field.name] = direction;
}
sortObj[field] = direction;
} else {
sortObj._score = 'desc';
}
return sortObj;
}
getSort.array = function (sort, indexPattern) {
return _(getSort(sort, indexPattern, true)).pairs().pop();
return _(getSort(sort, indexPattern)).pairs().pop();
};
return getSort;
});
});

View file

@ -43,32 +43,6 @@ define(function (require) {
expect(getSort(['foo'], indexPattern)).to.eql({_score: 'desc'});
expect(getSort({foo: 'bar'}, indexPattern)).to.eql({_score: 'desc'});
});
it('should provide script based sorting for scripted fields', function () {
expect(getSort(['script number', 'asc'], indexPattern)).to.eql({
_script: {
script: '1234',
type: 'number',
order: 'asc'
}
});
expect(getSort(['script string', 'asc'], indexPattern)).to.eql({
_script: {
script: '\'i am a string\'',
type: 'string',
order: 'asc'
}
});
});
it('should mimic normal sorting given a third truthy parameter', function () {
expect(getSort(['script number', 'asc'], indexPattern, true)).to.eql({ 'script number': 'asc' });
});
it('should sort by the default when passed an unsortable scripted field', function () {
expect(getSort(['script murmur3', 'asc'], indexPattern)).to.eql(defaultSort);
});
});
describe('getSort.array function', function () {
@ -79,10 +53,6 @@ define(function (require) {
it('should return an array for sortable fields', function () {
expect(getSort.array(['bytes', 'desc'], indexPattern)).to.eql([ 'bytes', 'desc' ]);
});
it('should return an array for scripted fields', function () {
expect(getSort.array(['script string', 'asc'], indexPattern)).to.eql([ 'script string', 'asc' ]);
});
});
});
});