Skip to content

Fulltext support #1580

Closed
Closed

Description

@rorhug

Adding support for this would be useful as it's becoming quite popular as a simpler alternative to systems like Solr, Sphinx and ElasticSearch.

Here are the docs for text search:
http://www.postgresql.org/docs/9.3/static/textsearch.html

Also, I don't know how useful this is, but here is a nice Ruby gem for Postgres full text which may help with laying out a plan on how to do it with sequelize.
https://github.com/Casecommons/pg_search

Activity

  1. self-assigned this
    on May 17, 2014
  2. bleupen commented on May 11, 2015

    @bleupen
  3. curiousdannii commented on Jun 8, 2015

    @curiousdannii
    Contributor

    The method on mathisonian.com is a good start, but because it uses a custom function text searches can't be combined with other search queries. If you wanted to search for posts containing X by user Y you'd need to filter for user Y separately.

    One option which may be quicker to implement (but might be slower) would be to use an index rather than a separate column. But the index might use less disk space.

    Ideally it would be possible to set weights for different columns when combining them into one index/TSVECTOR column.

  4. drouillard commented on Oct 2, 2015

    @drouillard
  5. mujz commented on Jul 7, 2016

    @mujz
  6. mjurincic commented on Jul 7, 2016

    @mjurincic
  7. mhoffman commented on Nov 11, 2016

    @mhoffman
  8. felixfbecker commented on Nov 11, 2016

    @felixfbecker
    Contributor

    Please don't +1 issues. It clutters the thread without adding value to the discussion and spams maintainers with notifications. Use GitHub reactions to upvote features.
    image

  9. 6 remaining items

  10. added
    dialect: postgres
    on Feb 16, 2018
  11. schettino commented on Mar 31, 2018

    @schettino

    Any news about it?

  12. mschipperheyn commented on May 3, 2019

    @mschipperheyn

    This is preventing me from upgrading to v5 because raw operations are no longer supported.

  13. jnelken commented on May 26, 2019

    @jnelken

    Looks like attempts are being made. Let's contribute! #1580

  14. slidenerd commented on Oct 3, 2019

    @slidenerd

    @mschipperheyn Here is a custom migration that creates a column for storing ts vector, adds an index for the same and creates a trigger where you specify the list of columns in your table from which the ts vector is generated

    Use the Sequelize CLI to generate a new migration with the command below
    ./node_modules/.bin/sequelize migration:create --name add-tsvector-to-feed-item-title-description

    Open the file
    Feel free to replace the column names and table name with whatever you want

    module.exports = {
      // eslint-disable-next-line no-unused-vars
      up: (queryInterface, Sequelize) => {
        /*
          Add altering commands here.
          Return a promise to correctly handle asynchronicity.
    
          Example:
          return queryInterface.createTable('users', { id: Sequelize.INTEGER });
        */
        const { sequelize } = queryInterface;
        const searchFields = ['title', 'summary'];
        const vectorName = 'title_summary_vector';
        const tableName = 'feed_items';
        const indexName = 'title_summary_vector_search_idx';
        const triggerName = 'title_summary_vector_update';
    
        return sequelize
          .query(`ALTER TABLE "${tableName}" ADD COLUMN "${vectorName}" TSVECTOR`)
          .then(() => {
            console.log('Column added: Adding updating values');
            return sequelize
              .query(`UPDATE "${tableName}" SET "${vectorName}" = to_tsvector('english', ${searchFields.join(' || \' \' || ')})`)
              .catch(console.log);
          }).then(() => {
            console.log('Values added: Creating Index');
            return sequelize
              .query(`CREATE INDEX ${indexName} ON "${tableName}" USING gin("${vectorName}");`)
              .catch(console.log);
          }).then(() => {
            console.log('Index created: Adding trigger');
            return sequelize
              .query(`CREATE TRIGGER ${triggerName} BEFORE INSERT OR UPDATE ON "${tableName}" FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger("${vectorName}", 'pg_catalog.english', ${searchFields.join(', ')})`)
              .catch(console.log);
          })
          .then(() => {
            console.log('Everything worked!');
          })
          .catch(console.log);
      },
    
      // eslint-disable-next-line no-unused-vars
      down: (queryInterface, Sequelize) => {
        /*
          Add reverting commands here.
          Return a promise to correctly handle asynchronicity.
    
          Example:
          return queryInterface.dropTable('users');
        */
        const { sequelize } = queryInterface;
        const vectorName = 'title_summary_vector';
        const tableName = 'feed_items';
        const indexName = 'title_summary_vector_search_idx';
        const triggerName = 'title_summary_vector_update';
    
        return sequelize
          .query(`DROP TRIGGER ${triggerName} ON "${tableName}"`)
          .then(() => {
            console.log('removed trigger');
            return sequelize
              .query(`DROP INDEX ${indexName}`)
              .catch(console.log);
          }).then(() => {
            console.log('removed index');
            return sequelize
              .query(`ALTER TABLE "${tableName}" DROP COLUMN "${vectorName}"`)
              .catch(console.log);
          }).then(() => {
            console.log('removed column');
          })
          .catch(console.log);
      },
    };

    Run the migration
    ./node_modules/.bin/sequelize db:migrate

    Boom, you got ts vector support ON, you dont need to modify your insert or update queries in any way as the trigger does it

  15. added
    status: wip
    and removed
    dialect: mssql
    dialect: mysql
    on Oct 30, 2019
  16. papb commented on Oct 30, 2019

    @papb
    Member

    It seemed to me that this request is postgres-only, is this correct?

  17. papb commented on Oct 30, 2019

    @papb
    Member

    This is preventing me from upgrading to v5 because raw operations are no longer supported.

    @mschipperheyn What do you mean?

  18. mschipperheyn commented on Nov 11, 2019

    @mschipperheyn

    @papb sorry, I lost track of this issue. I use MySql. The only reason I'm still on sequelize v4 is because it offers an escape hatch that permits me to hack

    Sequelize.literal(`MATCH (\someColumn\) AGAINST('someValue*' IN BOOLEAN MODE)`)`)
    

    which was blocked in v5 I think. full text search is a really useful alternative to implementing elastic search. Especially for simpler use cases like filtering table lists. Unfortunately, the approach between Postgres and MySql is completely different. It would be awesome if Sequelize would abstract this difference away but given the differences in approach between postgres and MySql not the easiest thing to implement. Personally, I find the approach MySql uses much more intuitive than the Postgres version.

    Since I don't use raw here, I don't remember if my decision to not upgrade to v5 is unfounded :-D

  19. cauefernandes commented on Apr 9, 2020

    @cauefernandes

    We have temporarily solved this issue by creating stored function in pg:

    CREATE OR REPLACE FUNCTION tsmatch(a TSVECTOR, b TSQUERY) RETURNS BOOLEAN AS $$
        BEGIN
            RETURN a @@ b;
        END;
        $$ LANGUAGE plpgsql;

    And then use it from js like this:

    Sequelize.where(
            Sequelize.fn("tsmatch",
                Sequelize.fn("to_tsvector", "english", vector),
                Sequelize.fn("to_tsquery", "english", query),
            ),
            true,
        );

    It works but the problem is that function won't use indexes.

  20. papb commented on Jan 27, 2021

    @papb
    Member

    Closed by #12955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgresstatus: wiptype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions