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

Space aware saved objects using only filtering #6

Merged
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
10 changes: 5 additions & 5 deletions src/server/saved_objects/service/lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class SavedObjectsRepository {
* @property {string} [options.search]
* @property {Array<string>} [options.searchFields] - see Elasticsearch Simple Query String
* Query field argument for more information
* @property {object} [options.extraQueryParams] - ES Query parameters to merge/append into the generated query
* @property {object} [options.filters] - ES Query filters to append
* @property {integer} [options.page=1]
* @property {integer} [options.perPage=20]
* @property {string} [options.sortField]
Expand All @@ -229,7 +229,7 @@ export class SavedObjectsRepository {
sortField,
sortOrder,
fields,
extraQueryParams,
filters,
} = options;

if (searchFields && !Array.isArray(searchFields)) {
Expand All @@ -240,8 +240,8 @@ export class SavedObjectsRepository {
throw new TypeError('options.searchFields must be an array');
}

if (extraQueryParams && typeof extraQueryParams !== 'object') {
throw new TypeError('options.extraQueryParams must be an object');
if (filters && !Array.isArray(filters)) {
throw new TypeError('options.filters must be an array');
}

const esOptions = {
Expand All @@ -258,7 +258,7 @@ export class SavedObjectsRepository {
type,
sortField,
sortOrder,
extraQueryParams
filters
})
}
};
Expand Down
15 changes: 13 additions & 2 deletions src/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,25 @@ describe('SavedObjectsRepository', () => {
}
});

it('passes mappings, search, searchFields, type, sortField, extraQueryParams, and sortOrder to getSearchDsl', async () => {
it('requires filters to be an array if defined', async () => {
try {
await savedObjectsRepository.find({ filters: 'string' });
throw new Error('expected find() to reject');
} catch (error) {
sinon.assert.notCalled(callAdminCluster);
sinon.assert.notCalled(onBeforeWrite);
expect(error.message).toMatch('must be an array');
}
});

it('passes mappings, search, searchFields, type, sortField, filters, and sortOrder to getSearchDsl', async () => {
const relevantOpts = {
search: 'foo*',
searchFields: ['foo'],
type: 'bar',
sortField: 'name',
sortOrder: 'desc',
extraQueryParams: { bool: {} },
filters: [{ bool: {} }],
};

await savedObjectsRepository.find(relevantOpts);
Expand Down
46 changes: 10 additions & 36 deletions src/server/saved_objects/service/lib/search_dsl/query_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { defaultsDeep } from 'lodash';
import { getRootPropertiesObjects } from '../../../../mappings';

/**
Expand Down Expand Up @@ -66,23 +65,21 @@ function getFieldsForTypes(searchFields, types) {
* @param {(string|Array<string>)} type
* @param {String} search
* @param {Array<string>} searchFields
* @param {Object} extraQueryParams query parameters to merge into the result
* @param {Array<object>} filters additional query filters
* @return {Object}
*/
export function getQueryParams(mappings, type, search, searchFields, extraQueryParams = {}) {
if (!type && !search) {
return {};
}
export function getQueryParams(mappings, type, search, searchFields, filters = []) {

const bool = {};
const bool = {
filter: [...filters],
};

if (type) {
bool.filter = [{ [Array.isArray(type) ? 'terms' : 'term']: { type } }];
bool.filter.push({ [Array.isArray(type) ? 'terms' : 'term']: { type } });
}

if (search) {
bool.must = [
...bool.must || [],
{
simple_query_string: {
query: search,
Expand All @@ -95,35 +92,12 @@ export function getQueryParams(mappings, type, search, searchFields, extraQueryP
];
}

// a list of fields to manually merge together
const fieldsToMerge = ['filter', 'must'];

const extraParams = {
...extraQueryParams.bool
};

// Remove the manual merge fields from the collection of properties we will automatically combine.
fieldsToMerge.forEach(field => delete extraParams[field]);

let query = {
bool: defaultsDeep(bool, extraParams)
};

if (extraQueryParams.bool) {

const extraBoolParams = extraQueryParams.bool;

query = fieldsToMerge.reduce((queryAcc, field) => {
const prop = queryAcc.bool[field];
const extraProp = extraBoolParams[field];
if (Array.isArray(prop) && Array.isArray(extraProp)) {
queryAcc.bool[field] = [...prop, ...extraProp];
}
return queryAcc;
}, query);
// Don't construct a query if there is nothing to search on.
if (bool.filter.length === 0 && !search) {
return {};
}

return { query };
return { query: { bool } };
}


Loading