Skip to content

Commit

Permalink
updated function for weighted columns
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ ONeal committed Jul 26, 2012
1 parent af4e526 commit d3b9595
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
9 changes: 4 additions & 5 deletions fts4-rank.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
int nPhrase; /* Number of phrases in the query */
int iPhrase; /* Current phrase */
double score = 0.0; /* Value to return */
double weight = 1.0;
double defaultWeight = 1.0;

assert( sizeof(int)==4 );

Expand All @@ -52,13 +52,10 @@ static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
** nPhrase to contain the number of reportable phrases in the users full-text
** query, and nCol to the number of columns in the table.
*/
if( nVal<1 || nVal>2) goto wrong_number_args;
aMatchinfo = (unsigned int *)sqlite3_value_blob(apVal[0]);
nPhrase = aMatchinfo[0];
nCol = aMatchinfo[1];
if (2 == nVal) {
weight = sqlite3_value_double(apVal[1]);
}
if ( (nVal - 1) > nCol ) goto wrong_number_args;


/* Iterate through each phrase in the users query. */
Expand All @@ -78,6 +75,8 @@ static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
for(iCol=0; iCol<nCol; iCol++){
int nHitCount = aPhraseinfo[3*iCol];
int nGlobalHitCount = aPhraseinfo[3*iCol+1];
double weight = defaultWeight;
if ( (1 + iCol) < nVal ) weight = sqlite3_value_double(apVal[1 + iCol]);
if( nHitCount>0 ){
score += ((double)nHitCount / (double)nGlobalHitCount) * weight;
}
Expand Down
44 changes: 44 additions & 0 deletions test-rank-weighted-columns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.load fts4-rank.sqlext

-- This example (and all others in this section) assumes the following schema
CREATE VIRTUAL TABLE documents USING fts4(title, content);

INSERT INTO documents VALUES('hello world', 'This message is a hello world message.');
INSERT INTO documents VALUES('urgent: serious', 'This mail is seen as a more serious mail');

SELECT title FROM documents WHERE documents MATCH 'message';

-- result should be 3.0
SELECT rank(matchinfo(documents)) AS rank
FROM documents
WHERE documents MATCH '"serious OR mail"'
ORDER BY rank DESC
LIMIT 10 OFFSET 0;

-- result should be 2.0
SELECT rank(matchinfo(documents), 2.0) AS rank
FROM documents
WHERE documents MATCH '"serious OR mail"'
ORDER BY rank DESC
LIMIT 10 OFFSET 0;

-- result should be 1.0
SELECT rank(matchinfo(documents), 0.0, 0.5) AS rank
FROM documents
WHERE documents MATCH '"serious OR mail"'
ORDER BY rank DESC
LIMIT 10 OFFSET 0;

-- result should be 3.8
SELECT rank(matchinfo(documents), 1.2, 1.3) AS rank
FROM documents
WHERE documents MATCH '"serious OR mail"'
ORDER BY rank DESC
LIMIT 10 OFFSET 0;

/* should fail */
SELECT rank(matchinfo(documents), 0.0, 0.0, 1.7) AS rank
FROM documents
WHERE documents MATCH '"serious OR mail"'
ORDER BY rank DESC
LIMIT 10 OFFSET 0;

0 comments on commit d3b9595

Please sign in to comment.