본문 바로가기
코딩 · 개발

FastAPI 검색 시스템 구축

FastAPI와 Elasticsearch 기반 키워드·벡터 검색 및 DB 동기화 백엔드 구축

추천 툴
ChatGPTAI 분류
쓰인 기법
역할 부여 · 상세 조건 제시 · 변수 활용

프롬프트 원문

ChatGPT로 실행
Act as a proficient software developer. You are tasked with building a comprehensive Elasticsearch search project using FastAPI. Your project should:

- Support various search methods: keyword, semantic, and vector search.
- Implement data splitting and importing functionalities for efficient data management.
- Include mechanisms to synchronize data from PostgreSQL to Elasticsearch.
- Design the system to be extensible, allowing for future integration with Kafka.

Responsibilities:
- Use FastAPI to create a robust and efficient API for search functionalities.
- Ensure Elasticsearch is optimized for various search queries (keyword, semantic, vector).
- Develop a data pipeline that handles data splitting and imports seamlessly.
- Implement synchronization features that keep Elasticsearch in sync with PostgreSQL databases.
- Plan and document potential integration points for Kafka to transport data.

Rules:
- Adhere to best practices in API development and Elasticsearch usage.
- Maintain code quality and documentation for future scalability.
- Consider performance impacts and optimize accordingly.

Use variables such as:
- ${searchMethod:keyword} to specify the type of search.
- ${databaseType:PostgreSQL} for database selection.
- ${integration:kafka} to indicate future integration plans.
한국어 풀이 보기(참고용)
숙련된 소프트웨어 개발자로 활동하세요. FastAPI를 사용하여 포괄적인 Elasticsearch 검색 프로젝트를 구축하는 업무를 맡았습니다. 프로젝트 요구사항은 다음과 같습니다:

- 다양한 검색 방식 지원: 키워드, 시맨틱, 벡터 검색.
- 효율적인 데이터 관리를 위한 데이터 분할 및 임포트 기능 구현.
- PostgreSQL에서 Elasticsearch로의 데이터 동기화 메커니즘 포함.
- 향후 Kafka와의 통합이 가능하도록 확장성 있는 시스템 설계.

담당 업무:
- FastAPI를 사용하여 검색 기능을 위한 견고하고 효율적인 API 생성.
- 다양한 검색 쿼리(키워드, 시맨틱, 벡터)에 맞게 Elasticsearch 최적화.
- 데이터 분할 및 임포트를 매끄럽게 처리하는 데이터 파이프라인 개발.
- PostgreSQL 데이터베이스와 Elasticsearch의 동기화를 유지하는 기능 구현.
- 데이터 전송을 위한 Kafka 통합 지점 계획 및 문서화.

규칙:
- API 개발 및 Elasticsearch 사용 시 모범 사례 준수.
- 향후 확장성을 위해 코드 품질과 문서화 유지.
- 성능 영향을 고려하고 이에 맞게 최적화.

다음과 같은 변수를 사용하세요:
- ${searchMethod:keyword} (검색 유형 지정)
- ${databaseType:PostgreSQL} (데이터베이스 선택)
- ${integration:kafka} (향후 통합 계획 표시)
  • 활용필요한 DB 종류나 검색 방식을 하단의 변수 파트에 맞춰 변경하여 사용할 수 있습니다.
  • 활용FastAPI 코드뿐만 아니라 Elasticsearch 인덱스 매핑 설정도 함께 요청해보세요.

출처 · prompts.chat · Creating a Comprehensive Elasticsearch Search Project with FastAPI — 원문은 출처에 게시된 그대로이며, 권리는 원작자에게 있습니다.

텍스트 결과

Gemini로 실행한 결과입니다. 입력 항목에는 국내 환경의 예시값을 적용했습니다(AI 생성 — 사실 확인 필요).

실행 입력값 보기
Act as a proficient software developer. You are tasked with building a comprehensive Elasticsearch search project using FastAPI. Your project should:

- Support various search methods: keyword, semantic, and vector search.
- Implement data splitting and importing functionalities for efficient data management.
- Include mechanisms to synchronize data from PostgreSQL to Elasticsearch.
- Design the system to be extensible, allowing for future integration with Kafka.

Responsibilities:
- Use FastAPI to create a robust and efficient API for search functionalities.
- Ensure Elasticsearch is optimized for various search queries (keyword, semantic, vector).
- Develop a data pipeline that handles data splitting and imports seamlessly.
- Implement synchronization features that keep Elasticsearch in sync with PostgreSQL databases.
- Plan and document potential integration points for Kafka to transport data.

Rules:
- Adhere to best practices in API development and Elasticsearch usage.
- Maintain code quality and documentation for future scalability.
- Consider performance impacts and optimize accordingly.

Configurations:
- Search Method: hybrid (keyword + dense_vector)
- Database Type: PostgreSQL 15
- Integration: Apache Kafka (CDC Debezium)

요청하신 FastAPI + Elasticsearch 기반 하이브리드 검색 시스템 프로젝트 아키텍처 및 구현 가이드입니다.

1. FastAPI 검색 엔드포인트 설계

  • 엔드포인트: POST /api/v1/search
  • 검색 로직:
  • Keyword: multi_match 쿼리로 Nori 형태소 분석기 활용.
  • Vector: dense_vector 필드 기반 kNN 검색.
  • Hybrid: RRF(Reciprocal Rank Fusion) 알고리즘을 적용해 키워드와 벡터 점수를 재정렬.

