<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ko">
  <title>Rion Hwang · AI Dev</title>
  <subtitle>한국어 검색, RAG, LLM 에이전트를 직접 만들며 배운 것을 기록합니다.</subtitle>
  <link href="https://ai-dev.selfdeveloper01.com/feed.xml" rel="self"/>
  <link href="https://ai-dev.selfdeveloper01.com/"/>
  <id>https://ai-dev.selfdeveloper01.com/</id>
  <updated>2026-12-04T09:00:00+09:00</updated>
  <author><name>Rion Hwang</name></author>
  <entry>
    <title>PostgreSQL에서 한국어 하이브리드 검색 제대로 하기 — 형태소 BM25 + pgvector</title>
    <link href="https://ai-dev.selfdeveloper01.com/posts/korean-hybrid-search-postgresql/"/>
    <id>https://ai-dev.selfdeveloper01.com/posts/korean-hybrid-search-postgresql/</id>
    <published>2026-12-04T09:00:00+09:00</published>
    <updated>2026-12-04T09:00:00+09:00</updated>
    <summary>PostgreSQL 전문 검색은 &#x27;대한민국에서&#x27;와 &#x27;대한민국의&#x27;를 다른 단어로 봅니다. 형태소 분석과 SQL 안의 BM25로 이 문제를 풀고, KorQuAD로 측정한 결과를 정리했습니다.</summary>
    <content type="html">&lt;p&gt;RAG를 PostgreSQL + pgvector로 만들다 보면 결국 하이브리드 검색이 필요해집니다. 벡터 검색은 의미가 비슷한 문서를 잘 찾지만, 상품 코드나 고유명사, 조항 번호처럼 &lt;strong&gt;글자가 정확히 맞아야 하는 질의&lt;/strong&gt;에는 약하기 때문입니다. 그래서 키워드 검색을 함께 돌리고 두 결과를 합칩니다.&lt;/p&gt;
