Git Product home page Git Product logo

pg-tsquery's Introduction

Text-Search parser for PostgreSQL

npm version build status coverage status

Why?

Using pg's to_tsquery directly with user input can throw errors. plainto_tsquery sanitizes the user input, but it's very limited (it just puts an and between words), websearch_to_tsquery extends this behavior a little further only between double-quotes, with followedBy operator and negations.

This module allows customizable text-search operators: and, or, followedBy, not, prefix, parentheses, quoted text (same behavior than websearch_to_tsquery).

See the options defaults values

usage

const tsquery = require('pg-tsquery')(/* options can be passed to override the defaults */);

pool.query('SELECT * FROM tabby WHERE to_tsvector(col) @@ to_tsquery($1)', [tsquery(str)]);

// or get a reusable instance
const {Tsquery} = require('pg-tsquery');

const parser = new Tsquery(/* options can be passed to override the defaults */);

// then process your input with parser.parse(str).toString()
inputs output
foo bar foo&bar
foo -bar, foo !bar, foo + !bar foo&!bar
foo bar,bip, foo+bar | bip foo&bar|bip
foo (bar,bip), foo+(bar|bip) foo&(bar|bip)
foo>bar>bip foo<->bar<->bip
foo*,bar* bana:* foo:*|bar:*&bana:*

Support

Please consider reporting issues and trying to create a pull request as well

pg-tsquery's People

Contributors

blasterpistol avatar caub avatar jefbarn avatar tomquist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pg-tsquery's Issues

Negated wildcard phrase produces invalid syntax

I was testing this module out in production and within 24h someone entered a term like this 😅

-"keyword"*

…which produces the invalid syntax:

!("keyword"):*

…perhaps a wildcard after a phrase should just be dropped?

Do not work well with Postgres 13

If use 'abs as input and pass it to pg-tsquery, the output will be the same 'abs, then pass it to to_tsquery() and postgres 13 will complain ERROR: syntax error in tsquery: "'abs". Queries as ', 'b, abs&'s and maybe some others will also throw an error.

Negation should not be applied in some cases

An input is just some serial number.

pg-tsquery produces the same result whether there is space character in front of negation or not:

parse('TS01-H-18011-005')
// TS01&!H&!18011&!005

parse('TS01 -H -18011 -005')
// TS01&!H&!18011&!005

Postgres has different result depending on the presence of a space character in front of negation:

select websearch_to_tsquery('TS01-H-18011-005');
--> 'ts01-h' <-> 'ts01' <-> 'h' <-> '18011' <-> '-005'

select websearch_to_tsquery('TS01 -H -18011 -005');
--> 'ts01' & !'h' & !'18011' & !'005'

This is useful in cases like SN inventory, slugs, and so on.

Phrase searching

Right now this library doesn't seem to support searching for phrases. For example, "sad cat" translates to "sad&cat", while Postgres's websearch_to_tsquery translates it to 'sad' <-> 'cat'.

types don't match module format

Ran into a weird issue with this module after switching typescript to the 'node16' module format.
It boils down to the code is exporting a function as a cjs module, but the index.d.ts definitions are written as if it's esm.

error TS2349: This expression is not callable.
Type 'typeof import(".../node_modules/pg-tsquery/index")' has no call signatures.

I think the fix would be something like this in index.d.ts instead of the export default:

declare function tsquery(options?: TsqueryOptions): (str: string) => string;
export = tsquery

For now a work around is this goofy thing:

import { default as Tsquery } from 'pg-tsquery'

type TsQueryFunc = (options?: Tsquery.TsqueryOptions) => (str: string) => string
const tsqueryParser = (Tsquery as unknown as TsQueryFunc)()

The typescript guys built this little tool that seems to agree with my analysis:
https://arethetypeswrong.github.io/?p=pg-tsquery%408.4.1

How to escape + and - modificators?

I have product SKU numbers like "G1-ABC", and search simply not found it with this library. The same with international phone numbers like "+37544:*" because + interpreted as modificators.

Documentation issue

There seems to be small issue with the documentation:

const tsquery = require('pg-tsquery')(/* options can be passed to override the defaults */);

pool.query('SELECT * FROM tabby WHERE to_tsvector(col) @@ to_tsquery($1)', [tsquery(str)]);

Seeing as the exported tsquery function returns a function, I believe this should be:

tsquery()(str)

License

Would it be possible to attach an explicit license to this repo? I'm interested in using in a project but without knowing the licensing it's tricky to do so.

Thanks for your efforts!

Support followed-by operator

select to_tsquery('tomato<->banana') @@ to_tsvector('tomato banana'); -- true
select to_tsquery('cherry<2>banana') @@ to_tsvector('cherry lol banana'); -- true

precedences are:

1.| (OR)
2. & (AND)
3. <-> (FOLLOWED BY)
4. ! (NOT)

Regex error?

Hi there. I am getting this error with the default settings. Any ideas?

SyntaxError: Invalid regular expression: /^[\s*&+<:,|]*(?<negated>[\s!-]*)[\s*&+<:,|]*(?:(?<quote>["'])(?<phrase>.*?)\k<quote>|(?<word>[^\s,|&+<>:*()[\]!-]+))/: Invalid group
    at Object.<anonymous> (/home/ec2-user/environment/dartmoorarchive/node_modules/pg-tsquery/index.js:6:21)
    at Module._compile (module.js:653:30)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.