iOS Question Does SQlite have search ranking like BM25

tsteward

Well-Known Member
Licensed User
Longtime User
As the title asks I would like to display records with best match first.
Dor searching for tree words would return exact matches first, then search for each word etc.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Apparently fts5 is available in iOS. You can see it with this code:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim sql As SQL
    sql.Initialize(File.DirDocuments, "test", True)
    Dim cr As ResultSet = sql.ExecQuery("PRAGMA compile_options")
    Do While cr.NextRow
        Log(cr.GetString2(0))
    Loop
    sql.ExecNonQuery("DROP TABLE IF EXISTS posts")
    sql.ExecNonQuery($"CREATE VIRTUAL TABLE posts
                        USING FTS5(title, body)"$)
    sql.ExecNonQuery($"INSERT INTO posts(title,body)
                    VALUES('Learn SQlite FTS5','This tutorial teaches you how to perform full-text search in SQLite using FTS5'),
                    ('Advanced SQlite Full-text Search','Show you some advanced techniques in SQLite full-text searching'),
                    ('SQLite Tutorial','Help you learn SQLite quickly and effectively')"$)
    Dim rs As ResultSet = sql.ExecQuery($"SELECT *
                    FROM posts 
                    WHERE posts MATCH 'text' 
                    ORDER BY rank"$)
    Do While rs.NextRow
        Log(rs.GetString2(0) & ": " & rs.GetString2(1))
    Loop
End Sub

Example based on: https://www.sqlitetutorial.net/sqlite-full-text-search/
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…