Handle count for search query

This commit is contained in:
Aurelsicoko 2018-06-07 17:24:09 +02:00
parent 60e0c6dcdb
commit abc8395f2d
3 changed files with 38 additions and 11 deletions

View File

@ -60,14 +60,11 @@ module.exports = {
// Search in columns which are not text value. // Search in columns which are not text value.
searchNoText.forEach(attribute => { searchNoText.forEach(attribute => {
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`); qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(params.search).replace(/[^a-zA-Z. ]/g, '')}%'`);
}) });
// Search in columns with text using index. // Search in columns with text using index.
switch (this.client) { switch (this.client) {
case 'mysql': case 'pg': {
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`);
break;
case 'pg':
const searchQuery = searchText.map(attribute => const searchQuery = searchText.map(attribute =>
_.toLower(attribute) === attribute _.toLower(attribute) === attribute
? `to_tsvector(${attribute})` ? `to_tsvector(${attribute})`
@ -75,7 +72,11 @@ module.exports = {
); );
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, '')); qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, params.search.replace(/[^a-zA-Z. ]/g, ''));
break;
}
default: default:
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${params.search.replace(/[^a-zA-Z. ]/g, '')}*`);
break;
} }
if (params.sort) { if (params.sort) {
@ -95,7 +96,38 @@ module.exports = {
}, },
countSearch: async function (params = {}) { 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) { findOne: async function (params, populate) {

View File

@ -44,7 +44,6 @@ module.exports = {
}, },
find: async ctx => { find: async ctx => {
console.log(ctx.request.query);
// Search // Search
if (!_.isEmpty(ctx.request.query.q)) { if (!_.isEmpty(ctx.request.query.q)) {
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].search(ctx.params, ctx.request.query); ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].search(ctx.params, ctx.request.query);

View File

@ -26,8 +26,6 @@ module.exports = {
const { limit, skip, sort, source, q, populate = [] } = query; // eslint-disable-line no-unused-vars const { limit, skip, sort, source, q, populate = [] } = query; // eslint-disable-line no-unused-vars
const filters = strapi.utils.models.convertParams(params.model, query); const filters = strapi.utils.models.convertParams(params.model, query);
console.log("SEARCH", q);
// Find entries using `queries` system // Find entries using `queries` system
return await strapi.query(params.model, source).search({ return await strapi.query(params.model, source).search({
limit: limit || filters.limit, limit: limit || filters.limit,
@ -40,8 +38,6 @@ module.exports = {
countSearch: async (params, query) => { countSearch: async (params, query) => {
const { source, q } = query; const { source, q } = query;
console.log("COUNT SEARCH", q);
return await strapi.query(params.model, source).countSearch({ search: q }); return await strapi.query(params.model, source).countSearch({ search: q });
}, },