#!/usr/bin/env bash # rag/search.sh "" — Five RAG 코퍼스 키워드 검색. # # bluge/임베딩 검색계층이 없을 때 쓰는 의존성 없는 검색기. 각 문서를 # frontmatter `keywords:`(가중치 3) + 본문 매치 수로 점수화해 랭킹하고, # 매칭 섹션 헤더를 함께 보여준다. 결과를 컨텍스트에 그대로 붙여 쓰면 됨. # # ./search.sh session token csprng # ./search.sh xss sanitize set -u RAGDIR="$(cd "$(dirname "$0")" && pwd)" if [ $# -eq 0 ]; then echo "usage: ./search.sh " exit 1 fi tmp="$(mktemp)" for f in "$RAGDIR"/0*.md; do score=0 kwline="$(awk '/^keywords:/{print; exit}' "$f")" for term in "$@"; do kw=$(printf '%s' "$kwline" | grep -io "$term" | grep -c .) bd=$(grep -io "$term" "$f" | grep -c .) score=$(( score + kw*3 + bd )) done [ "$score" -gt 0 ] && printf '%d\t%s\n' "$score" "$f" >> "$tmp" done if [ ! -s "$tmp" ]; then echo "(매치 없음)" rm -f "$tmp" exit 0 fi # 검색어를 OR 정규식으로(섹션 헤더 매칭 표시용) pat="$(printf '%s|' "$@" | sed 's/|$//')" sort -t"$(printf '\t')" -k1 -rn "$tmp" | while IFS="$(printf '\t')" read -r s f; do title="$(awk -F': ' '/^title:/{print $2; exit}' "$f")" echo "■ [$s] $(basename "$f") — $title" # 매칭되는 ## 섹션 헤더 상위 3개 grep -nE "^#{1,6} " "$f" | grep -iE "$pat" | head -3 | sed 's/^/ §/' echo done rm -f "$tmp"