2. 데이터 분할 및 파이프라인 (Data Pipeline)

  • 텍스트 분할: RecursiveCharacterTextSplitter를 사용해 긴 문서를 500자 단위(Overlap 50자)로 청크화.
  • 임베딩 및 인덱싱: Sentence-Transformers 기반 벡터 생성 후 elasticsearch.helpers.bulk로 대용량 비동기 배치 배치.

3. PostgreSQL ↔ Elasticsearch 동기화

  • 단기 (Batch): Celery 스케줄러를 활용해 updated_at 기준 주기적 차분 동기화.
  • 실시간 (CDC): Debezium을 사용해 PostgreSQL WAL(Write-Ahead Log) 이벤트를 감지하고 ES 인덱스를 실시간 갱신.

4. Kafka 확장성 설계 (Future Roadmap)

  • Event Producer: PG 변경 사항(CUD)을 Kafka Topic(db-changes)으로 발행.
  • Consumer Pipeline: Kafka Consumer 파이프라인 구축으로 FastAPI 서비스 장애와 무관하게 데이터 갱신 보장.

hotpick 개선안

원문을 더 잘 동작하게 고쳐 쓴 프롬프트

원문을 그대로 넣으면 AI 가 매번 다르게 답하거나 중요한 부분을 빠뜨릴 수 있습니다. 개선안은 원문의 뜻과 언어는 그대로 두고 역할 · 바꿔 넣을 입력 칸 · 결과 형식 · 지킬 조건을 보완해 다시 쓴 버전입니다. 원문과 같은 곳(ChatGPT · Claude 등)에 그대로 붙여 넣어 쓰면 됩니다.

AI 자체 평가 — 개선안을 쓴 AI 가 명확성 · 구체성 · 결과 형식 · 재사용성 기준(100점)으로 직접 매긴 점수 · 결과를 돌려 검증한 값은 아님

원문74
개선안93

원문의 문제 → 개선안에서 고친 점

  • 입력 칸레슨 05 배우기 ›

    원문 · 없음바꿔 넣을 자리가 표시돼 있지 않아 쓸 때마다 원문을 직접 고쳐야 했습니다.

    개선 · 6개[DATABASE_TYPE: PostgreSQL] · [SEARCH_METHODS: keyword, semantic, vector] · [MESSAGE_BROKER: Kafka] 외 3개

  • 작업 순서레슨 07 배우기 ›

    원문 · 없음작업 순서가 정해져 있지 않아 AI 가 순서를 스스로 정해야 했습니다.

    개선 · 4단계

고친 이유 (개선안을 만든 AI 의 설명)

  1. 1구체적인 출력 형식(코드, 스키마, 구조)을 명시함
  2. 2단계별 작업 순서와 체계적인 요구사항을 정의함
  3. 3변수 표기를 표준 대괄호 형식으로 통일하여 재사용성 향상

원문

Act as a proficient software developer. You are tasked with building a comprehensive Elasticsearch search project using FastAPI. Your project should:

- Support various search methods: keyword, semantic, and vector search.
- Implement data splitting and importing functionalities for efficient data management.
- Include mechanisms to synchronize data from PostgreSQL to Elasticsearch.
- Design the system to be extensible, allowing for future integration with Kafka.

Responsibilities:
- Use FastAPI to create a robust and efficient API for search functionalities.
- Ensure Elasticsearch is optimized for various search queries (keyword, semantic, vector).
- Develop a data pipeline that handles data splitting and imports seamlessly.
- Implement synchronization features that keep Elasticsearch in sync with PostgreSQL databases.
- Plan and document potential integration points for Kafka to transport data.

Rules:
- Adhere to best practices in API development and Elasticsearch usage.
- Maintain code quality and documentation for future scalability.
- Consider performance impacts and optimize accordingly.

Use variables such as:
- ${searchMethod:keyword} to specify the type of search.
- ${databaseType:PostgreSQL} for database selection.
- ${integration:kafka} to indicate future integration plans.

개선안 · 색칠한 [칸]에 내 내용을 넣어 쓰세요

Act as a Senior Search & Backend Engineer. You are tasked with designing and implementing a production-ready search service using FastAPI and Elasticsearch.

Configurations:
- Primary Database: [DATABASE_TYPE: PostgreSQL]
- Target Search Methods: [SEARCH_METHODS: keyword, semantic, vector]
- Future Integration Target: [MESSAGE_BROKER: Kafka]

Task Workflow & Deliverables:
1. Index Schema & Data Pipeline: Define the Elasticsearch index mapping optimized for [SEARCH_METHODS]. Provide logic for data chunking/splitting and efficient bulk importing from [DATABASE_TYPE].
2. Real-time/Scheduled Sync: Implement a robust synchronization mechanism to reflect changes from [DATABASE_TYPE] into Elasticsearch.
3. FastAPI Search Endpoints: Write modular FastAPI API routes supporting query parsing, pagination, filtering, and combined search strategies.
4. Event-Driven Scalability: Document explicit integration points and interface abstractions for streaming data via [MESSAGE_BROKER].

Output Requirements:
- Project directory layout.
- Production-grade Python code snippets (FastAPI, Elasticsearch-py) with type hints and error handling.
- Elasticsearch index mapping definition (JSON).
- Short self-review checklist covering performance and failure scenarios.

같은 분야 프롬프트 코딩 · 개발

더 보기 ›