Add unmapped_type to sort, remove unused function

This commit is contained in:
Rashid Khan 2015-06-02 09:23:42 -07:00
parent 81d8326c2d
commit 09a54eb9fd
4 changed files with 86 additions and 114 deletions

View file

@ -0,0 +1,32 @@
define(function (require) {
var _ = require('lodash');
return function normalizeSortRequest() {
/**
* Decorate queries with default parameters
* @param {query} query object
* @returns {object}
*/
return function (sortObject) {
if (!_.isArray(sortObject)) sortObject = [sortObject];
var defaultSortOptions = {
// Will cause indices missing the field to use null as sort.
unmapped_type: 'boolean'
};
/*
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;
};
};
});

View file

@ -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) {
@ -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() {
@ -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

View file

@ -1,83 +0,0 @@
define(function (require) {
describe('SearchSource#getNormalizedSort', function () {
require('services/private');
require('angular').module('getNormalizedSort', ['kibana']);
var source;
beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
var SearchSource = Private(require('components/courier/data_source/search_source'));
source = new SearchSource();
}));
[
{
sort: {
starCount: 'asc'
},
normal: {
starCount: 'asc'
}
},
{
sort: {
starCount: 'desc'
},
normal: {
starCount: 'desc'
}
},
{
sort: 'starCount',
normal: {
starCount: 'asc'
}
},
{
sort: [
'starCount'
],
normal: {
starCount: 'asc'
}
},
{
sort: [
'starCount',
'name'
],
normal: {
starCount: 'asc',
name: 'asc'
}
},
{
sort: [
{ name: 'asc' }
],
normal: {
name: 'asc'
}
},
{
sort: [
{ starCount: 'desc' },
{ name: 'asc' }
],
normal: {
starCount: 'desc',
name: 'asc'
}
}
].forEach(function (test) {
it('reads ' + JSON.stringify(test.sort) + ' properly', function () {
source.sort(test.sort);
var normal = source.getNormalizedSort();
expect(normal).to.eql(test.normal);
});
});
});
});

View file

@ -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);
});
});
});