Skip to content

Commit

Permalink
feat(query): introduce fts_term() function for preparing tokens for F…
Browse files Browse the repository at this point in the history
…TS5 token matching
  • Loading branch information
missinglink committed Dec 13, 2021
1 parent a60df73 commit bfcdc8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
18 changes: 13 additions & 5 deletions lib/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ module.exports.matchSubjectObjectGeomIntersects = function( subject, object, cb
if( 0 > RTREE_THRESHOLD ){ return cb( null, [] ); }

if( isPartialToken ){
object = object.replace(/ /g, '_').replace(REMOVE_PARTIAL_TOKEN_REGEX, '');
object = object.replace(REMOVE_PARTIAL_TOKEN_REGEX, '');
if( '' === object.trim() ){ return cb( null, [] ); }

this._queryAll(
this.prepare( query.match_subject_object_geom_intersects_autocomplete ),
{
subject,
object,
subject_quoted: `"${subject}"`,
object_quoted: `"${object}"`,
subject_fts: fts_term(subject),
object_fts: fts_term(object),
threshold: RTREE_THRESHOLD,
limit: MAX_RESULTS
},
Expand All @@ -169,12 +169,20 @@ module.exports.matchSubjectObjectGeomIntersects = function( subject, object, cb
{
subject,
object,
subject_quoted: `"${subject}"`,
object_quoted: `"${object}"`,
subject_fts: fts_term(subject),
object_fts: fts_term(object),
threshold: RTREE_THRESHOLD,
limit: MAX_RESULTS
},
cb
);
}
};

// fts_term() converts a regular term (of one or more tokens)
// into a format which can be used by the FTS5 table.
// 1. terms should be enclosed in double-quotes
// 2. spaces should be replaced with underscores (as per the analyzer)
function fts_term(term) {
return `"${term.replace(/ /g, '_')}"`;
}
4 changes: 2 additions & 2 deletions query/match_subject_object_geom_intersects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FROM fulltext f1
(r1.minY - $threshold) < r2.maxY AND
(r1.maxY + $threshold) > r2.minY
)
JOIN fulltext AS f2 ON f2.fulltext MATCH $object_quoted
JOIN fulltext AS f2 ON f2.fulltext MATCH $object_fts
JOIN tokens t2 ON (
f2.rowid = t2.rowid
AND r2.id = t2.id
Expand All @@ -22,7 +22,7 @@ FROM fulltext f1
t2.lang IN ('eng', 'und')
)
)
WHERE f1.fulltext MATCH $subject_quoted
WHERE f1.fulltext MATCH $subject_fts
AND t1.token = $subject
GROUP BY t1.id, t2.id
ORDER BY t1.id ASC, t2.id ASC
Expand Down
4 changes: 2 additions & 2 deletions query/match_subject_object_geom_intersects_autocomplete.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FROM fulltext f1
(r1.minY - $threshold) < r2.maxY AND
(r1.maxY + $threshold) > r2.minY
)
JOIN fulltext AS f2 ON f2.fulltext MATCH $object_quoted OR $object_quoted*
JOIN fulltext AS f2 ON f2.fulltext MATCH $object_fts OR $object_fts*
JOIN tokens t2 ON (
f2.rowid = t2.rowid
AND r2.id = t2.id
Expand All @@ -22,7 +22,7 @@ FROM fulltext f1
t2.lang IN ('eng', 'und')
)
)
WHERE f1.fulltext MATCH $subject_quoted
WHERE f1.fulltext MATCH $subject_fts
AND t1.token = $subject
GROUP BY t1.id, t2.id
ORDER BY t1.id ASC, t2.id ASC
Expand Down

0 comments on commit bfcdc8b

Please sign in to comment.