-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for full text search indexes #203
Comments
Seconded of course, I thought it might have already been supported as it seems kinda important. |
Will be supported in the next minor release, I'm shooting for the end of March for that. |
@tgriesser wow, that's unexpected but awesome. Thank you! |
@tgriesser Did this actually happen? |
It's not released yet. Let me go though and make sure this got added though, trying to close out almost every remaining ticket here with the new version. I know you can now rename column and change types :) |
Oooooooh exciting! 🎉 cc/ @sebgie |
@ErisDS so I'm looking at this again, just wanted to make sure how I was thinking about going about it would work... was thinking you'd do |
You mean you lose ability to add columns normally, and have to do the whole table-copy thing? |
Yep. Seems to be the story with pretty much everything around modifying sqlite, except now it's even on adding columns. |
Wooohoooooo 🎈 ... lol In Ghost - there will be the post markdown, the title, and perhaps the tags names that we'll want to do FTS on I think to start with... not sure if that helps. |
I just discovered Need some funding for 0.6 @tgriesser? I dunno how you've done this much already :) |
👍 |
@tgriesser @ErisDS For sqllite and FTS maybe following this pattern would work? http://peewee.readthedocs.org/en/latest/peewee/playhouse.html?highlight=full%20text#FTSModel They create a separate table specifically to use in the full text search index. That way they avoid changing all columns to text in the main table. |
@dwstevens that could work, not sure where we are on FTS here. Bookshelf supports plugins, perhaps something like this could be done with a plugin and knex raw? Would be an awesome thing for someone to have a play with and see if they could get a working version together. |
I've just raised a discussion issue on Ghost about implementing search, it's linked here. It's quite literally the oldest issue on our repository and we desperately need to move forward with it. @tgriesser what's the status here in knex? Are there any plans? We could really use your input. |
@ErisDS Our solution with Sequelize was to use raw Sequelize queries for our FT indexes. Now that I'm trying to switch to a DAO pattern and using Knex, I'll likely implement something similar. It's not as elegant as having built-in support, but it'll do. @tgriesser I'd like to help if I can! |
@bomattin I think most people are using raw queries, but are only having to support a single DB, where we need to have support for Sqlite3, MySQL & pg. Are you planning to write something generalised? |
@ErisDS I'm going to start looking into it. Unfortunately most of my programming time for the next week or so is going to be building my DAOs and trying to sell my fellow devs on it. I'm taking a look at the Knex codebase when I can so I can try to dive in and do it right quickly. |
Hello! Any luck on this? |
@s-stude No movement on my end, unfortunately. This is what I was using for the time being: Users.find = function(params, options) {
return new Promise(function(resolve, reject){
var options = options? options : {};
var whereclause;
var lim = options.limit? options.limit : 65536; // Need a valid number here? 50 isn't a bad default.
knex.select(cols.user)
.from('users')
.orWhereRaw('MATCH(firstname_preferred,lastname_preferred,username) AGAINST(? IN BOOLEAN MODE)', params.search)
//.orWhereRaw('MATCH(groups.name) AGAINST(? IN BOOLEAN MODE)', '+matt*')
.limit(lim)
.then(function(userresults){
resolve(new Users(userresults))
})
.catch(function(err){
reject(err)
});
})
}; |
Any updates here? This is what I'm using as a workaround:
|
I looked at http://knexjs.org/#Schema-index and missed the little detail about "index type is only supported on PostgreSQL", so I ran the following as part of my migration: return knex.schema.table('host', t => {
t.index('name', 'name_idx', 'FULLTEXT') # <<< I wish
}) resulting [ { sql: 'alter table `host` add index name_idx(`name`)',
bindings: [] } ] where I suppose it should have said |
What happened with this? Is there a holdup? Can I be of help? |
@yamikuronue sure, looks like major databases support this, so common API to create index would be nice. Pull requests are welcome if you like to implement this. |
Was any work already done I can build on, or should I start over from scratch? I'm not sure I'll be able to do it, of course, but I'm willing to take a look at least |
removed flood +1 and status update request comments, please use thumbs for voting EDIT: if there are no new messages in this feed, then there is nothing new to tell |
So how can this be done in the meantime in one top-level invocation (i.e. knex.raw('alter table `table` add fulltext (`col`)'); ? This actually isn't all that annoying. I keep all my schemas in one directory, with file names corresponding to table names. So for some table // foo.js
module.exports = function (table) {
table.string(...);
}; and for some table // bar.js
module.exports = {
builder: function (table) {
table.string(...);
},
raw: function () {
return knex.raw('alter table `bar` add fulltext (`col`)');
}
}; Then I just: function createTable(table) {
const schema = require('schemas/' + table);
return (
typeof schema === 'function'
? db.schema.createTable(table, schema)
: db.schema.createTable(table, schema.builder)
.then(() => schema.raw())
).catch(err => {
logError(err.message);
})
} |
This issue was the top hit for many of the searches I did, but didn't address my question. For those who also end up here for the same reason... Postgres users can add a full text index to a tsvector column using the third argument to exports.up = (knex) => {
return knex.schema.createTable('foo', (table) => {
table.increments('id');
table.specificType('fulltext', 'tsvector');
table.index('fulltext', null, 'gin');
);
); Also see: https://knexjs.org/#Schema-index and https://stackoverflow.com/questions/45871474/how-to-add-gin-index-using-knex-js |
@rapodaca Thank you for your contribution, added this information to the wiki! |
this may also be a helpful workaround |
Does this still working for you guys ?
Even using table.index('title', null, 'BOOM'), knex isn't throwing an error and the sql result is the same. Did i missed something ? Using RAW query is working.
|
Guys ) I've thought off workaround without any .then()'s: So knex guys should just remove |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
PRs definitely would be most welcome. |
A little addition to this for anyone who finds this thread as I did while looking for how to add custom indexes for full text search (trigram search really) in postgres. I used @denysaw's idea above and came up with:
Which gives:
This might count as SQL injection ;) |
Postgres, sqlite, and MySQL all support some form of full-text index. It would be useful to support generating schema for and querying based on these indexes.
This does not seem like a trivial feature, as the implementations are very different for index creation requirements (sqlite appears to be at the table-level, MySQL only supports it on MyISAM tables) and querying.
That said, this issue could at least be used to track interest for such a feature.
References:
Example index creation and querying:
The text was updated successfully, but these errors were encountered: