Example showing how to work with matchinfo (SQLite3)
Posted: Wed 11 Feb 2015 01:19
Hi,
Is it possible to show an example of how to work with the blob data returned by SQLite matchinfo function (http://www.sqlite.org/fts3.html)?
I found this snippet from mORMot framework, but could not figure out how to use it with UniDAC user function signature.
Essentially, assuming user is searching for one term in fts table, I would like to get how many occurrences there are for current row, e.g.:
Can you provide some assistance?
Thank you
Rael
Is it possible to show an example of how to work with the blob data returned by SQLite matchinfo function (http://www.sqlite.org/fts3.html)?
I found this snippet from mORMot framework, but could not figure out how to use it with UniDAC user function signature.
Code: Select all
// from SynSQLite3.pas
PFTSMatchInfo = ^TFTSMatchInfo;
/// map the matchinfo function returned BLOB value
// - i.e. the default 'pcx' layout, for both FTS3 and FTS4
// - see http://www.sqlite.org/fts3.html#matchinfo
// - used for the FTS3/FTS4 ranking of results by TSQLRest.FTSMatch method
// and the internal RANK() function as proposed in
// http://www.sqlite.org/fts3.html#appendix_a
TFTSMatchInfo = record
nPhrase: integer;
nCol: integer;
hits: array[1..9] of record
this_row: integer;
all_rows: integer;
docs_with_hits: integer;
end;
end;
procedure InternalRank(Context: TSQLite3FunctionContext;
argc: integer; var argv: TSQLite3ValueArray); {$ifndef SQLITE3_FASTCALL}cdecl;{$endif}
// supplies the same "RANK" internal function as proposed in
// http://www.sqlite.org/fts3.html#appendix_a
var MI: PFTSMatchInfo;
p,c: integer;
score: Double;
begin
if argc>=1 then begin
MI := sqlite3.value_blob(argv[0]);
if argc=MI^.nCol+1 then begin
score := 0;
for p := 1 to MI^.nPhrase do
for c := 1 to MI^.nCol do
with MI^.hits[c] do
if this_row>0 then
score := score+(this_row/all_rows)*sqlite3.value_double(argv[c]);
sqlite3.result_double(Context,score);
exit; // success: don't call sqlite3.result_error()
end;
end;
ErrorWrongNumberOfArgs(Context);
end;
Code: Select all
SELECT docid, rank(matchinfo(table_fts)) FROM table_fts WHERE table_fts MATCH 'myterm'
Thank you
Rael