Skip to content

justsml/knex-full-text-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Knex Full-text Search Plugin

NPM version GitHub stars

A Knex plugin for easy Full-text Search queries in Postgres.

Get Started

npm install knex-full-text-search-plugin
# OR
yarn add knex-full-text-search-plugin

Once installed, add the plugin to your Knex instance:

import Knex from 'knex';
import KnexFullTextSearchPlugin from 'knex-full-text-search-plugin';

export const db = Knex(config);

// Simply call the plugin with your Knex instance
KnexFullTextSearchPlugin(db);

Methods

selectWebSearchRank

Add a column with a rank/score for the given query.

The column alias defaults to rank.

The rank/score is calculated using the Postgres function ts_rank.

const results = await db('products')
    .select('id', 'name')
    .selectWebSearchRank('description', 'Shoes')
    .whereWebSearch('description', 'Shoes')
    .orderBy('rank', 'desc');

Will produce the following SQL query:

select id, name, ts_rank(`description`, websearch_to_tsquery('simple', 'Shoes')) as `rank`
  from products
  where `description` @@ websearch_to_tsquery('simple', 'Shoes')
  order by `rank` desc;

whereWebSearch

Add a WHERE clause to your query to filter by the given query.

Use in conjunction with selectWebSearchRank to compute a rank to order the results.

Note: Intelligently handles undefined input by returning the query builder unmodified.

const results = await db('products')
  .select('id', 'name')
  .whereWebSearch('description', 'Shoes')

Will produce the following SQL query:

select id, name
  from products
  where `description` @@ websearch_to_tsquery('simple', 'Shoes');

About

Knex plugin for Postgres FTS (Full-text Search).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published