From 62d9e3f03f6eb3a519f273cc9ec79ec8cf9e4a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20No=C3=ABl?= Date: Mon, 18 Jun 2018 14:58:25 +0200 Subject: [PATCH] Add the possibility to search by ContentId or LocationId in the search tab of the browse feature in Universal Discovery Module --- .../services/universal.discovery.service.js | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/modules/universal-discovery/services/universal.discovery.service.js b/src/modules/universal-discovery/services/universal.discovery.service.js index 1ba73bb9..bdd03fa5 100644 --- a/src/modules/universal-discovery/services/universal.discovery.service.js +++ b/src/modules/universal-discovery/services/universal.discovery.service.js @@ -8,6 +8,30 @@ const HEADERS_CREATE_VIEW = { const QUERY_LIMIT = 50; const ENDPOINT_CREATE_VIEW = '/api/ezp/v2/views'; +/** + * Build the criterion for the search. + * Tries to find a search by contentId or locationId with 'location:123' or 'content:456' in the query + * If no such pattern is found, return a FullTextCriterion + * @function buildQueryCriterions + * @param {String} query + * @returns {Object} + */ +const buildQueryCriterions = query => { + const re = new RegExp(/(content|location):([0-9]+)/); + let criterion = {}; + let searchParts = []; + if ((searchParts = re.exec(query)) !== null) { + if (parseInt(searchParts[2]) > 0) { + const criterion = {}; + let criterionName = searchParts[1].toLowerCase()+'IdCriterion'; + criterionName = criterionName.charAt(0).toUpperCase()+criterionName.substr(1); + criterion[criterionName] = parseInt(searchParts[2]); + return criterion; + } + } + return {FullTextCriterion: query}; +}; + /** * Loads preselected location data * @@ -201,7 +225,7 @@ export const findContentBySearchQuery = ({ token, siteaccess }, query, callback) Criteria: {}, FacetBuilders: {}, SortClauses: {}, - Filter: { FullTextCriterion: query }, + Filter: buildQueryCriterions(query), limit: QUERY_LIMIT, offset: 0, },