Skip to content

Commit

Permalink
Adding ability to specify filters when calling the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Jun 6, 2018
1 parent 1231c70 commit 9da30a1
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/server/saved_objects/service/lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export class SavedObjectsRepository {
* @property {string} [options.sortField]
* @property {string} [options.sortOrder]
* @property {Array<string>} [options.fields]
* @property {Array<Object>} [options.filters]
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }], total, per_page, page }
*/
async find(options = {}) {
Expand All @@ -224,6 +225,7 @@ export class SavedObjectsRepository {
sortField,
sortOrder,
fields,
filters,
} = options;

if (searchFields && !Array.isArray(searchFields)) {
Expand All @@ -247,7 +249,8 @@ export class SavedObjectsRepository {
searchFields,
type,
sortField,
sortOrder
sortOrder,
filters,
})
}
};
Expand Down
7 changes: 6 additions & 1 deletion src/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,18 @@ describe('SavedObjectsRepository', () => {
}
});

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

await savedObjectsRepository.find(relevantOpts);
Expand Down
52 changes: 31 additions & 21 deletions src/server/saved_objects/service/lib/search_dsl/query_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,43 @@ function getFieldsForTypes(searchFields, types) {
* @param {Array<string>} searchFields
* @return {Object}
*/
export function getQueryParams(mappings, type, search, searchFields) {
if (!type && !search) {
return {};
}

const bool = {};
export function getQueryParams(mappings, type, search, searchFields, filters = []) {
const filter = [...filters];
const must = [];

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

if (search) {
bool.must = [
...bool.must || [],
{
simple_query_string: {
query: search,
...getFieldsForTypes(
searchFields,
getTypes(mappings, type)
)
}
must.push({
simple_query_string: {
query: search,
...getFieldsForTypes(
searchFields,
getTypes(mappings, type)
)
}
];
});
}

if (filter.length === 0 && must.length === 0) {
return {};
}

const result = {
query: {
bool: {}
}
};

if (filter.length > 0) {
result.query.bool.filter = filter;
}

if (must.length > 0) {
result.query.bool.must = must;
}

return { query: { bool } };
return result;
}
Loading

0 comments on commit 9da30a1

Please sign in to comment.