From 3c06ee10713df2e13ae2441d3551428313a58269 Mon Sep 17 00:00:00 2001 From: Daneryl Date: Wed, 17 Feb 2021 08:39:22 -0500 Subject: [PATCH] customFilters for search endpoint --- app/api/search/documentQueryBuilder.js | 13 ++++++++++++- app/api/search/search.js | 2 ++ app/api/search/searchSchema.ts | 6 ++++++ app/api/search/specs/fixtures_elastic.js | 1 + app/api/search/specs/search.spec.js | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/api/search/documentQueryBuilder.js b/app/api/search/documentQueryBuilder.js index 65679ce7e4d..ee0631acc3e 100644 --- a/app/api/search/documentQueryBuilder.js +++ b/app/api/search/documentQueryBuilder.js @@ -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() { @@ -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'); diff --git a/app/api/search/search.js b/app/api/search/search.js index 8475759233f..97bc0ac8acb 100644 --- a/app/api/search/search.js +++ b/app/api/search/search.js @@ -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); @@ -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)) diff --git a/app/api/search/searchSchema.ts b/app/api/search/searchSchema.ts index 9b67c6a1b13..2b022baf911 100644 --- a/app/api/search/searchSchema.ts +++ b/app/api/search/searchSchema.ts @@ -4,6 +4,12 @@ export const searchSchema = { properties: { aggregateGeneratedToc: { type: 'boolean' }, filters: { type: 'object' }, + customFilters: { + type: 'object', + properties: { + generatedToc: { type: 'string' }, + }, + }, types: { type: 'array', items: [{ type: 'string' }] }, _types: { type: 'array', items: [{ type: 'string' }] }, fields: { type: 'array', items: [{ type: 'string' }] }, diff --git a/app/api/search/specs/fixtures_elastic.js b/app/api/search/specs/fixtures_elastic.js index 86cb00f2c73..086b970e7f5 100644 --- a/app/api/search/specs/fixtures_elastic.js +++ b/app/api/search/specs/fixtures_elastic.js @@ -72,6 +72,7 @@ export const fixtures = { language: 'en', title: 'Batman finishes en', published: true, + generatedToc: true, user: userId, metadata: { relationship: [ diff --git a/app/api/search/specs/search.spec.js b/app/api/search/specs/search.spec.js index e7152ea72af..0752f726daa 100644 --- a/app/api/search/specs/search.spec.js +++ b/app/api/search/specs/search.spec.js @@ -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 = {