From 5d4a9fca719062423de1a9805d3c236286da8482 Mon Sep 17 00:00:00 2001 From: zzzzwen Date: Wed, 15 Mar 2017 17:36:35 +0800 Subject: [PATCH] Refine search logic --- backend/proxy/movie.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/backend/proxy/movie.js b/backend/proxy/movie.js index 9b7e7a7..a858d36 100644 --- a/backend/proxy/movie.js +++ b/backend/proxy/movie.js @@ -33,25 +33,25 @@ function getSearchString(searchString, priority) { } exports.getMovieByTitleCount = function (searchString) { - var rawString = searchString; + var rawString = searchString.trim(); return Movie.count({ - where: sequelize.literal('"movies"."title" ILIKE \'' + getSearchString(rawString, 1) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 2) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 3) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 4) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 5) + '\'') + where: { + title: { + $ilike: '%' + rawString + '%' + } + } }) }; exports.getMovieByTitle = function (userId, searchString, offset, limit) { - var rawString = searchString; + var rawString = searchString.trim(); return Movie.findAll({ - where: sequelize.literal('"movies"."title" ILIKE \'' + getSearchString(rawString, 1) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 2) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 3) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 4) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 5) + '\''), + where: { + title: { + $ilike: '%' + rawString + '%' + } + }, limit: limit, offset: offset, include: [ @@ -89,13 +89,13 @@ exports.getMovieByTitle = function (userId, searchString, offset, limit) { exports.getShowingMovieByTitle = function (userId, searchString) { - var rawString = searchString; + var rawString = searchString.trim(); return Movie.findAll({ - where: sequelize.literal('"movies"."title" ILIKE \'' + getSearchString(rawString, 1) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 2) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 3) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 4) + '\' OR ' + - '"movies"."title" ILIKE \'' + getSearchString(rawString, 5) + '\''), + where: { + title: { + $ilike: '%' + rawString + '%' + } + }, include: [ { model: PublicRate,