Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ services:
networks:
- social-network

elasticsearch:
container_name: social-es
image: docker.elastic.co/elasticsearch/elasticsearch:7.11.0
environment:
- node.name=elasticsearch
- cluster.name=es-docker-cluster
- discovery.seed_hosts=elasticsearch
- cluster.initial_master_nodes=elasticsearch
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
ports:
- 9200:9200
volumes:
- /data/elasticsearch/
networks:
- social-network

networks:
social-network:
driver: bridge
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"author": "trananh22112001@gmail.com",
"license": "ISC",
"dependencies": {
"@elastic/elasticsearch": "^7.11.0",
"@nestjs/common": "^7.0.5",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^7.0.5",
"@nestjs/cqrs": "^9.0.1",
"@nestjs/elasticsearch": "^7.1",
"@nestjs/platform-express": "^7.0.5",
"@nestjs/swagger": "^4.4.0",
"@nestjs/testing": "^7.0.5",
Expand Down
2 changes: 2 additions & 0 deletions src/article/application/events/event.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserEntity } from "../../../user/core/entities/user.entity";
import { ArticleEntity, BlockEntity } from "../../core";
import { CommentEntity } from "../../core/entities/comment.entity";
import { EventHandlers } from ".";
import { ElasticSearchModule } from "../../../elastic-search/elastic-search.module";

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { EventHandlers } from ".";
READ_CONNECTION
),
CqrsModule,
ElasticSearchModule,
],
providers: [...EventHandlers],
controllers: [],
Expand Down
2 changes: 2 additions & 0 deletions src/article/application/events/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export * from "./article-deleted.handler";
export * from "./article-favorited.handler";
export * from "./article-unfavorited.handler";

export * from "./indexing-article.handler";

export * from "./comment-created.handler";
export * from "./comment-deleted.handler";
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IEventHandler } from "@nestjs/cqrs";
import { EventsHandler } from "@nestjs/cqrs/dist/decorators/events-handler.decorator";
import { ArticleEntity } from "../../../core";
import { SearchService } from "../../../../elastic-search/elastic-search.service";

export class IndexingArticleEvent {
constructor(public readonly articles: ArticleEntity[]) {}
}

@EventsHandler(IndexingArticleEvent)
export class IndexingArticleEventHandler
implements IEventHandler<IndexingArticleEvent>
{
constructor(private readonly elasticSearch: SearchService) {}

async handle({ articles }: IndexingArticleEvent) {
await this.elasticSearch.bulkIndexArticles(articles);
}
}
3 changes: 3 additions & 0 deletions src/article/application/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ArticleUpdatedEventHandler,
CommentCreatedEventHandler,
CommentDeletedEventHandler,
IndexingArticleEventHandler,
} from "./handlers";

export * from "./handlers";
Expand All @@ -19,4 +20,6 @@ export const EventHandlers = [
ArticleUnFavoritedEventHandler,
CommentCreatedEventHandler,
CommentDeletedEventHandler,

IndexingArticleEventHandler,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from "@nestjs/common";
import { EventBus } from "@nestjs/cqrs";
import { ConsumerService } from "../../../rabbitmq/consumer.service";
import { ES_ARTICLE_QUEUE } from "../../../rabbitmq/rabbitmq.constants";
import { IMessage, IProjection } from "../../core";
import { MessageType } from "../../core/enums/article.enum";
import { IndexingArticleEvent } from "../events";

@Injectable()
export class ElasticSearchArticleProjection implements IProjection {
constructor(
private readonly consumer: ConsumerService,
private readonly eventBus: EventBus
) {}

async handle() {
await this.consumer.consume(ES_ARTICLE_QUEUE, (msg: IMessage) => {
this.handleMessage(msg);
});
}

private async handleMessage({ type, payload }: IMessage) {
switch (type) {
case MessageType.INDEXING_ARTICLE:
this.eventBus.publish(new IndexingArticleEvent(payload.articles));
break;
default:
break;
}
}
}
1 change: 1 addition & 0 deletions src/article/application/projections/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./article.projection";
export * from "./elastic-search-article.projection";
2 changes: 2 additions & 0 deletions src/article/application/queries/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from "./find-feed-article.handler";
export * from "./find-one-article.handler";

export * from "./find-comments.handler";

export * from "./search-article.handler";
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { IQueryHandler, QueryHandler } from "@nestjs/cqrs";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { READ_CONNECTION } from "../../../../configs";
import { PublisherService } from "../../../../rabbitmq/publisher.service";
import { ES_ARTICLE_QUEUE } from "../../../../rabbitmq/rabbitmq.constants";
import { ArticleEntity, MessageType } from "../../../core";

export class IndexingArticleQuery {
constructor() {}
}

@QueryHandler(IndexingArticleQuery)
export class IndexingArticleQueryHandler
implements IQueryHandler<IndexingArticleQuery>
{
constructor(
@InjectRepository(ArticleEntity, READ_CONNECTION)
private readonly articleRepository: Repository<ArticleEntity>,

private readonly publisher: PublisherService
) {}

async execute(_: IndexingArticleQuery): Promise<any> {
const articles = await this.articleRepository.find({
relations: ["author", "blocks"],
});

const chunkArticles = this.chunkArray(articles, 100);

chunkArticles.forEach((articles: ArticleEntity[]) => {
this.publisher.publish(ES_ARTICLE_QUEUE, {
type: MessageType.INDEXING_ARTICLE,
payload: { articles },
});
});
}

private chunkArray<T>(array: T[], chunkSize: number): T[][] {
const chunks: T[][] = [];

for (let i = 0; i < array.length; i += chunkSize) {
const chunk = array.slice(i, i + chunkSize);
chunks.push(chunk);
}

return chunks;
}
}
32 changes: 32 additions & 0 deletions src/article/application/queries/handlers/search-article.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IQueryHandler, QueryHandler } from "@nestjs/cqrs";
import { ElasticsearchService } from "@nestjs/elasticsearch";

