Skip to content

Commit ed26abf

Browse files
committed
use fuzzy text search in main torrent search
1 parent 8eb6083 commit ed26abf

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

api/src/controllers/torrent.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bencode from "bencode";
22
import crypto from "crypto";
33
import mongoose from "mongoose";
4-
import { createNGrams } from "mongoose-fuzzy-searching/helpers";
4+
import { createNGrams, nGrams } from "mongoose-fuzzy-searching/helpers";
55
import slugify from "slugify";
66
import Torrent from "../schema/torrent";
77
import User from "../schema/user";
@@ -463,7 +463,23 @@ export const getTorrentsPage = async ({
463463
userId,
464464
tracker,
465465
}) => {
466+
const queryNGrams = nGrams(query, false, 2, false).join(" ");
467+
466468
const torrents = await Torrent.aggregate([
469+
...(query
470+
? [
471+
{
472+
$match: {
473+
$text: {
474+
$search: queryNGrams,
475+
},
476+
},
477+
},
478+
{
479+
$addFields: { confidenceScore: { $meta: "textScore" } },
480+
},
481+
]
482+
: []),
467483
{
468484
$project: {
469485
infoHash: 1,
@@ -476,6 +492,7 @@ export const getTorrentsPage = async ({
476492
created: 1,
477493
freeleech: 1,
478494
tags: 1,
495+
confidenceScore: 1,
479496
},
480497
},
481498
...(Array.isArray(ids)
@@ -485,18 +502,6 @@ export const getTorrentsPage = async ({
485502
},
486503
]
487504
: []),
488-
...(query
489-
? [
490-
{
491-
$match: {
492-
$or: [
493-
{ name: { $regex: query, $options: "i" } },
494-
{ description: { $regex: query, $options: "i" } },
495-
],
496-
},
497-
},
498-
]
499-
: []),
500505
...(category
501506
? [
502507
{
@@ -534,7 +539,9 @@ export const getTorrentsPage = async ({
534539
]
535540
: []),
536541
{
537-
$sort: { created: -1 },
542+
$sort: query
543+
? { confidenceScore: { $meta: "textScore" } }
544+
: { created: -1 },
538545
},
539546
{
540547
$skip: skip,
@@ -592,23 +599,24 @@ export const getTorrentsPage = async ({
592599
{ $unwind: { path: "$fetchedBy", preserveNullAndEmptyArrays: true } },
593600
]);
594601

602+
console.log(torrents);
603+
595604
const [count] = await Torrent.aggregate([
596-
...(Array.isArray(ids)
605+
...(query
597606
? [
598607
{
599-
$match: { expr: { $in: ["$_id", ids] } },
608+
$match: {
609+
$text: {
610+
$search: queryNGrams,
611+
},
612+
},
600613
},
601614
]
602615
: []),
603-
...(query
616+
...(Array.isArray(ids)
604617
? [
605618
{
606-
$match: {
607-
$or: [
608-
{ name: { $regex: query, $options: "i" } },
609-
{ description: { $regex: query, $options: "i" } },
610-
],
611-
},
619+
$match: { expr: { $in: ["$_id", ids] } },
612620
},
613621
]
614622
: []),

client/pages/search/[[...query]].js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ const Search = ({ results, error }) => {
3535
{query ? `Search results for “${query}”` : "Search"}
3636
</Text>
3737
<Box as="form" onSubmit={handleSearch} display="flex" mb={5}>
38-
<Input
39-
placeholder="Search torrents (text or RegExp)"
40-
name="query"
41-
mr={3}
42-
required
43-
/>
38+
<Input name="query" mr={3} required />
4439
<Button>Search</Button>
4540
</Box>
4641
{error ? (

0 commit comments

Comments
 (0)