Skip to content

Commit

Permalink
Merge pull request #111 from Daseul1/feat/#110
Browse files Browse the repository at this point in the history
[#110]feat: 엘라스틱 최종 수정
  • Loading branch information
Daseul1 committed Apr 1, 2022
2 parents 6eae714 + dc7bdd6 commit 2cb6dad
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 70 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion ars/elk/logstash/logstash.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ input {
output {
elasticsearch {
hosts => "elasticsearch:9200"
index => "artipul00"
index => "artipul09"
}
}
7 changes: 6 additions & 1 deletion ars/src/apis/art/art.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@nestjs/elasticsearch';
import {
ElasticsearchModule,
ElasticsearchService,
} from '@nestjs/elasticsearch';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ArtImage } from '../artImage/entities/artImage.entity';
import { Engage } from '../engage/entities/engage.entity';
Expand All @@ -11,6 +14,7 @@ import { User } from '../user/entities/user.entity';
import { ArtResolver } from './art.resolver';
import { ArtService } from './art.service';
import { Art } from './entities/art.entity';
import { ArtsSearch } from './entities/artsSearch.entity';
import { LikeArt } from './entities/likeArt.entity';

@Module({
Expand All @@ -22,6 +26,7 @@ import { LikeArt } from './entities/likeArt.entity';
User,
Payment,
Engage,
ArtsSearch,
]),
ElasticsearchModule.register({
node: 'http://elasticsearch:9200',
Expand Down
219 changes: 166 additions & 53 deletions ars/src/apis/art/art.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,175 @@ export class ArtResolver {

@Query(() => [ArtsSearch])
async fetchArts(
@Args('tag1') tag1: string,
@Args('tag2', { nullable: true }) tag2: string,
@Args('tag3', { nullable: true }) tag3: string,
@Args('tag4', { nullable: true }) tag4: string,
@Args({ name: 'tags', type: () => [String] }) tags: string[],
) {
//redis에 캐시되어 있는지 확인하기
const redisValue = await this.cacheManager.get(
`tag1: ${tag1}, tag2: ${tag2}, tag3: ${tag3}, tag4: ${tag4}`,
);
if (redisValue) {
return redisValue;
if (tags.length === 1) {
const redisValue = await this.cacheManager.get(`tags: ${tags}`);
if (redisValue) {
return redisValue;
}

const result = await this.elasticsearchService.search({
index: 'artipul09',
query: {
bool: {
must: [{ match: { tag1: tags[0] } }],
},
},
});

if (!result.hits.hits.length) return null;

const artTags = result.hits.hits.map((el: any) => {
return {
id: el._source.id,
title: el._source.title,
start_price: el._source.start_price,
instant_bid: el._source.instant_bid,
price: el._source.price,
deadline: el._source.deadline,
thumbnail: el._source.thumbnail,
tag1: el._source.tag1,
nickname: el._source.nickname,
};
});

// 엘라스틱서치에서 조회 결과가 있다면, 레디스에 검색결과 캐싱해놓기
await this.cacheManager.set(`tags: ${tags}`, artTags, {
ttl: 5,
});
return artTags;
}
// 레디스에 캐시가 되어있지 않다면, 엘라스틱서치에서 조회하기(유저가 검색한 검색어로 조회하기)
const result = await this.elasticsearchService.search({
index: 'artipul00',
query: {
bool: {
should: [
{ match: { tag1: tag1 } },
{ match: { tag2: tag2 } },
{ match: { tag3: tag3 } },
{ match: { tag4: tag4 } },
],

if (tags.length === 2) {
const redisValue = await this.cacheManager.get(`tags: ${tags}`);
if (redisValue) {
return redisValue;
}

const result = await this.elasticsearchService.search({
index: 'artipul09',
query: {
bool: {
must: [{ match: { tag1: tags[0] } }, { match: { tag2: tags[1] } }],
},
},
},
});

if (!result.hits.hits.length) return null;

const artTags = result.hits.hits.map((el: any) => {
return {
id: el._source.id,
title: el._source.title,
start_price: el._source.start_price,
instant_bid: el._source.instant_bid,
price: el._source.price,
deadline: el._source.deadline,
thumbnail: el._source.thumbnail,
tag1: el._source.tag1,
tag2: el._source.tag2,
tag3: el._source.tag3,
tag4: el._source.tag4,
nickname: el._source.nickname,
};
});

// 엘라스틱서치에서 조회 결과가 있다면, 레디스에 검색결과 캐싱해놓기
await this.cacheManager.set(
`tag1: ${tag1}, tag2: ${tag2}, tag3: ${tag3}, tag4: ${tag4}`,
artTags,
{ ttl: 0 },
);
// 최종 결과 브라우저에 리턴해주기

return artTags;
}
});

if (!result.hits.hits.length) return null;

const artTags = result.hits.hits.map((el: any) => {
return {
id: el._source.id,
title: el._source.title,
start_price: el._source.start_price,
instant_bid: el._source.instant_bid,
price: el._source.price,
deadline: el._source.deadline,
thumbnail: el._source.thumbnail,
tag1: el._source.tag1,
tag2: el._source.tag2,
nickname: el._source.nickname,
};
});

// 엘라스틱서치에서 조회 결과가 있다면, 레디스에 검색결과 캐싱해놓기
await this.cacheManager.set(`tags: ${tags}`, artTags, {
ttl: 5,
});
return artTags;
}

if (tags.length === 3) {
const redisValue = await this.cacheManager.get(`tags: ${tags}`);
if (redisValue) {
return redisValue;
}

const result = await this.elasticsearchService.search({
index: 'artipul09',
query: {
bool: {
must: [
{ match: { tag1: tags[0] } },
{ match: { tag2: tags[1] } },
{ match: { tag3: tags[2] } },
],
},
},
});

if (!result.hits.hits.length) return null;

const artTags = result.hits.hits.map((el: any) => {
return {
id: el._source.id,
title: el._source.title,
start_price: el._source.start_price,
instant_bid: el._source.instant_bid,
price: el._source.price,
deadline: el._source.deadline,
thumbnail: el._source.thumbnail,
tag1: el._source.tag1,
tag2: el._source.tag2,
tag3: el._source.tag3,
nickname: el._source.nickname,
};
});

// 엘라스틱서치에서 조회 결과가 있다면, 레디스에 검색결과 캐싱해놓기
await this.cacheManager.set(`tags: ${tags}`, artTags, {
ttl: 5,
});
return artTags;
}

if (tags.length === 4) {
const redisValue = await this.cacheManager.get(`tags: ${tags}`);
if (redisValue) {
return redisValue;
}

const result = await this.elasticsearchService.search({
index: 'artipul09',
query: {
bool: {
must: [
{ match: { tag1: tags[0] } },
{ match: { tag2: tags[1] } },
{ match: { tag3: tags[2] } },
{ match: { tag4: tags[3] } },
],
},
},
});

if (!result.hits.hits.length) return null;

const artTags = result.hits.hits.map((el: any) => {
return {
id: el._source.id,
title: el._source.title,
start_price: el._source.start_price,
instant_bid: el._source.instant_bid,
price: el._source.price,
deadline: el._source.deadline,
thumbnail: el._source.thumbnail,
tag1: el._source.tag1,
tag2: el._source.tag2,
tag3: el._source.tag3,
tag4: el._source.tag4,
nickname: el._source.nickname,
};
});

// 엘라스틱서치에서 조회 결과가 있다면, 레디스에 검색결과 캐싱해놓기
await this.cacheManager.set(`tags: ${tags}`, artTags, {
ttl: 5,
});
return artTags;
}


@Query(() => Art)
async fetchArt(@Args('artId') artId: string) {
Expand Down
6 changes: 5 additions & 1 deletion ars/src/apis/art/art.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ArtService {
}

// 작품 등록
async create({ image_urls, ...rest }, currentUser) {
async create({ image_urls, tags, ...rest }, currentUser) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
Expand All @@ -143,6 +143,10 @@ export class ArtService {
...rest,
user: currentUser,
thumbnail: image_urls[0],
tag1: tags[0],
tag2: tags[1],
tag3: tags[2],
tag4: tags[3],
});

for (let i = 0; i < image_urls.length; i++) {
Expand Down
19 changes: 11 additions & 8 deletions ars/src/apis/art/dto/createArtInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ export class CreateArtInput {
@Field(() => Boolean)
is_soldout: boolean;

@Field(() => String)
tag1: string;
@Field(() => [String])
tags: string[];

// @Field(() => String)
// tag1: string;

@Field(() => String, { nullable: true })
tag2?: string;
// @Field(() => String, { nullable: true })
// tag2?: string;

@Field(() => String, { nullable: true })
tag3?: string;
// @Field(() => String, { nullable: true })
// tag3?: string;

@Field(() => String, { nullable: true })
tag4?: string;
// @Field(() => String, { nullable: true })
// tag4?: string;
}
1 change: 1 addition & 0 deletions ars/src/apis/payment/payment.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ArtService } from '../art/art.service';
import { Art } from '../art/entities/art.entity';
Expand Down
1 change: 0 additions & 1 deletion ars/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { PaymentModule } from './apis/payment/payment.module';
entities: [__dirname + '/apis/**/*.entity.*'],
synchronize: true,
logging: true,
timezone: 'Asia/seoul',
}),
CacheModule.register<RedisClientOptions>({
store: redisStore,
Expand Down
7 changes: 2 additions & 5 deletions ars/src/common/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type Comment {
}

type Query {
fetchArts(tag1: String!, tag2: String, tag3: String, tag4: String): [ArtsSearch!]!
fetchArts(tags: [String!]!): [ArtsSearch!]!
fetchArt(artId: String!): Art!
fetchArtImages(artId: String!): [ArtImage!]!
fetchEngageCount: Float!
Expand Down Expand Up @@ -206,10 +206,7 @@ input CreateArtInput {
deadline: DateTime!
image_urls: [String!]!
is_soldout: Boolean!
tag1: String!
tag2: String
tag3: String
tag4: String
tags: [String!]!
}

"""The `Upload` scalar type represents a file upload."""
Expand Down

0 comments on commit 2cb6dad

Please sign in to comment.