-
Notifications
You must be signed in to change notification settings - Fork 3.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
SQLite full-text search support #4823
Comments
combined into #1590 |
Un-combining. |
You are talking about fts 3 and 4, will you also support 5? |
Now that SQLitePCL.raw supports FTS5, we'd probably just start with support for that. |
Some notes:
|
Daydreaming about this some more: LINQ like the this... from e in db.Emails
where EF.Functions.Match(e, query)
orderby EF.Functions.Bm25(e)
select new
{
e.Id,
Subject = EF.Functions.Highlight(e.Subject, "<b>", "</b>"),
Body = EF.Functions.Snippet(e.Body, "<b>", "</b>", "...", 64)
}; ...would produce SQL like this. select
id,
highlight(email, subject, '<b>', '</b>') as subject,
snippet(email, body, '<b>', '</b>', '...', 64) as body
from email
where match(email, $query)
order by bm25(email); Notice how expressions like Obviously, |
Note, some functionality is already possible in EF Core today.
from e in db.Emails
where e.Body == query
orderby e.Rank
select e; |
would order by bm25 also work for special letters like umlauts: äöü or something like ß? |
After realizing that all these great features and "daydreams" have not become part of the latest efcore version, is there any announcement when (or if) one can expect this will part of a future version? |
@Kalle4242 Yes: 👍 on the issue. We will be publishing a plan for EF7 soon. See release planning for more info. |
I have a problem calling a select/search statement with FromSqlRaw on a fts5 virtual table. I have two test projects, both using the same model, but I get different results calling the same statement: protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"CREATE VIRTUAL TABLE AssetSearch USING fts5(AssetCommonId UNINDEXED, AssetVersionId
UNINDEXED, AssetPartId UNINDEXED, Title, Body, Comment, Notice)");
}
public IQueryable<AssetSearchDbo> Search(string question)
{
string query =
"SELECT " +
" \"AssetSearch\".RowId " +
" , \"AssetSearch\".AssetCommonId " +
" , \"AssetSearch\".AssetVersionId " +
" , \"AssetSearch\".AssetPartId " +
" , snippet( \"AssetSearch\", 3, '>>', '<<', '...', 8 ) As Title " +
" , snippet( \"AssetSearch\", 4, '>>', '<<', '...', 8 ) As Body " +
" , snippet( \"AssetSearch\", 5, '>>', '<<', '...', 8 ) As Comment " +
" , snippet( \"AssetSearch\", 6, '>>', '<<', '...', 8 ) AS Notice " +
" , bm25(\"AssetSearch\") As Rank " +
" FROM \"AssetSearch\" " +
$" WHERE \"AssetSearch\" MATCH ' - {{{{AssetCommonId AssetVersionId AssetPartId}}}} : {question}' " +
" ORDER BY Rank";
IQueryable<AssetSearchDbo> searchResults = AssetSearch.FromSqlRaw<AssetSearchDbo>(query);
return searchResults;
} When inspecting the searchResults in one case an enumeration of AssetSearchDbo objects is returned and the results of snippet and rank functions are not computed. The snippet fields contain the entire field values without tagging the "match term" and rank is null. Can anybody imagine which difference makes it work either this or that way? Maybe there is someone knowing more about the combination of efcore, lazy loading and fts5. |
I'm working on it myself. A difference between the two test project is, that in first on the context is kept open during the entire test. In the second one a new DbContext is created for each test method. |
Do you have any sample project on how to use FTS5 on SQLite EF .NET Core? I followed https://www.bricelam.net/2020/08/08/sqlite-fts-and-efcore.html Can you share the migration and the data model created by this? |
Sorry. I didn't manage to run Brice's example. It also didn't match my requirements. The search content of my solution is not in a database table. My database is a metadata database for different assets which our solution handles. The search data is extracted from the content of those assets and only should be separately stored to fts5 tables for full-text-search purpose. It is not required, so it is superfluous in the metadata database. |
Thanks, @Kalle4242. I looked at the Vahid's project and also your's suggestions in mind and I was able to create first working solution. |
SQLite's FTS3 and FTS4 (full-text search) extension modules allow users to create special tables with a built-in full-text index. This extension is usually enabled by default. (I checked: most platforms we support have a version of SQLite compiled with ENABLE_FTS3).
To leverage this, the create table syntax and query syntax are slightly different.
To enable this in EF, we would need to add some kind of configuration option to specific which tables are FTS and make query respond accordingly.
In the meantime, users can work around this with
MigrationBuilder.Sql
andFromSql
.More docs: https://www.sqlite.org/fts3.html
The text was updated successfully, but these errors were encountered: