Skip to content

Commit

Permalink
Adding filters in find for authorized types
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed May 22, 2018
1 parent a8af87b commit 8b0fa36
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 17 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 @@ -189,6 +189,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 @@ -201,6 +202,7 @@ export class SavedObjectsRepository {
sortField,
sortOrder,
fields,
filters,
} = options;

if (searchFields && !Array.isArray(searchFields)) {
Expand All @@ -224,7 +226,8 @@ export class SavedObjectsRepository {
searchFields,
type,
sortField,
sortOrder
sortOrder,
filters,
})
}
};
Expand Down
9 changes: 7 additions & 2 deletions src/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,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'
sortOrder: 'desc',
filters: [{
terms: {
type: ['foo', 'bar']
}
}]
};

await savedObjectsRepository.find(relevantOpts);
Expand Down
14 changes: 5 additions & 9 deletions src/server/saved_objects/service/lib/search_dsl/query_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ 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 bool = {
filter: [...filters],
};

if (type) {
bool.filter = [
{ term: { type } }
];
bool.filter = [...bool.filter, { term: { type } }];
}

if (search) {
Expand Down
228 changes: 227 additions & 1 deletion src/server/saved_objects/service/lib/search_dsl/query_params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ describe('searchDsl/queryParams', () => {
describe('{}', () => {
it('searches for everything', () => {
expect(getQueryParams(MAPPINGS))
.toEqual({});
.toEqual({
query: {
bool: {
filter: []
}
}
});
});
});

Expand All @@ -61,12 +67,54 @@ describe('searchDsl/queryParams', () => {
});
});

describe('{type,filters}', () => {
it('includes filters and a term filter for type', () => {
expect(getQueryParams(MAPPINGS, 'saved', null, null, [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
{
term: { type: 'saved' }
}
]
}
}
});
});
});

describe('{search}', () => {
it('includes just a sqs query', () => {
expect(getQueryParams(MAPPINGS, null, 'us*'))
.toEqual({
query: {
bool: {
filter: [],
must: [
{
simple_query_string: {
query: 'us*',
all_fields: true
}
}
]
}
}
});
});
});

describe('{search,filters}', () => {
it('includes filters and a sqs query', () => {
expect(getQueryParams(MAPPINGS, null, 'us*', null, [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } }
],
must: [
{
simple_query_string: {
Expand Down Expand Up @@ -104,12 +152,37 @@ describe('searchDsl/queryParams', () => {
});
});

describe('{type,search,filters}', () => {
it('includes bool with sqs query, filters and term filter for type', () => {
expect(getQueryParams(MAPPINGS, 'saved', 'y*', null, [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
{ term: { type: 'saved' } }
],
must: [
{
simple_query_string: {
query: 'y*',
all_fields: true
}
}
]
}
}
});
});
});

describe('{search,searchFields}', () => {
it('includes all types for field', () => {
expect(getQueryParams(MAPPINGS, null, 'y*', ['title']))
.toEqual({
query: {
bool: {
filter: [],
must: [
{
simple_query_string: {
Expand All @@ -131,6 +204,7 @@ describe('searchDsl/queryParams', () => {
.toEqual({
query: {
bool: {
filter: [],
must: [
{
simple_query_string: {
Expand All @@ -152,6 +226,85 @@ describe('searchDsl/queryParams', () => {
.toEqual({
query: {
bool: {
filter: [],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'type.title',
'pending.title',
'saved.title',
'type.title.raw',
'pending.title.raw',
'saved.title.raw',
]
}
}
]
}
}
});
});
});

describe('{search,searchFields,filters}', () => {
it('specifies filters and includes all types for field', () => {
expect(getQueryParams(MAPPINGS, null, 'y*', ['title'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'type.title',
'pending.title',
'saved.title'
]
}
}
]
}
}
});
});
it('specifies filters and supports field boosting', () => {
expect(getQueryParams(MAPPINGS, null, 'y*', ['title^3'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'type.title^3',
'pending.title^3',
'saved.title^3'
]
}
}
]
}
}
});
});
it('specifies filters and supports field and multi-field', () => {
expect(getQueryParams(MAPPINGS, null, 'y*', ['title', 'title.raw'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
],
must: [
{
simple_query_string: {
Expand Down Expand Up @@ -242,4 +395,77 @@ describe('searchDsl/queryParams', () => {
});
});
});

describe('{type,search,searchFields,filters}', () => {
it('includes specified filters, type filter and sqs with field list', () => {
expect(getQueryParams(MAPPINGS, 'saved', 'y*', ['title'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
{ term: { type: 'saved' } }
],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'saved.title'
]
}
}
]
}
}
});
});
it('supports fields pointing to multi-fields', () => {
expect(getQueryParams(MAPPINGS, 'saved', 'y*', ['title.raw'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
{ term: { type: 'saved' } }
],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'saved.title.raw'
]
}
}
]
}
}
});
});
it('supports multiple search fields', () => {
expect(getQueryParams(MAPPINGS, 'saved', 'y*', ['title', 'title.raw'], [{ terms: { foo: ['bar', 'baz' ] } }]))
.toEqual({
query: {
bool: {
filter: [
{ terms: { foo: ['bar', 'baz' ] } },
{ term: { type: 'saved' } }
],
must: [
{
simple_query_string: {
query: 'y*',
fields: [
'saved.title',
'saved.title.raw'
]
}
}
]
}
}
});
});
});
});
5 changes: 3 additions & 2 deletions src/server/saved_objects/service/lib/search_dsl/search_dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function getSearchDsl(mappings, options = {}) {
search,
searchFields,
sortField,
sortOrder
sortOrder,
filters,
} = options;

if (!type && sortField) {
Expand All @@ -21,7 +22,7 @@ export function getSearchDsl(mappings, options = {}) {
}

return {
...getQueryParams(mappings, type, search, searchFields),
...getQueryParams(mappings, type, search, searchFields, filters),
...getSortingParams(mappings, type, sortField, sortOrder),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ describe('getSearchDsl', () => {
});

describe('passes control', () => {
it('passes (mappings, type, search, searchFields) to getQueryParams', () => {
it('passes (mappings, type, search, searchFields, filters) to getQueryParams', () => {
const spy = sandbox.spy(queryParamsNS, 'getQueryParams');
const mappings = { type: { properties: {} } };
const opts = {
type: 'foo',
search: 'bar',
searchFields: ['baz']
searchFields: ['baz'],
filters: [
{ terms: { foo: ['bar', 'baz'] } }
]
};

getSearchDsl(mappings, opts);
Expand All @@ -44,6 +47,7 @@ describe('getSearchDsl', () => {
opts.type,
opts.search,
opts.searchFields,
opts.filters,
);
});

Expand Down
Loading

0 comments on commit 8b0fa36

Please sign in to comment.