Browse files

updated function for weighted columns

  • Loading branch information...
1 parent af4e526 commit d3b959579463d83b99d7934338958d7aee457cc4 @coolaj86 committed Jul 26, 2012
Showing with 48 additions and 5 deletions.
  1. +4 −5 fts4-rank.c
  2. +44 −0 test-rank-weighted-columns.sql
View
9 fts4-rank.c
@@ -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 );
@@ -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. */
@@ -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;
}
View
44 test-rank-weighted-columns.sql
@@ -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.