Add FTS snippet functionality

Open
#367 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
sqlite, swift
Domain
database

Research direction

Start with the VirtualTable, Expression, and QueryType APIs used in the example, then trace how .match("cold") builds and selects expressions. Compare that behavior with SQLite FTS3/4 snippet and matchInfo queries. Done means callers can select these FTS results and receive SQLite’s matched snippet or match information rather than a rendered expression description.

Written by the indexing model from the issue text.

Description

enhancement

SQLite when using the FTS3/4 extensions can support the concept of a snippet. Essentially the snippet is a subset of the indexed column, that has html bold tags surrounding the matched terms.

Source information here: https://www.sqlite.org/fts3.html#section_4_2

So using the example from the above link, a snippet query it might look something like:

SELECT snippet(text) FROM text WHERE text MATCH 'cold';

I have successfully used the VirtualTable implementation to retrieve matching records using the .match(string: String) function, but would like to be able to select the snippet as well.

My current implementation attempt is:

let db = try? Connection("table.database")
let indexTable = VirtualTable("fts_index")
let guidField = Expression<String>("object_guid")
let contentsField = Expression<String>("contents")
let snippet = Expression<String>("snippet(contents)")
let _ = try? db?.run(indexTable.create(.FTS4(guidField, contentsField)))

let matchQuery: QueryType =indexTable.select(snippet, guidField, contentsField).match("cold")
let results = try? db?.prepare(matchQuery)

if let concreteResults = results {
  let _ = concreteResults.flatMap( { 
    print("\($0[guidField]), \($0[snippet]), \($0[contentsField])")
  })
}

I get what I expect for both guidField and contentsField, yet the result of snippet is something puzzling.

3771714D-19AB-464B-80E2-6C5A798D8104, snippet(Expression<String>(template: "\"contents\"", bindings: [])), This is a cold record

I imagine I just need a custom expression, but I am not sure how to implement custom SQLite functions, or else I may just be missing it in the docs.

Thanks for any help, and I would be glad once I know how to implement this for both the snippet and matchInfo capability in SQLite.

Thanks, awesome library and really enjoying using it.

Dominant language
Swift
Stars
10.2k
Forks
1.6k
Avg merge
6d 2h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stephencelis/SQLite.swift

All issues in stephencelis/SQLite.swift

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.