&lt;p&gt;문제는 한국어입니다. 이 글에서는 PostgreSQL에서 한국어 키워드 검색이 왜 잘 안 되는지, 그리고 확장 설치 없이 이를 해결하는 방법을 정리합니다. 구현은 &lt;a href=&quot;https://github.com/RionHwang0/kohybrid&quot;&gt;kohybrid&lt;/a&gt;라는 라이브러리로 공개했습니다.&lt;/p&gt;
&lt;div class=&quot;toc&quot;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#postgresql&quot;&gt;PostgreSQL 전문 검색은 한국어 조사를 모른다&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#_1&quot;&gt;흔한 우회 방법과 그 비용&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#postgresql_1&quot;&gt;접근: 형태소 분석은 앱에서, 검색은 PostgreSQL에서&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ts_rank-idf&quot;&gt;ts_rank에는 IDF가 없다&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#sql&quot;&gt;하이브리드는 SQL 한 번으로&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#korquad-10&quot;&gt;측정: KorQuAD 1.0&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#_2&quot;&gt;사용법&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#_3&quot;&gt;남은 일&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;postgresql&quot;&gt;PostgreSQL 전문 검색은 한국어 조사를 모른다&lt;a class=&quot;headerlink&quot; href=&quot;#postgresql&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;PostgreSQL 전문 검색은 텍스트를 &lt;code&gt;tsvector&lt;/code&gt;로 바꿔 색인합니다. 영어는 &lt;code&gt;english&lt;/code&gt; 설정이 어간을 추출해 &lt;code&gt;running&lt;/code&gt;과 &lt;code&gt;runs&lt;/code&gt;를 &lt;code&gt;run&lt;/code&gt;으로 묶어 주지만, 한국어 설정은 없습니다. 남는 선택지는 띄어쓰기 단위로 자르는 &lt;code&gt;simple&lt;/code&gt;뿐입니다.&lt;/p&gt;
&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_tsvector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;simple&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;한라산은 대한민국에서 가장 높은 산이다&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- &amp;#39;가장&amp;#39;:3 &amp;#39;높은&amp;#39;:4 &amp;#39;대한민국에서&amp;#39;:2 &amp;#39;산이다&amp;#39;:5 &amp;#39;한라산은&amp;#39;:1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;한국어는 조사와 어미가 단어에 붙기 때문에, &lt;code&gt;대한민국에서&lt;/code&gt;·&lt;code&gt;대한민국의&lt;/code&gt;·&lt;code&gt;대한민국은&lt;/code&gt;이 모두 다른 토큰이 됩니다. &quot;대한민국의 가장 높은 산은?&quot;으로 검색하면 위 문장과 겹치는 토큰이 &lt;code&gt;가장&lt;/code&gt;, &lt;code&gt;높은&lt;/code&gt; 두 개뿐이고, &lt;code&gt;산은&lt;/code&gt;과 &lt;code&gt;산이다&lt;/code&gt;는 아예 다른 단어로 취급됩니다.&lt;/p&gt;
&lt;h2 id=&quot;_1&quot;&gt;흔한 우회 방법과 그 비용&lt;a class=&quot;headerlink&quot; href=&quot;#_1&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;바이그램(pg_bigm)&lt;/strong&gt;: 텍스트를 두 글자씩 잘라 색인합니다. 조사가 붙어도 &lt;code&gt;대한&lt;/code&gt;, &lt;code&gt;한민&lt;/code&gt;, &lt;code&gt;민국&lt;/code&gt;이 남으니 잘 찾습니다. 다만 확장을 설치해야 해서 관리형 DB에서는 지원 여부를 확인해야 하고, 색인이 커지고, &lt;code&gt;민국에&lt;/code&gt; 같은 의미 없는 조각도 함께 매칭됩니다.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Elasticsearch/OpenSearch + Nori&lt;/strong&gt;: 품질은 좋지만 검색 시스템이 하나 더 생깁니다. 벡터는 Postgres에, 키워드는 ES에 두고 동기화하는 운영 비용이 따라옵니다.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DB 안에 한국어 파서 설치&lt;/strong&gt;: &lt;code&gt;textsearch_ko&lt;/code&gt; 같은 C 확장은 RDS, Cloud SQL, Azure 같은 관리형 DB에 설치할 수 없습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;postgresql_1&quot;&gt;접근: 형태소 분석은 앱에서, 검색은 PostgreSQL에서&lt;a class=&quot;headerlink&quot; href=&quot;#postgresql_1&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;파서를 DB에 넣을 수 없다면, 분석을 DB 밖에서 하면 됩니다. kohybrid는 문서를 저장하기 전에 &lt;a href=&quot;https://github.com/bab2min/kiwipiepy&quot;&gt;Kiwi&lt;/a&gt; 형태소 분석기로 내용어만 남기고, 결과를 평범한 &lt;code&gt;tsvector&lt;/code&gt; 컬럼에 넣습니다.&lt;/p&gt;
&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;한라산은 대한민국에서 가장 높은 산이다   →  한라산 대한민국 높다 산
대한민국의 가장 높은 산은?              →  대한민국 높다 산
PostgreSQL 16에서 벡터를 검색했습니다   →  postgresql 16 벡터 검색
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;규칙은 단순합니다. 명사, 숫자, 외국어, 어근은 그대로 두고, 동사와 형용사는 사전형(&lt;code&gt;높은&lt;/code&gt; → &lt;code&gt;높다&lt;/code&gt;)으로 바꾸고, 조사, 어미, 문장부호는 버립니다. 이제 질의와 문서가 같은 형태소로 만납니다.&lt;/p&gt;
&lt;p&gt;구현에서 한 가지 함정이 있었습니다. 분석 결과를 &lt;code&gt;to_tsvector(&#x27;simple&#x27;, &#x27;...&#x27;)&lt;/code&gt;로 넣으면 PostgreSQL 파서가 문장부호 기준으로 &lt;strong&gt;다시 자릅니다&lt;/strong&gt;. &lt;code&gt;C++&lt;/code&gt;의 &lt;code&gt;+&lt;/code&gt;가 사라지는 식입니다. 그래서 kohybrid는 형태소를 &lt;code&gt;&#x27;한라산&#x27;:1 &#x27;대한민국&#x27;:2 ...&lt;/code&gt; 형태의 &lt;code&gt;tsvector&lt;/code&gt; 리터럴로 직접 만들어 넣습니다. 파서를 거치지 않으니 분석 결과가 그대로 보존됩니다.&lt;/p&gt;
&lt;h2 id=&quot;ts_rank-idf&quot;&gt;ts_rank에는 IDF가 없다&lt;a class=&quot;headerlink&quot; href=&quot;#ts_rank-idf&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;형태소로 매칭을 고쳐도 순위 문제가 남습니다. PostgreSQL의 &lt;code&gt;ts_rank&lt;/code&gt;는 단어가 문서에 몇 번 나오는지만 보고, &lt;strong&gt;그 단어가 전체 문서에서 얼마나 흔한지는 보지 않습니다.&lt;/strong&gt; 모든 문서에 나오는 &lt;code&gt;하다&lt;/code&gt;와 두 문서에만 나오는 고유명사가 같은 무게를 받습니다.&lt;/p&gt;
&lt;p&gt;검색 엔진이 쓰는 BM25는 이 문제를 IDF(역문서빈도)로 풉니다. BM25를 계산하려면 단어별 문서 빈도, 문서 길이, 평균 문서 길이가 필요합니다. kohybrid는 이 통계를 문서 테이블 옆의 보조 테이블 세 개에 두고, &lt;strong&gt;문장 단위 트리거(statement-level trigger)와 전이 테이블(transition table)&lt;/strong&gt;로 INSERT, UPDATE, DELETE 때마다 갱신합니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;table&amp;gt;_bm25_terms&lt;/code&gt;: 단어별 문서 빈도&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;table&amp;gt;_bm25_stats&lt;/code&gt;: 문서 수와 전체 길이&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;table&amp;gt;_bm25_postings&lt;/code&gt;: 단어 → (문서, 단어 빈도, 문서 길이) 역색인&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;트리거로 관리하니 라이브러리를 거치지 않고 SQL로 직접 지운 행도 통계에 반영됩니다. 처음에는 역색인 없이 질의 시점에 후보 문서의 &lt;code&gt;tsvector&lt;/code&gt;를 풀어 단어 빈도를 셌는데, 문서가 늘수록 느려져서 역색인 테이블을 따로 두는 방식으로 바꿨습니다. 같은 데이터에서 질의당 18.6ms가 9.5ms로 줄었습니다.&lt;/p&gt;
&lt;h2 id=&quot;sql&quot;&gt;하이브리드는 SQL 한 번으로&lt;a class=&quot;headerlink&quot; href=&quot;#sql&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;키워드 후보와 벡터 후보는 Reciprocal Rank Fusion(RRF)으로 합칩니다. 점수 척도가 다른 두 검색 결과를 순위만으로 합치는 방법이라, 가중치를 따로 튜닝하지 않아도 안정적입니다. 두 후보 목록 추출과 합치기를 CTE로 묶어 쿼리 한 번에 처리합니다.&lt;/p&gt;
&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;WITH&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- pgvector: 코사인 거리 순 상위 N개&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OVER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;-- 형태소 BM25 상위 N개&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;OUTER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;결과마다 &lt;code&gt;vector_rank&lt;/code&gt;와 &lt;code&gt;keyword_rank&lt;/code&gt;를 함께 돌려주니, 어떤 검색이 그 문서를 찾았는지 바로 확인할 수 있습니다.&lt;/p&gt;
&lt;h2 id=&quot;korquad-10&quot;&gt;측정: KorQuAD 1.0&lt;a class=&quot;headerlink&quot; href=&quot;#korquad-10&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://korquad.github.io/&quot;&gt;KorQuAD 1.0&lt;/a&gt; dev 세트로 키워드 검색 품질을 쟀습니다. 질문 5,774개마다 그 질문이 만들어진 문단을 964개 문단 중에서 찾는 과제입니다. 모든 방식을 같은 PostgreSQL 16에서 돌렸고, 질의어 중 하나라도 맞으면 후보로 삼았습니다.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;방식&lt;/th&gt;
&lt;th&gt;R@1&lt;/th&gt;
&lt;th&gt;R@5&lt;/th&gt;
&lt;th&gt;R@10&lt;/th&gt;
&lt;th&gt;MRR@10&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL &lt;code&gt;simple&lt;/code&gt; + ts_rank&lt;/td&gt;
&lt;td&gt;0.720&lt;/td&gt;
&lt;td&gt;0.848&lt;/td&gt;
&lt;td&gt;0.877&lt;/td&gt;
&lt;td&gt;0.775&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;글자 바이그램 + ts_rank&lt;/td&gt;
&lt;td&gt;0.887&lt;/td&gt;
&lt;td&gt;0.979&lt;/td&gt;
&lt;td&gt;0.989&lt;/td&gt;
&lt;td&gt;0.927&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;형태소 + ts_rank&lt;/td&gt;
&lt;td&gt;0.882&lt;/td&gt;
&lt;td&gt;0.970&lt;/td&gt;
&lt;td&gt;0.981&lt;/td&gt;
&lt;td&gt;0.921&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;형태소 + BM25 (kohybrid 기본값)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.896&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.980&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.988&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.934&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;기본 설정 대비 정답 문단을 1위로 찾는 비율이 72.0%에서 89.6%로 올랐습니다.&lt;/p&gt;
&lt;p&gt;솔직하게 덧붙일 점도 있습니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;같은 &lt;code&gt;ts_rank&lt;/code&gt; 조건에서는 &lt;strong&gt;바이그램이 형태소보다 약간 낫습니다&lt;/strong&gt;(0.887 대 0.882). KorQuAD 질문은 문단의 표현을 거의 그대로 가져다 쓴 경우가 많아, 글자 조각 매칭에 유리한 데이터입니다. 대신 바이그램 색인은 형태소의 약 두 배 크기였고, 순수 SQL 기준 질의 시간도 약 4배였습니다.&lt;/li&gt;
&lt;li&gt;BM25는 정확도가 가장 높지만 공짜는 아닙니다. 같은 데이터에서 &lt;code&gt;HybridStore.search&lt;/code&gt; 기준(형태소 분석 포함) 질의당 9.9ms로, ts_rank의 2.9ms보다 느리고 역색인이 문서 테이블의 약 1.8배 공간을 씁니다. 지연이 중요하면 &lt;code&gt;ranking=&quot;ts_rank&quot;&lt;/code&gt;로 바꿀 수 있습니다.&lt;/li&gt;
&lt;li&gt;벡터 검색과 하이브리드 결과는 임베딩 모델에 따라 달라서 이번 표에는 넣지 않았습니다. 질문을 바꿔 쓴(paraphrase) 한국어 데이터셋으로 하이브리드까지 측정하는 것이 다음 과제입니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;재현 스크립트는 저장소의 &lt;a href=&quot;https://github.com/RionHwang0/kohybrid/blob/main/benchmarks/korquad_keyword.py&quot;&gt;&lt;code&gt;benchmarks/korquad_keyword.py&lt;/code&gt;&lt;/a&gt;에 있습니다.&lt;/p&gt;
&lt;h2 id=&quot;_2&quot;&gt;사용법&lt;a class=&quot;headerlink&quot; href=&quot;#_2&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;pip&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;install&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;kohybrid[binary]&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;psycopg&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;kohybrid&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HybridStore&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# OpenAI, Azure OpenAI, BGE-M3 등 어떤 임베딩이든&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psycopg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;postgresql://user:pass@localhost/db&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HybridStore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;docs&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dimensions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 테이블 + GIN/HNSW 인덱스 + BM25 트리거&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;대한민국의 수도는 서울이다.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;한라산은 대한민국에서 가장 높은 산이다.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hit&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;대한민국에서 제일 높은 산&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keyword_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;LangChain을 쓰고 있다면 리트리버로 바로 연결할 수 있습니다.&lt;/p&gt;
&lt;div class=&quot;hl&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;kohybrid.langchain&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KohybridRetriever&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pip install &amp;quot;kohybrid[binary,langchain]&amp;quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;retriever&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KohybridRetriever&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;docs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retriever&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;대한민국에서 제일 높은 산&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;필요한 것은 pgvector 확장뿐이라 RDS, Aurora, Cloud SQL, Azure Database for PostgreSQL, Supabase, Neon에서 그대로 동작합니다. 키워드 검색만 쓸 때는 pgvector도 필요 없습니다.&lt;/p&gt;
&lt;h2 id=&quot;_3&quot;&gt;남은 일&lt;a class=&quot;headerlink&quot; href=&quot;#_3&quot; title=&quot;이 제목으로 연결&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;질문을 바꿔 쓴 한국어 질의셋과 임베딩을 포함한 하이브리드 벤치마크&lt;/li&gt;
&lt;li&gt;비동기 API, LlamaIndex 연동&lt;/li&gt;
&lt;li&gt;수백만 건 규모에서 BM25 지연 최적화&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;kohybrid는 MIT 라이선스입니다. 써 보시고 버그나 제안이 있으면 &lt;a href=&quot;https://github.com/RionHwang0/kohybrid/issues&quot;&gt;이슈&lt;/a&gt;로 남겨 주세요. 도움이 됐다면 &lt;a href=&quot;https://github.com/RionHwang0/kohybrid&quot;&gt;GitHub 스타&lt;/a&gt;도 큰 힘이 됩니다.&lt;/p&gt;</content>
  </entry>
</feed>
