Skip to content

Commit

Permalink
feat: postgres 컨테이너에 pgvector, mecab 설치 및 mecab 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
ezcolin2 committed Feb 18, 2025
1 parent d8c2cb9 commit cb168ea
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
10 changes: 9 additions & 1 deletion compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ version: "3.8"

services:
postgres:
image: pgvector/pgvector:0.8.0-pg16
build:
context: .
dockerfile: ./services/postgres/Dockerfile
image: postgres-pgvector-mecab
env_file:
- .env.local
volumes:
Expand Down Expand Up @@ -55,6 +58,7 @@ services:
- .env.local
volumes:
- ./apps/backend:/app/apps/backend
- ./libs:/app/libs
depends_on:
postgres:
condition: service_healthy
Expand All @@ -74,6 +78,8 @@ services:
- .env.local
volumes:
- ./apps/scheduler:/app/apps/scheduler
- ./libs:/app/libs

depends_on:
postgres:
condition: service_healthy
Expand All @@ -93,6 +99,8 @@ services:
- .env.local
volumes:
- ./apps/websocket:/app/apps/websocket
- ./libs:/app/libs

depends_on:
postgres:
condition: service_healthy
Expand Down
2 changes: 1 addition & 1 deletion libs/page/src/page.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Page {
@Column({
generatedType: "STORED",
type: "tsvector",
asExpression: `COALESCE(title, '') || ' ' || COALESCE(document, '')`,
asExpression: `to_tsvector('korean', COALESCE(title, '') || ' ' || COALESCE(document, ''))`,
nullable: true,
})
fts: string;
Expand Down
12 changes: 6 additions & 6 deletions libs/page/src/page.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Page } from './page.entity';
import { InjectDataSource } from '@nestjs/typeorm';
import { Injectable, OnModuleInit } from "@nestjs/common";
import { DataSource, Repository } from "typeorm";
import { Page } from "./page.entity";
import { InjectDataSource } from "@nestjs/typeorm";

@Injectable()
export class PageRepository extends Repository<Page> implements OnModuleInit {
Expand Down Expand Up @@ -60,7 +60,7 @@ export class PageRepository extends Repository<Page> implements OnModuleInit {
create or replace function hybrid_search(
query_text text,
query_embedding vector(512),
query_embedding vector(384),
match_count int,
full_text_weight float = 1,
semantic_weight float = 1,
Expand Down Expand Up @@ -119,7 +119,7 @@ export class PageRepository extends Repository<Page> implements OnModuleInit {
AND attname = 'fts')
THEN
ALTER TABLE page
ADD COLUMN fts tsvector GENERATED ALWAYS AS (to_tsvector('english', document)) STORED;
ADD COLUMN fts tsvector GENERATED ALWAYS AS (to_tsvector('korean', COALESCE(title, '') || ' ' || COALESCE(document, ''))) STORED;
END IF;
END $$;
`);
Expand Down
52 changes: 52 additions & 0 deletions services/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 베이스 이미지를 postgres:16으로 설정
FROM postgres:16

# 시스템 패키지 업데이트 및 설치
RUN apt update -y && \
apt install -y \
wget \
build-essential \
postgresql-server-dev-16 \
automake \
unzip \
libmecab-dev

# pgvector 설치
RUN cd /tmp && \
wget https://github.com/pgvector/pgvector/archive/refs/tags/v0.8.0.tar.gz && \
tar -xvzf v0.8.0.tar.gz && \
cd pgvector-0.8.0 && \
make && \
make install

# mecab-ko 설치
RUN cd /tmp && \
wget https://bitbucket.org/eunjeon/mecab-ko/downloads/mecab-0.996-ko-0.9.2.tar.gz && \
tar xvfz mecab-0.996-ko-0.9.2.tar.gz && \
cd mecab-0.996-ko-0.9.2 && \
./configure CC=gcc CXX=g++ CFLAGS="-m64" CXXFLAGS="-m64" && \
make && \
make install

# mecab-ko-dic 설치
RUN cd /tmp && \
wget https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.1.1-20180720.tar.gz && \
tar xvfz mecab-ko-dic-2.1.1-20180720.tar.gz && \
cd mecab-ko-dic-2.1.1-20180720 && \
./autogen.sh && \
./configure && \
make && \
make install

RUN cd /tmp && \
apt install git -y && \
git clone https://github.com/i0seph/textsearch_ko.git && \
cd textsearch_ko && \
make USE_PGXS=1 && \
make USE_PGXS=1 install && \
cp /tmp/textsearch_ko/ts_mecab_ko.sql /docker-entrypoint-initdb.d/

COPY services/postgres/init.sql /docker-entrypoint-initdb.d/z_init.sql

# PostgreSQL 컨테이너 기본 명령어 설정
CMD ["postgres"]
1 change: 1 addition & 0 deletions services/postgres/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION IF NOT EXISTS vector;

0 comments on commit cb168ea

Please sign in to comment.