Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

add negative queries

daniel31x13bbe702925be2
diff --git a/.env.sample b/.env.sample
index e0d7a52f..8054e223 100644
--- a/.env.sample
+++ b/.env.sample
@@ -39,7 +39,8 @@ MAX_WORKERS=
DISABLE_PRESERVATION=
NEXT_PUBLIC_RSS_POLLING_INTERVAL_MINUTES=
RSS_SUBSCRIPTION_LIMIT_PER_USER=
-SLICE_TEXT_CONTENT=
+TEXT_CONTENT_LIMIT=
+SEARCH_QUERY_LIMIT=
INDEX_TAKE_COUNT=
# AI Settings
diff --git a/lib/api/preservationScheme/handleReadability.ts b/lib/api/preservationScheme/handleReadability.ts
index 992ee917..2644ff2d 100644
--- a/lib/api/preservationScheme/handleReadability.ts
+++ b/lib/api/preservationScheme/handleReadability.ts
@@ -10,6 +10,8 @@ const handleReadability = async (
link: Link,
keepContent?: boolean
) => {
+ const TEXT_CONTENT_LIMIT = Number(process.env.TEXT_CONTENT_LIMIT);
+
const window = new JSDOM("").window;
const purify = DOMPurify(window);
const cleanedUpContent = purify.sanitize(content);
@@ -19,7 +21,7 @@ const handleReadability = async (
const articleText = article?.textContent
.replace(/ +(?= )/g, "") // strip out multiple spaces
.replace(/(\r\n|\n|\r)/gm, " ") // strip out line breaks
- .slice(0, process.env.SLICE_TEXT_CONTENT === "true" ? 10000 : undefined); // limit to 10,000 characters if SLICE_TEXT_CONTENT is true
+ .slice(0, TEXT_CONTENT_LIMIT ? TEXT_CONTENT_LIMIT : undefined); // limit characters if TEXT_CONTENT_LIMIT is defined
if (articleText && articleText !== "") {
const collectionId = (
diff --git a/lib/api/searchQueryBuilder.ts b/lib/api/searchQueryBuilder.ts
index 01ee504d..7720831f 100644
--- a/lib/api/searchQueryBuilder.ts
+++ b/lib/api/searchQueryBuilder.ts
@@ -1,66 +1,80 @@
interface Token {
field: string;
value: string;
+ isNegative: boolean;
}
+const SEARCH_CONDITIONS = [
+ "url",
+ "name",
+ "description",
+ "type",
+ "collection",
+ "pinned",
+ "public",
+ "before",
+ "after",
+ "tag",
+];
+
export function parseSearchTokens(searchQueryString: string): Token[] {
// Split on whitespace
- const rawTokens = searchQueryString.match(/\S+/g) || [];
+ const rawTokens =
+ searchQueryString.match(/\\?.|^$/g)?.reduce(
+ (p, c) => {
+ if (c === '"' || c === "'") {
+ p.quote ^= 1;
+ } else if (!p.quote && c === " ") {
+ p.str.push("");
+ } else {
+ p.str[p.str.length - 1] += c.replace(/\\(.)/, "$1");
+ }
+ return p;
+ },
+ { str: [""], quote: 0 }
+ ).str || [];
const tokens: Token[] = [];
- for (const token of rawTokens) {
- if (token.startsWith("url:") && token.length > "url:".length) {
- tokens.push({ field: "url", value: token.substring("url:".length) });
- } else if (token.startsWith("name:") && token.length > "name:".length) {
- tokens.push({ field: "name", value: token.substring("name:".length) });
- } else if (
- token.startsWith("description:") &&
- token.length > "description:".length
- ) {
- tokens.push({
- field: "description",
- value: token.substring("description:".length),
- });
- } else if (token.startsWith("type:") && token.length > "type:".length) {
- tokens.push({ field: "type", value: token.substring("type:".length) });
- } else if (token.startsWith("before:") && token.length > "before:".length) {
- tokens.push({
- field: "before",
- value: token.substring("before:".length),
- });
- } else if (token.startsWith("after:") && token.length > "after:".length) {
- if (token.startsWith("after:")) {
+ for (let token of rawTokens) {
+ let isNegative = false;
+
+ if (token.startsWith("!") && token.length > 1) {
+ const valueAfterNegation = token.substring(1);
+
+ if (
+ SEARCH_CONDITIONS.some((field) =>
+ valueAfterNegation.startsWith(`${field}:`)
+ )
+ ) {
+ isNegative = true;
+ token = valueAfterNegation;
+ }
+ }
+
+ let match = false;
+
+ for (const field of SEARCH_CONDITIONS) {
+ if (token.startsWith(`${field}:`) && token.length > `${field}:`.length) {
tokens.push({
- field: "after",
- value: token.substring("after:".length),
+ field,
+ value: token.substring(`${field}:`.length),
+ isNegative,
});
+ match = true;
+ break;
}
- } else if (
- token.startsWith("collection:") &&
- token.length > "collection:".length
- ) {
- tokens.push({
- field: "collection",
- value: token.substring("collection:".length),
- });
- } else if (token === "pinned:true" || token === "pinned:false") {
- tokens.push({
- field: "pinned",
- value: token.substring("pinned:".length).toLowerCase(),
- });
- } else if (token.startsWith("#") && token.length > 1) {
- tokens.push({ field: "tag", value: token.substring(1) });
- } else if (token === "public:true") {
- tokens.push({
- field: "public",
- value: token.substring("public:".length).toLowerCase(),
- });
- } else {
+ }
+
+ if (!match) {
// everything else -> 'general' text
- tokens.push({ field: "general", value: token });
+ tokens.push({ field: "general", value: token, isNegative });
}
}
+ const SEARCH_QUERY_LIMIT = Number(process.env.SEARCH_QUERY_LIMIT);
+
+ if (SEARCH_QUERY_LIMIT) return tokens.slice(0, SEARCH_QUERY_LIMIT);
+
return tokens;
}
@@ -77,55 +91,107 @@ export function buildMeiliFilters(tokens: Token[], userId: number): string[] {
`(collectionOwnerId = ${userId}) OR (collectionMemberIds = ${userId})`,
];
- for (const { field, value } of tokens) {
+ for (const { field, value, isNegative } of tokens) {
switch (field) {
case "url":
- filters.push(`url = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT url = "${escapeForMeilisearch(value)}"`
+ : `url = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "name":
- filters.push(`name = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT name = "${escapeForMeilisearch(value)}"`
+ : `name = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "description":
- filters.push(`description = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT description = "${escapeForMeilisearch(value)}"`
+ : `description = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "type":
- filters.push(`type = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT type = "${escapeForMeilisearch(value)}"`
+ : `type = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "collection":
- filters.push(`collectionName = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT collectionName = "${escapeForMeilisearch(value)}"`
+ : `collectionName = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "pinned":
if (value === "true") {
- filters.push(`pinnedBy = ${userId}`);
+ filters.push(
+ isNegative ? `NOT pinnedBy = ${userId}` : `pinnedBy = ${userId}`
+ );
} else if (value === "false") {
- filters.push(`NOT pinnedBy = ${userId}`);
+ filters.push(
+ isNegative ? `pinnedBy = ${userId}` : `NOT pinnedBy = ${userId}`
+ );
}
break;
+
case "public":
if (value === "true") {
- filters.push(`collectionIsPublic = true`);
+ filters.push(
+ isNegative
+ ? `NOT collectionIsPublic = true`
+ : `collectionIsPublic = true`
+ );
}
break;
+
case "before":
if (!isNaN(Date.parse(value))) {
const creationTimestamp = Date.parse(value) / 1000;
- filters.push(`creationTimestamp < ${creationTimestamp}`);
+ filters.push(
+ isNegative
+ ? `creationTimestamp >= ${creationTimestamp}`
+ : `creationTimestamp < ${creationTimestamp}`
+ );
}
break;
+
case "after":
if (!isNaN(Date.parse(value))) {
const creationTimestamp = Date.parse(value) / 1000;
- filters.push(`creationTimestamp > ${creationTimestamp}`);
+ filters.push(
+ isNegative
+ ? `creationTimestamp <= ${creationTimestamp}`
+ : `creationTimestamp > ${creationTimestamp}`
+ );
}
break;
+
case "tag":
- filters.push(`tags = "${escapeForMeilisearch(value)}"`);
+ filters.push(
+ isNegative
+ ? `NOT tags = "${escapeForMeilisearch(value)}"`
+ : `tags = "${escapeForMeilisearch(value)}"`
+ );
break;
+
case "general":
default:
+ // "general" text is handled by the main query, not by filters
break;
}
}
+
return filters;
}