'use strict';

var pagesdb = require('../../../db/pagesdb')
var checkAndCoerceDateFilterParams = require('../../../utils/checkAndCoerceDateFilterParams')
var processSearchTerms = require('./processSearchTerms')

function search(req, res, next){

  var processedSearchTerms = processSearchTerms(req.params.searchTerms)
  var domainToSearchFor = processedSearchTerms.domainToSearchFor
  var searchTerms = processedSearchTerms.processedSearchTerms
  var dateFilter = checkAndCoerceDateFilterParams(req.body)
  var knexSQL = null

  if(!searchTerms.length){
    /****
     * If user just wants to list all saved pages by a domain (with no text search)
     */
    if(domainToSearchFor){
      knexSQL = pagesdb.db('pages').where({pageDomain: domainToSearchFor})
      if(dateFilter){
        /****
         * knex will automatically convert additional WHERE's to AND WHERE
         */
        knexSQL = knexSQL
          .where('dateCreated', '>=', dateFilter.dateFilterStartDate)
          .where('dateCreated', '<=', dateFilter.dateFilterEndDate)
      }
      knexSQL = knexSQL.orderBy('dateCreated', 'desc')
    }
    else{
      /****
       * If there are no search terms and they are not searching for domain
       * results, then send back zero results as an empty array.
       */
      res.json([])
    }
  }
  else{
    knexSQL = pagesdb
      .db
      .select(
        `rank`,
        `pageUrl`,
        `dateCreated`,
        `pageDomain`,
        `pageTitle`,
        `pageText`,
        `pageDescription`,
        `archiveLink`,
        `safeBrowsing`,
        pagesdb.db.raw(
          `snippet(fts, -1, '<span class="searchHighlight">',
          '</span>', '...', 64) as snippet`
        )
      )
      .from('fts')

    if(domainToSearchFor){
      knexSQL = knexSQL.where({pageDomain: domainToSearchFor})
    }
    if(dateFilter){
      knexSQL = knexSQL
        .where('dateCreated', '>=', dateFilter.dateFilterStartDate)
        .where('dateCreated', '<=', dateFilter.dateFilterEndDate)
    }
    /****
     * https://sqlite.org/fts5.html#section_5_1_1
     * bm25(fts, 4.0, 1.0, 2.0) - Give pageTitle a boost of 4,
     * and pageDescription a boost of 2.
     * Note: the SQL operators in the 'searchTerm OR NEAR()` are case-sensitive
     * and must be in uppercase!
     * The final SQL statement should be similar to this:
     * select "rank", "pageUrl", "dateCreated", "pageDomain", "pageTitle", "pageText", "pageDescription", "archiveLink", "safeBrowsing", snippet(fts, -1, '<span class="searchHighlight">',
     '</span>', '...', 64) as snippet from "fts" where fts match 'foo bar' order by bm25(fts, 4.0, 1.0, 2.0)
     */
    knexSQL = knexSQL
      //.whereRaw(`fts match ? order by bm25(fts, 4.0, 1.0, 2.0)`, `"${searchTerms}" OR NEAR(${searchTerms})`)
      .whereRaw(`fts match ? order by bm25(fts, 4.0, 1.0, 2.0)`, `${searchTerms}`)
  }

  knexSQL
    .then( rows => {
      res.json(rows)
    })
    .catch( err => {
      console.error(err)
      res
        .status(500)
        .json({
          searchError: `There was an issue with the search, check your search text is correct.`
        })
    })

}

module.exports = search
@@ -2330,17 +2330,21 @@
 be s
-imilar to
+omething like
  thi
@@ -2533,21 +2533,16 @@
 ight%22%3E',
-%0A    
  '%3C/span

Comment update