WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

Full text search

Open qiaolin-pan opened this issue 5 years ago • 23 comments

I'm wondering about FTS support. For sure it is an sql's feature, but do you have something similar for such queries?

qiaolin-pan avatar Jan 08 '19 13:01 qiaolin-pan

@pakyo-pan There's no FTS support in Watermelon right now, but it would be very nice to have it. If you want to research the issue and propose a solution, I'd be happy to take a look, give you pointers as for implementation, etc.

radex avatar Jan 10 '19 16:01 radex

If it's possible to access the underlying SQLite database, could FTS5 be bridged to work with this, or would that be more work than writing it from scratch?

Rob117 avatar Apr 01 '19 04:04 Rob117

@Rob117 sure, using SQLite capabilities is possible. Although it would be ideal if it could also work on the web (via LokiJS). But, of course, first things first. If you'd like to work on this, I can give you a few pointers

radex avatar Apr 01 '19 07:04 radex

Maybe Fuse.js would work better for that? Replicating the contents into Fuse on write/delete could provide a short-term solution, but it makes things tricky because there has to be a way to save relations between the generated documents and their original DB records.

Does WatermelonDB generate uuids for each record under the hood?

Rob117 avatar Apr 08 '19 02:04 Rob117

You mean for web only? Maybe… I would fear performance implications of just dumping lots of data into yet another library, but perhaps when it's just for one field, set up for a specific query, then it might work.

yes, watermelon generates unique ids for records, but not in UUID format, if that's what you mean

radex avatar Apr 12 '19 14:04 radex

@radex I'm going to be working on an app this year - let me see if I can get a solution rigged up. I'm much more likely to find something involving gluing Fuse to Watermelon, so I'll go that route to at least see what I can do.

Rob117 avatar Apr 29 '19 12:04 Rob117

@radex Is the db built with SQLite fts5 enabled?

If not, where would I change that flag in the source build?

Rob117 avatar Jun 17 '19 07:06 Rob117

I used FTS in another SQLite project and the FTS tables can live quite autonomously using SQLite triggers to update themselves.

I had the idea to introduce a schema field isSearchable and if set, that column would become FTS indexed. We'd need to add an additional Query type (or wait for #103) as well.

Not sure how to add that feature to the LokiJS adapter though.

stigi avatar Jun 17 '19 08:06 stigi

Here's an example that would add a sqlite FTS index for table message over the fields subject and body

-- Creating the FTS4 virtual table table
CREATE VIRTUAL TABLE "message_FTS" USING fts4(subject, body);

-- Populating message_FTS with existing content from message
INSERT INTO "message_FTS"(rowid, subject, body) SELECT rowid, subject, body from "message";

-- Setting up triggers to keep message_FTS current
CREATE TRIGGER message_FTS_DELETE AFTER DELETE ON message
   BEGIN
     DELETE FROM message_FTS WHERE rowid = OLD.rowid;
   END;
CREATE TRIGGER message_FTS_INSERT AFTER INSERT ON message
   BEGIN
     INSERT INTO message_FTS (rowid, subject, body) VALUES (NEW.rowid, NEW.subject, NEW.body);
   END;
CREATE TRIGGER message_FTS_UPDATE AFTER UPDATE ON message
   BEGIN
     UPDATE message_FTS SET subject = NEW.subject, body=NEW.body WHERE rowid = NEW.rowid;
   END;

The query would use a join like this message_FTS MATCH :filterQuery AND message_FTS.rowid == message.rowid.

I believe we based this on FTS4 which was widely enabled by default on Android and iOS.

stigi avatar Jun 17 '19 08:06 stigi

@radex Is the db built with SQLite fts5 enabled? If not, where would I change that flag in the source build?

🍉 uses system-supplied SQLite. I'm not sure if iOS and Android SQLite supports FTS. If so, let's just use that.

It would be great if 🍉 allowed the user to specify a custom SQLite build BTW. e.g. for encryption or other extensions.

I had the idea to introduce a schema field isSearchable and if set, that column would become FTS indexed. We'd need to add an additional Query type (or wait for #103) as well.

an isSearchable schema and migrations field seems to make sense to me, given the need for FTS to set up its indexer.

It would be best to have a Query for this. e.g. comments.query(Q.where('body', Q.textMatches('xxxxx'))) — not very difficult to implement, I can give pointers to all the necessary places :)

Not sure how to add that feature to the LokiJS adapter though.

Me neither. I'm OK with implementing this for only one platform first, if necessary — as long as there's an invariant / dev warning that this won't work.

There might be a dumb implementation for web that only works for exact matches Q.like('%foo%'').

Or we might use Fuse or something like it on the web worker thread to do the search

radex avatar Jun 21 '19 15:06 radex

Let me know how I can help you go about implementing this. We'd be very interested at @Nozbe to have full text search in 🍉 — no time to implement it ourselves, but I'm more than happy to give advice and pointers to where all the pieces need work

radex avatar Jun 21 '19 15:06 radex

I'm dabbling around on this branch https://github.com/stigi/WatermelonDB/tree/fts

Hope I can get something in a PR next week.

stigi avatar Jun 21 '19 19:06 stigi

hey @stigi any issue with FTS5? I have keen interest in adding this support for my RN app (so no Web support is fine with me). Any pointers and blockers in integrating it would be appreciated.

And heads up on any other ongoing effort @radex? It was nice meeting you at Chain React btw.

fungilation avatar Sep 13 '19 05:09 fungilation

@fungilation My understanding is that @stigi abandoned this work, and no one as far as I know is working on FTS currently, so I encourage you to take @stigi’s branch where he left off and continue this work :) You can start sending smaller pull requests and I wil be happy to review them and give pointers/feedback

radex avatar Sep 13 '19 19:09 radex

@fungilation I talked to @radex during the react-native eu conf. I stopped because the syntax around the query builder didn't come natural to me. I believe I got the table creation and propagation done and it's mostly only missing the query (See https://github.com/Nozbe/WatermelonDB/issues/197#issuecomment-502580179)

stigi avatar Sep 19 '19 18:09 stigi

I picked up @stigi branch and completed FTS feature.

A pull request was created: https://github.com/Nozbe/WatermelonDB/pull/623

Kenneth-KT avatar Feb 15 '20 05:02 Kenneth-KT

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Aug 13 '20 09:08 stale[bot]

Hi,

I am interested in this feature too. @Kenneth-KT's PR looks to be dropped, am I right ?

mlecoq avatar Sep 03 '20 11:09 mlecoq

@mlecoq Not at all, I hope that it's finished and then I can merge it. I'm unable to contact @Kenneth-KT and don't currently need this myself, so it's hanging on the list. If you need it, the best way for you to get this feature is to fork out the branch, finish up what needs to be done, and then I'll review it

radex avatar Sep 07 '20 07:09 radex

Ok, I will not use it right now, I think that I will start to implement search next March ou April. If PR is still not merged, I will create a new PR as you suggest. Thanks !

mlecoq avatar Sep 07 '20 07:09 mlecoq

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Jun 18 '21 23:06 stale[bot]

See #984

mlecoq avatar Jun 22 '21 19:06 mlecoq

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Apr 16 '22 17:04 stale[bot]