Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce fts_term() function for preparing tokens for FTS5 token matching #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -4,7 +4,7 @@ SELECT
FROM fulltext f1
JOIN tokens t1 ON (
f1.rowid = t1.rowid
AND f1.fulltext MATCH $subject_quoted
AND f1.fulltext MATCH $subject_fts
AND LIKELY(t1.token = $subject)
)
JOIN rtree AS r1 ON t1.id = r1.id
Expand All @@ -15,7 +15,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 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 @@ -4,7 +4,7 @@ SELECT
FROM fulltext f1
JOIN tokens t1 ON (
f1.rowid = t1.rowid
AND f1.fulltext MATCH $subject_quoted
AND f1.fulltext MATCH $subject_fts
AND LIKELY(t1.token = $subject)
)
JOIN rtree AS r1 ON t1.id = r1.id
Expand All @@ -15,7 +15,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 Down