diff --git a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js index 565d0a399e..c5f90896d9 100755 --- a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js +++ b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js @@ -60,14 +60,11 @@ module.exports = { // Search in columns which are not text value. searchNoText.forEach(attribute => { qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`); - }) + }); // Search in columns with text using index. switch (this.client) { - case 'mysql': - qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`); - break; - case 'pg': + case 'pg': { const searchQuery = searchText.map(attribute => _.toLower(attribute) === attribute ? `to_tsvector(${attribute})` @@ -75,7 +72,11 @@ module.exports = { ); qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, '')); + break; + } default: + qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`); + break; } if (params.sort) { @@ -95,7 +96,38 @@ module.exports = { }, countSearch: async function (params = {}) { - return 0; + const associations = this.associations.map(x => x.alias); + const searchText = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => ['string', 'type'].includes(this._attributes[attribute].type)); + + const searchNoText = Object.keys(this._attributes) + .filter(attribute => attribute !== this.primaryKey && !associations.includes(attribute)) + .filter(attribute => !['string', 'type'].includes(this._attributes[attribute].type)); + + return this.query(qb => { + // Search in columns which are not text value. + searchNoText.forEach(attribute => { + qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`); + }); + + // Search in columns with text using index. + switch (this.client) { + case 'pg': { + const searchQuery = searchText.map(attribute => + _.toLower(attribute) === attribute + ? `to_tsvector(${attribute})` + : `to_tsvector('${attribute}')` + ); + + qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, '')); + break; + } + default: + qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`); + break; + } + }).count(); }, findOne: async function (params, populate) { diff --git a/packages/strapi-plugin-content-manager/controllers/ContentManager.js b/packages/strapi-plugin-content-manager/controllers/ContentManager.js index 9b1896d9f0..62232ffa84 100755 --- a/packages/strapi-plugin-content-manager/controllers/ContentManager.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentManager.js @@ -44,7 +44,6 @@ module.exports = { }, find: async ctx => { - console.log(ctx.request.query); // Search if (!_.isEmpty(ctx.request.query.q)) { ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].search(ctx.params, ctx.request.query); diff --git a/packages/strapi-plugin-content-manager/services/ContentManager.js b/packages/strapi-plugin-content-manager/services/ContentManager.js index 290751d958..e97f572e39 100644 --- a/packages/strapi-plugin-content-manager/services/ContentManager.js +++ b/packages/strapi-plugin-content-manager/services/ContentManager.js @@ -26,8 +26,6 @@ module.exports = { const { limit, skip, sort, source, q, populate = [] } = query; // eslint-disable-line no-unused-vars const filters = strapi.utils.models.convertParams(params.model, query); - console.log("SEARCH", q); - // Find entries using `queries` system return await strapi.query(params.model, source).search({ limit: limit || filters.limit, @@ -40,8 +38,6 @@ module.exports = { countSearch: async (params, query) => { const { source, q } = query; - console.log("COUNT SEARCH", q); - return await strapi.query(params.model, source).countSearch({ search: q }); },