import { SearchArticleQuery } from "../impl";
import { IArticleSearchResult } from "../../../core";

@QueryHandler(SearchArticleQuery)
export class SearchArticleQueryHandler
implements IQueryHandler<SearchArticleQuery>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute({ query }: SearchArticleQuery): Promise<any> {
const response =
await this.elasticsearchService.search<IArticleSearchResult>({
index: "articles", // TODO: replace by constant
body: {
from: query.limit || 0,
size: query.offset || 10,
query: {
multi_match: {
query: query.search,
fields: ["title", "description", "author", "blocks"],
},
},
},
});
const hits = response.body.hits.hits;
const articleEs = hits.map((item) => item._source);
return articleEs;
}
}
4 changes: 4 additions & 0 deletions src/article/application/queries/impl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ export * from "./find-all.article.query";
export * from "./find-feed-article.query";
export * from "./find-one-article.query";

export * from "./search-article.query";

export * from "./find-comments.query";

export * from "./search-article.query";
5 changes: 5 additions & 0 deletions src/article/application/queries/impl/search-article.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SearchArticleDto } from "../../../core/dto";

export class SearchArticleQuery {
constructor(public readonly query: SearchArticleDto) {}
}
4 changes: 4 additions & 0 deletions src/article/application/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IndexingArticleQueryHandler } from "./handlers/indexing-article.handler";
import {
FindAllArticleQueryHandler,
FindCommentQueryHandler,
FindFeedArticleQueryHandler,
FindOneArticleQueryHandler,
SearchArticleQueryHandler,
} from "./handlers";

export * from "./handlers";
Expand All @@ -13,4 +15,6 @@ export const QueryHandlers = [
FindFeedArticleQueryHandler,
FindOneArticleQueryHandler,
FindCommentQueryHandler,
IndexingArticleQueryHandler,
SearchArticleQueryHandler,
];
4 changes: 4 additions & 0 deletions src/article/application/queries/query.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { UserModule } from "../../../user/user.module";
import { ArticleEntity } from "../../core";
import { CommentEntity } from "../../core/entities/comment.entity";
import { ArticleService } from "../services/article.service";
import { ElasticSearchModule } from "../../../elastic-search/elastic-search.module";
import { RabbitMqModule } from "../../../rabbitmq/rabbitmq.module";

@Module({
imports: [
Expand All @@ -19,6 +21,8 @@ import { ArticleService } from "../services/article.service";
),
UserModule,
CqrsModule,
ElasticSearchModule,
RabbitMqModule,
],
providers: [ArticleService, ...QueryHandlers],
controllers: [],
Expand Down
7 changes: 6 additions & 1 deletion src/article/article.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EventModule } from "./application/events/event.module";
import { QueryModule } from "./application/queries/query.module";
import { ArticleService } from "./application/services/article.service";
import { ArticleController } from "./presentation/article.controller";
import { ElasticSearchArticleProjection } from "./application/projections/elastic-search-article.projection";

@Module({
imports: [
Expand All @@ -27,7 +28,11 @@ import { ArticleController } from "./presentation/article.controller";
RabbitMqModule,
RedisModule,
],
providers: [ArticleService, ArticleProjection],
providers: [
ArticleService,
ArticleProjection,
ElasticSearchArticleProjection,
],
controllers: [ArticleController],
})
export class ArticleModule implements NestModule {
Expand Down
1 change: 1 addition & 0 deletions src/article/core/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { CreateArticleDto } from "./create-article.dto";
export { CreateCommentDto } from "./create-comment";
export * from "./article-query";
export * from "./block.dto";
export * from "./search-article";
12 changes: 12 additions & 0 deletions src/article/core/dto/search-article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";

export class SearchArticleDto {
@ApiProperty({ required: false })
limit?: number;

@ApiProperty({ required: false })
offset?: number;

@ApiProperty({ required: true })
search: string;
}
2 changes: 2 additions & 0 deletions src/article/core/enums/article.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export enum MessageType {
ARTICLE_UNFAVORITED = "ARTICLE_UNFAVORITED",
COMMENT_CREATED = "COMMENT_CREATED",
COMMENT_DELETED = "COMMENT_DELETED",

INDEXING_ARTICLE = "INDEXING_ARTICLE",
}
21 changes: 19 additions & 2 deletions src/article/core/interfaces/article.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BlockEntity } from "../entities/block.entity";
import { IBlock } from "./block.interface";
import { ProfileData } from "../../../profile/core/interfaces/profile.interface";
import { IUser } from "../../../user/core/interfaces/user.interface";
import { BlockEntity } from "../entities/block.entity";
import { IBlock } from "./block.interface";

// export interface Comment {
// id: number;
Expand Down Expand Up @@ -63,3 +63,20 @@ export interface IComment {
article?: IArticle;
author?: IUser | ProfileData;
}

interface IArticleElasticSearch {
id: string;
title: string;
description: string;
author: string;
blockText: string;
}

export interface IArticleSearchResult {
hits: {
total: number;
hits: Array<{
_source: ArticleData;
}>;
};
}
1 change: 1 addition & 0 deletions src/article/core/interfaces/projection.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IMessage {
type: MessageType;
payload: {
article?: ArticleEntity;
articles?: ArticleEntity[];
user?: UserEntity;
slug?: string;
userId?: number;
Expand Down
Loading