[CORL-325] Comment Search API (#2578)

* feat: added comment search support

* fix: updated readme

* Update README.md
This commit is contained in:
Wyatt Johnson
2019-10-01 18:57:50 +00:00
committed by GitHub
parent c045f52daa
commit 53fa5f43e5
4 changed files with 51 additions and 5 deletions
@@ -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,
@@ -2523,6 +2523,7 @@ type Query {
storyID: ID
status: COMMENT_STATUS
tag: TAG
query: String
): CommentsConnection! @auth(roles: [ADMIN, MODERATOR])
"""
@@ -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,
});
}
}