From 66dbf2496e659296f8f49abedfae28af0ae655b3 Mon Sep 17 00:00:00 2001 From: Abhi Shandilya Date: Tue, 15 Apr 2025 19:23:41 -0400 Subject: [PATCH] fix: url search (#2083) * fix: url search * refine --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b --- api/resolvers/search.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/api/resolvers/search.js b/api/resolvers/search.js index 975066ee..637ab8a9 100644 --- a/api/resolvers/search.js +++ b/api/resolvers/search.js @@ -1,6 +1,7 @@ import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor' import { whenToFrom } from '@/lib/time' import { getItem, itemQueryWithMeta, SELECT } from './item' +import { parse } from 'tldts' function queryParts (q) { const regex = /"([^"]*)"/gm @@ -253,24 +254,17 @@ export default { // if search contains a url term, modify the query text if (url) { - const uri = url.slice(4) - let uriObj - try { - uriObj = new URL(uri) - } catch { - try { - uriObj = new URL(`https://${uri}`) - } catch {} - } - - if (uriObj) { - termQueries.push({ - wildcard: { url: `*${uriObj?.hostname ?? uri}${uriObj?.pathname ?? ''}*` } - }) - termQueries.push({ - match: { text: `${uriObj?.hostname ?? uri}${uriObj?.pathname ?? ''}` } - }) + let uri = url.slice(4) + termQueries.push({ + match_bool_prefix: { url: { query: uri, operator: 'and', boost: 1000 } } + }) + const parsed = parse(uri) + if (parsed?.subdomain?.length > 0) { + uri = uri.replace(`${parsed.subdomain}.`, '') } + termQueries.push({ + wildcard: { url: { value: `*${uri}*` } } + }) } // if nym, items must contain nym