From 53fa5f43e5094660292d7f64f7343034bdc58d7d Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 1 Oct 2019 18:57:50 +0000 Subject: [PATCH] [CORL-325] Comment Search API (#2578) * feat: added comment search support * fix: updated readme * Update README.md --- README.md | 10 +++--- .../server/graph/tenant/loaders/Comments.ts | 10 ++++++ .../server/graph/tenant/schema/schema.graphql | 1 + .../migrations/1569955183249_text_indexes.ts | 35 +++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/core/server/services/migrate/migrations/1569955183249_text_indexes.ts diff --git a/README.md b/README.md index 5bf09d67a..3cb120be7 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Preview Coral easily by running Coral via a Heroku App: ## Requirements -- MongoDB >=3.6 +- MongoDB >=4.2 - Redis >=3.2 - NodeJS >=10 - NPM >=6.7 @@ -84,7 +84,7 @@ services: - REDIS_URI=redis://redis:6379 - SIGNING_SECRET= mongo: - image: mongo:3.6 + image: mongo:4.2 volumes: - ./data/mongo:/data/db redis: @@ -121,7 +121,7 @@ This should output all the compiled application code to `./dist`. Running Coral with default settings assumes that you have: -- MongoDB >=3.6 running on `127.0.0.1:27017` +- MongoDB >=4.2 running on `127.0.0.1:27017` - Redis >=3.2 running on `127.0.0.1:6379` If you don't already have these databases running, you can execute the following @@ -310,14 +310,14 @@ npm install Running Coral with default settings assumes that you have: -- MongoDB >=3.6 running on `127.0.0.1:27017` +- MongoDB >=4.2 running on `127.0.0.1:27017` - Redis >=3.2 running on `127.0.0.1:6379` If you don't already have these databases running, you can execute the following assuming you have Docker installed on your local machine: ```bash -docker run -d -p 27017:27017 --restart always --name mongo mongo:3.6 +docker run -d -p 27017:27017 --restart always --name mongo mongo:4.2 docker run -d -p 6379:6379 --restart always --name redis redis:3.2 ``` diff --git a/src/core/server/graph/tenant/loaders/Comments.ts b/src/core/server/graph/tenant/loaders/Comments.ts index 3f29c6989..a8e4c4c08 100644 --- a/src/core/server/graph/tenant/loaders/Comments.ts +++ b/src/core/server/graph/tenant/loaders/Comments.ts @@ -48,6 +48,14 @@ const tagFilter = (tag?: GQLTAG): CommentConnectionInput["filter"] => { return {}; }; +const queryFilter = (query?: string): CommentConnectionInput["filter"] => { + if (query) { + return { $text: { $search: `"${query}"` } }; + } + + return {}; +}; + /** * primeCommentsFromConnection will prime a given context with the comments * retrieved via a connection. @@ -123,6 +131,7 @@ export default (ctx: Context) => ({ storyID, status, tag, + query, }: QueryToCommentsArgs) => retrieveCommentConnection(ctx.mongo, ctx.tenant.id, { first, @@ -130,6 +139,7 @@ export default (ctx: Context) => ({ orderBy: GQLCOMMENT_SORT.CREATED_AT_DESC, filter: omitBy( { + ...queryFilter(query), ...tagFilter(tag), storyID, status, diff --git a/src/core/server/graph/tenant/schema/schema.graphql b/src/core/server/graph/tenant/schema/schema.graphql index 7030eced4..4e13f4687 100644 --- a/src/core/server/graph/tenant/schema/schema.graphql +++ b/src/core/server/graph/tenant/schema/schema.graphql @@ -2523,6 +2523,7 @@ type Query { storyID: ID status: COMMENT_STATUS tag: TAG + query: String ): CommentsConnection! @auth(roles: [ADMIN, MODERATOR]) """ diff --git a/src/core/server/services/migrate/migrations/1569955183249_text_indexes.ts b/src/core/server/services/migrate/migrations/1569955183249_text_indexes.ts new file mode 100644 index 000000000..43918cdf5 --- /dev/null +++ b/src/core/server/services/migrate/migrations/1569955183249_text_indexes.ts @@ -0,0 +1,35 @@ +import { Db } from "mongodb"; + +import Migration from "coral-server/services/migrate/migration"; +import collections from "coral-server/services/mongodb/collections"; + +import { createConnectionOrderVariants, createIndexFactory } from "../indexing"; + +export default class extends Migration { + public async indexes(mongo: Db) { + const createIndex = createIndexFactory(collections.comments(mongo)); + + // Text searches. + await createIndex( + { + tenantID: 1, + "revisions.body": "text", + }, + { background: true } + ); + + const variants = createConnectionOrderVariants(createIndex, [ + { createdAt: -1 }, + { createdAt: 1 }, + { childCount: -1, createdAt: -1 }, + { "actionCounts.REACTION": -1, createdAt: -1 }, + ]); + + // Story based Comment Connection pagination. + // { storyID, ...connectionParams } + await variants({ + tenantID: 1, + storyID: 1, + }); + } +}