{"id":28977,"date":"2025-09-26T00:13:52","date_gmt":"2025-09-26T07:13:52","guid":{"rendered":"http:\/\/dev-en.pingcap.com\/?post_type=tutorial&amp;p=28977"},"modified":"2025-09-26T00:13:52","modified_gmt":"2025-09-26T07:13:52","slug":"tutorial-hybrid-search-combining-vector-and-full-text-search","status":"publish","type":"tutorial","link":"https:\/\/www.pingcap.com\/ko\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/","title":{"rendered":"Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search"},"content":{"rendered":"<h1 class=\"wp-block-heading\">Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Hybrid_Search_Combining_Vector_and_Full-text_Search\"><\/span>Hybrid Search: Combining Vector and Full-text Search<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Modern search applications need to understand both semantic meaning and exact keyword matches. Hybrid search combines the best of both worlds: vector search for semantic understanding and full-text search for precise keyword matching.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Hybrid_Search\"><\/span>What is Hybrid Search?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Hybrid search merges two powerful search techniques:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vector Search<\/strong>: Understands semantic meaning and context using embeddings<\/li>\n\n\n\n<li><strong>Full-text Search<\/strong>: Provides precise keyword matching and traditional relevance scoring<\/li>\n\n\n\n<li><strong>Fusion Methods<\/strong>: Intelligently combines results from both approaches<\/li>\n<\/ul>\n\n\n\n<p>This approach ensures you never miss relevant results, whether users search with natural language or specific keywords.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Hybrid Search with TiDB\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/qrISRPmsuvU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><em>TiDB Hybrid Search Demo<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before starting, ensure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3.10+<\/li>\n\n\n\n<li>TiDB database instance (<a href=\"https:\/\/tidbcloud.com\/free-trial\">Create a free TiDB Serverless Cluster<\/a>)<\/li>\n\n\n\n<li>OpenAI API key (<a href=\"https:\/\/platform.openai.com\/api-keys\">Get your API key<\/a>)<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note<\/strong>: Full-text search is currently available in specific regions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TiDB Cloud Starter: Frankfurt (eu-central-1), Singapore (ap-southeast-1)<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Run\"><\/span>How to Run<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Step 1<\/strong>: Clone the repository to local<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/pingcap\/pytidb.git\ncd pytidb\/examples\/hybrid_search\/<\/code><\/pre>\n\n\n\n<p><strong>Step 2<\/strong>: Install the required packages and set up the environment<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m venv .venv\nsource .venv\/bin\/activate\npip install -r reqs.txt<\/code><\/pre>\n\n\n\n<p><strong>Step 3<\/strong>: Set up environment to connect to TiDB<\/p>\n\n\n\n<p>Go to <a href=\"https:\/\/tidbcloud.com\/clusters\">TiDB Cloud console<\/a> and get the connection parameters, then set up the environment variable like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; .env &lt;&lt;EOF\nTIDB_HOST={gateway-region}.prod.aws.tidbcloud.com\nTIDB_PORT=4000\nTIDB_USERNAME={prefix}.root\nTIDB_PASSWORD={password}\nTIDB_DATABASE=hybrid_search_demo\nOPENAI_API_KEY=your-openai-api-key\nEOF<\/code><\/pre>\n\n\n\n<p><strong>Step 4<\/strong>: Run the Streamlit app<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>streamlit run app.py<\/code><\/pre>\n\n\n\n<p><strong>Step 5<\/strong>: Open your browser and visit <code>http:\/\/localhost:8501<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_It_Works\"><\/span>How It Works<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\ud83d\udca1 <strong>Source Code<\/strong>: You can find the complete source code for this example on <a href=\"https:\/\/github.com\/pingcap\/pytidb\/tree\/main\/examples\/hybrid_search\">GitHub<\/a>. This working example includes all the necessary files to get you started with hybrid search in minutes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Schema Definition<\/h3>\n\n\n\n<p>Define a table schema with both vector and full-text fields using <code>pytidb.schema.TableModel<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define table schema\nclass Document(TableModel):\n    __tablename__ = \"documents\"\n    __table_args__ = {\"extend_existing\": True}\n\n    id: int = Field(primary_key=True)\n    text: str = FullTextField()\n    text_vec: list&#91;float] = embed_fn.VectorField(\n        source_field=\"text\",\n    )\n    meta: dict = Field(sa_type=JSON)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Embedding Function<\/h3>\n\n\n\n<p>Configure automatic embedding generation with OpenAI embeddings &#8211; vectors are automatically generated when inserting text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Hybrid Search with Fusion<\/h3>\n\n\n\n<p>Use the search API to combine vector and full-text search with fusion methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Perform hybrid search with different search types\ndef hybrid_search(query_text, search_type=\"hybrid\", limit=5):\n    query = (\n        table.search(query_text, search_type=search_type)\n        .distance_threshold(0.8)\n        .fusion(method=\"rrf\")  # Reciprocal Rank Fusion\n        .limit(limit)\n    )\n\n    return query.to_pandas()\n\n# Example: Vector search only\nvector_results = hybrid_search(\"database performance\", search_type=\"vector\")\n\n# Example: Full-text search only\nfulltext_results = hybrid_search(\"TiDB distributed\", search_type=\"fulltext\")\n\n# Example: Hybrid search (combines both)\nhybrid_results = hybrid_search(\"TiDB performance optimization\", search_type=\"hybrid\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Results<\/h3>\n\n\n\n<p>Get ranked results with combined scores from both vector similarity and text matching, displayed in an interactive Streamlit interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Related_Resources\"><\/span>Related Resources<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Source Code<\/strong>: <a href=\"https:\/\/github.com\/pingcap\/pytidb\/tree\/main\/examples\/hybrid_search\">View on GitHub<\/a><\/li>\n\n\n\n<li><strong>TiDB Vector Documentation<\/strong>: <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/vector-search-overview\">Vector Data Types<\/a><\/li>\n\n\n\n<li><strong>Full-text Search<\/strong>: <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/full-text-search\">Full-text Search in TiDB<\/a><\/li>\n\n\n\n<li><strong>Hands-on Lab<\/strong>: <a href=\"https:\/\/labs.tidb.io\/lab?preview=demo_409\">Build Hybrid Search Apps Using Jupyter Notebook<\/a> (60 min)<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search Hybrid Search: Combining Vector and Full-text Search Modern search applications need to understand both semantic meaning and exact keyword matches. Hybrid search combines the best of both worlds: vector search for semantic understanding and full-text search for precise keyword matching. What is Hybrid Search? Hybrid search [&hellip;]<\/p>\n","protected":false},"featured_media":0,"template":"","categories":[],"tags":[],"class_list":["post-28977","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pingcap.com\/ko\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB\" \/>\n<meta property=\"og:description\" content=\"Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search Hybrid Search: Combining Vector and Full-text Search Modern search applications need to understand both semantic meaning and exact keyword matches. Hybrid search combines the best of both worlds: vector search for semantic understanding and full-text search for precise keyword matching. What is Hybrid Search? Hybrid search [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pingcap.com\/ko\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/\" \/>\n<meta property=\"og:site_name\" content=\"TiDB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/pingcap2015\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"714\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/\",\"url\":\"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/\",\"name\":\"Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/#website\"},\"datePublished\":\"2025-09-26T07:13:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pingcap.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pingcap.com\/#website\",\"url\":\"https:\/\/www.pingcap.com\/\",\"name\":\"TiDB\",\"description\":\"TiDB | SQL at Scale\",\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pingcap.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pingcap.com\/#organization\",\"name\":\"PingCAP\",\"url\":\"https:\/\/www.pingcap.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"width\":811,\"height\":232,\"caption\":\"PingCAP\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/pingcap2015\",\"https:\/\/x.com\/PingCAP\",\"https:\/\/linkedin.com\/company\/pingcap\",\"https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pingcap.com\/ko\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/","og_locale":"ko_KR","og_type":"article","og_title":"Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB","og_description":"Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search Hybrid Search: Combining Vector and Full-text Search Modern search applications need to understand both semantic meaning and exact keyword matches. Hybrid search combines the best of both worlds: vector search for semantic understanding and full-text search for precise keyword matching. What is Hybrid Search? Hybrid search [&hellip;]","og_url":"https:\/\/www.pingcap.com\/ko\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/","og_site_name":"TiDB","article_publisher":"https:\/\/facebook.com\/pingcap2015","og_image":[{"width":1440,"height":714,"url":"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@PingCAP","twitter_misc":{"Est. reading time":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/","url":"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/","name":"Tutorial: Hybrid Search - Combining Vector and Full-text Search | TiDB","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"datePublished":"2025-09-26T07:13:52+00:00","breadcrumb":{"@id":"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pingcap.com\/tutorial\/tutorial-hybrid-search-combining-vector-and-full-text-search\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pingcap.com\/"},{"@type":"ListItem","position":2,"name":"Tutorial: Hybrid Search &#8211; Combining Vector and Full-text Search"}]},{"@type":"WebSite","@id":"https:\/\/www.pingcap.com\/#website","url":"https:\/\/www.pingcap.com\/","name":"\ud2f0DB","description":"TiDB | SQL at Scale","publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pingcap.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/www.pingcap.com\/#organization","name":"PingCAP","url":"https:\/\/www.pingcap.com\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/","url":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","contentUrl":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","width":811,"height":232,"caption":"PingCAP"},"image":{"@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/pingcap2015","https:\/\/x.com\/PingCAP","https:\/\/linkedin.com\/company\/pingcap","https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA"]}]}},"_links":{"self":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/tutorial\/28977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/types\/tutorial"}],"wp:attachment":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media?parent=28977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/categories?post=28977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/tags?post=28977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}