Skip to content

Commit

Permalink
customFilters for search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Feb 17, 2021
1 parent 77b0532 commit 3045b84
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/api/search/documentQueryBuilder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase, max-lines */

import { preloadOptionsSearch } from 'shared/config';
import filterToMatch, { multiselectFilter } from './metadataMatchers';
import filterToMatch, { textFilter, multiselectFilter } from './metadataMatchers';
import { propertyToAggregation, generatedTocAggregations } from './metadataAggregations';

export default function() {
Expand Down Expand Up @@ -231,6 +231,17 @@ export default function() {
}
},

customFilters(filters = {}) {
Object.keys(filters).forEach(key => {
if (filters[key].values.length) {
addFilter({
terms: { [key]: filters[key].values },
});
}
});
return this;
},

filterMetadata(filters = []) {
filters.forEach(filter => {
const match = filterToMatch(filter, filter.suggested ? 'suggestedMetadata' : 'metadata');
Expand Down
2 changes: 2 additions & 0 deletions app/api/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ const buildQuery = async (query, language, user, resources) => {
const filters = processFilters(query.filters, [...allUniqueProps, ...properties]);
// this is where the query filters are built
queryBuilder.filterMetadata(filters);
queryBuilder.customFilters(query.customFilters);
// this is where the query aggregations are built
queryBuilder.aggregations(aggregations, dictionaries);

Expand All @@ -627,6 +628,7 @@ const search = {
}

// queryBuilder.query() is the actual call
// console.log(JSON.stringify(queryBuilder.query(), null, ' '));
return elastic
.search({ body: queryBuilder.query() })
.then(response => processResponse(response, templates, dictionaries, language, query.filters))
Expand Down
11 changes: 11 additions & 0 deletions app/api/search/searchSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ export const searchSchema = {
properties: {
aggregateGeneratedToc: { type: 'boolean' },
filters: { type: 'object' },
customFilters: {
type: 'object',
properties: {
generatedToc: {
type: 'object',
properties: {
values: { type: 'array', items: [{ type: 'string' }] },
},
},
},
},
types: { type: 'array', items: [{ type: 'string' }] },
_types: { type: 'array', items: [{ type: 'string' }] },
fields: { type: 'array', items: [{ type: 'string' }] },
Expand Down
1 change: 1 addition & 0 deletions app/api/search/specs/fixtures_elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const fixtures = {
language: 'en',
title: 'Batman finishes en',
published: true,
generatedToc: true,
user: userId,
metadata: {
relationship: [
Expand Down
15 changes: 15 additions & 0 deletions app/api/search/specs/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,21 @@ describe('search', () => {
});
});

describe('customFilters', () => {
it('should filter by the values passed', async () => {
const query = {
customFilters: {
generatedToc: {
values: ['true'],
},
},
};

const { rows } = await search.search(query, 'en');
expect(rows).toEqual([expect.objectContaining({ title: 'Batman finishes en' })]);
});
});

describe('autocompleteAggregations()', () => {
it('should return a list of options matching by label and options related to the matching one', async () => {
const query = {
Expand Down

0 comments on commit 3045b84

Please sign in to comment.