Skip to content

Commit

Permalink
deploy: ccff0c5
Browse files Browse the repository at this point in the history
  • Loading branch information
RifeWang committed Jul 16, 2024
1 parent ae89ece commit 8c5e879
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions redis-full-text/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html><html lang=zh-CN><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=robots content="noodp"><title>Redis 全文检索及使用示例 - 凌虚 Blog</title><meta name=Description content="Redis 全文检索及使用示例"><meta property="og:url" content="https://rifewang.github.io/redis-full-text/">
<meta property="og:site_name" content="凌虚 Blog"><meta property="og:title" content="Redis 全文检索及使用示例"><meta property="og:description" content="Redis 全文检索及使用示例"><meta property="og:locale" content="zh_CN"><meta property="og:type" content="article"><meta property="article:section" content="posts"><meta property="article:published_time" content="2024-07-15T22:11:09+08:00"><meta property="article:modified_time" content="2024-07-16T13:19:53+08:00"><meta property="article:tag" content="Redis"><meta property="og:image" content="https://rifewang.github.io/images/avatar.png"><meta name=twitter:card content="summary_large_image"><meta name=twitter:image content="https://rifewang.github.io/images/avatar.png"><meta name=twitter:title content="Redis 全文检索及使用示例"><meta name=twitter:description content="Redis 全文检索及使用示例"><meta name=application-name content="凌虚的博客"><meta name=apple-mobile-web-app-title content="凌虚的博客"><meta name=theme-color content="#ffffff"><meta name=msapplication-TileColor content="#da532c"><link rel=canonical href=https://rifewang.github.io/redis-full-text/><link rel=prev href=https://rifewang.github.io/redis-vector/><link rel=stylesheet href=/css/style.min.css><link rel=preload href=/lib/fontawesome-free/all.min.css as=style onload='this.onload=null,this.rel="stylesheet"'><noscript><link rel=stylesheet href=/lib/fontawesome-free/all.min.css></noscript><link rel=preload href=/lib/animate/animate.min.css as=style onload='this.onload=null,this.rel="stylesheet"'><noscript><link rel=stylesheet href=/lib/animate/animate.min.css></noscript><script type=application/ld+json>{"@context":"http://schema.org","@type":"BlogPosting","headline":"Redis 全文检索及使用示例","inLanguage":"zh-CN","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/rifewang.github.io\/redis-full-text\/"},"image":["https:\/\/rifewang.github.io\/images\/avatar.png"],"genre":"posts","keywords":"Redis","wordcount":2389,"url":"https:\/\/rifewang.github.io\/redis-full-text\/","datePublished":"2024-07-15T22:11:09+08:00","dateModified":"2024-07-16T13:19:53+08:00","license":"Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)","publisher":{"@type":"Organization","name":"凌虚","logo":"https:\/\/rifewang.github.io\/images\/avatar.png"},"author":{"@type":"Person","name":"凌虚"},"description":"Redis 全文检索及使用示例"}</script></head><script async src="https://www.googletagmanager.com/gtag/js?id=G-VRMQFEVL7J"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-VRMQFEVL7J")</script><body data-header-desktop=fixed data-header-mobile=auto><script type=text/javascript>(window.localStorage&&localStorage.getItem("theme")?localStorage.getItem("theme")==="dark":"light"==="auto"?window.matchMedia("(prefers-color-scheme: dark)").matches:"light"==="dark")&&document.body.setAttribute("theme","dark")</script><div id=mask></div><div class=wrapper><header class=desktop id=header-desktop><div class=header-wrapper><div class=header-title><a href=/ title="凌虚 Blog">凌虚的博客</a></div><div class=menu><div class=menu-inner><a class=menu-item href=/posts/>文章 </a><a class=menu-item href=/categories/>分类 </a><a class=menu-item href=/tags/>标签 </a><a class=menu-item href=/about>作者 </a><span class="menu-item delimiter"></span><span class="menu-item search" id=search-desktop>
<meta property="og:site_name" content="凌虚 Blog"><meta property="og:title" content="Redis 全文检索及使用示例"><meta property="og:description" content="Redis 全文检索及使用示例"><meta property="og:locale" content="zh_CN"><meta property="og:type" content="article"><meta property="article:section" content="posts"><meta property="article:published_time" content="2024-07-15T22:11:09+08:00"><meta property="article:modified_time" content="2024-07-16T13:21:48+08:00"><meta property="article:tag" content="Redis"><meta property="og:image" content="https://rifewang.github.io/images/avatar.png"><meta name=twitter:card content="summary_large_image"><meta name=twitter:image content="https://rifewang.github.io/images/avatar.png"><meta name=twitter:title content="Redis 全文检索及使用示例"><meta name=twitter:description content="Redis 全文检索及使用示例"><meta name=application-name content="凌虚的博客"><meta name=apple-mobile-web-app-title content="凌虚的博客"><meta name=theme-color content="#ffffff"><meta name=msapplication-TileColor content="#da532c"><link rel=canonical href=https://rifewang.github.io/redis-full-text/><link rel=prev href=https://rifewang.github.io/redis-vector/><link rel=stylesheet href=/css/style.min.css><link rel=preload href=/lib/fontawesome-free/all.min.css as=style onload='this.onload=null,this.rel="stylesheet"'><noscript><link rel=stylesheet href=/lib/fontawesome-free/all.min.css></noscript><link rel=preload href=/lib/animate/animate.min.css as=style onload='this.onload=null,this.rel="stylesheet"'><noscript><link rel=stylesheet href=/lib/animate/animate.min.css></noscript><script type=application/ld+json>{"@context":"http://schema.org","@type":"BlogPosting","headline":"Redis 全文检索及使用示例","inLanguage":"zh-CN","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/rifewang.github.io\/redis-full-text\/"},"image":["https:\/\/rifewang.github.io\/images\/avatar.png"],"genre":"posts","keywords":"Redis","wordcount":2309,"url":"https:\/\/rifewang.github.io\/redis-full-text\/","datePublished":"2024-07-15T22:11:09+08:00","dateModified":"2024-07-16T13:21:48+08:00","license":"Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)","publisher":{"@type":"Organization","name":"凌虚","logo":"https:\/\/rifewang.github.io\/images\/avatar.png"},"author":{"@type":"Person","name":"凌虚"},"description":"Redis 全文检索及使用示例"}</script></head><script async src="https://www.googletagmanager.com/gtag/js?id=G-VRMQFEVL7J"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-VRMQFEVL7J")</script><body data-header-desktop=fixed data-header-mobile=auto><script type=text/javascript>(window.localStorage&&localStorage.getItem("theme")?localStorage.getItem("theme")==="dark":"light"==="auto"?window.matchMedia("(prefers-color-scheme: dark)").matches:"light"==="dark")&&document.body.setAttribute("theme","dark")</script><div id=mask></div><div class=wrapper><header class=desktop id=header-desktop><div class=header-wrapper><div class=header-title><a href=/ title="凌虚 Blog">凌虚的博客</a></div><div class=menu><div class=menu-inner><a class=menu-item href=/posts/>文章 </a><a class=menu-item href=/categories/>分类 </a><a class=menu-item href=/tags/>标签 </a><a class=menu-item href=/about>作者 </a><span class="menu-item delimiter"></span><span class="menu-item search" id=search-desktop>
<input type=text placeholder="Search titles or contents..." id=search-input-desktop>
<a href=javascript:void(0); class="search-button search-toggle" id=search-toggle-desktop title=Search><i class="fas fa-search fa-fw" aria-hidden=true></i>
</a><a href=javascript:void(0); class="search-button search-clear" id=search-clear-desktop title=Clear><i class="fas fa-times-circle fa-fw" aria-hidden=true></i>
Expand All @@ -8,9 +8,9 @@
<a href=javascript:void(0); class="search-button search-toggle" id=search-toggle-mobile title=Search><i class="fas fa-search fa-fw" aria-hidden=true></i>
</a><a href=javascript:void(0); class="search-button search-clear" id=search-clear-mobile title=Clear><i class="fas fa-times-circle fa-fw" aria-hidden=true></i>
</a><span class="search-button search-loading" id=search-loading-mobile><i class="fas fa-spinner fa-fw fa-spin" aria-hidden=true></i></span></div><a href=javascript:void(0); class=search-cancel id=search-cancel-mobile>Cancel</a></div><a class=menu-item href=/posts/ title>文章</a><a class=menu-item href=/categories/ title>分类</a><a class=menu-item href=/tags/ title>标签</a><a class=menu-item href=/about title>作者</a><a href=javascript:void(0); class="menu-item theme-switch" title="Switch Theme">
<i class="fas fa-adjust fa-fw" aria-hidden=true></i></a></div></div></header><div class="search-dropdown desktop"><div id=search-dropdown-desktop></div></div><div class="search-dropdown mobile"><div id=search-dropdown-mobile></div></div><main class=main><div class=container><div class=toc id=toc-auto><h2 class=toc-title>Contents</h2><div class="toc-content always-active" id=toc-content-auto></div></div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">Redis 全文检索及使用示例</h1><div class=post-meta><div class=post-meta-line><span class=post-author><a href=/ title=Author rel=author class=author><i class="fas fa-user-circle fa-fw" aria-hidden=true></i>凌虚</a></span>&nbsp;<span class=post-category>included in <a href=/categories/middleware/><i class="far fa-folder fa-fw" aria-hidden=true></i>Middleware</a></span></div><div class=post-meta-line><i class="far fa-calendar-alt fa-fw" aria-hidden=true></i>&nbsp;<time datetime=2024-07-15>2024-07-15</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden=true></i>&nbsp;2389 words&nbsp;
<i class="fas fa-adjust fa-fw" aria-hidden=true></i></a></div></div></header><div class="search-dropdown desktop"><div id=search-dropdown-desktop></div></div><div class="search-dropdown mobile"><div id=search-dropdown-mobile></div></div><main class=main><div class=container><div class=toc id=toc-auto><h2 class=toc-title>Contents</h2><div class="toc-content always-active" id=toc-content-auto></div></div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">Redis 全文检索及使用示例</h1><div class=post-meta><div class=post-meta-line><span class=post-author><a href=/ title=Author rel=author class=author><i class="fas fa-user-circle fa-fw" aria-hidden=true></i>凌虚</a></span>&nbsp;<span class=post-category>included in <a href=/categories/middleware/><i class="far fa-folder fa-fw" aria-hidden=true></i>Middleware</a></span></div><div class=post-meta-line><i class="far fa-calendar-alt fa-fw" aria-hidden=true></i>&nbsp;<time datetime=2024-07-15>2024-07-15</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden=true></i>&nbsp;2309 words&nbsp;
<i class="far fa-clock fa-fw" aria-hidden=true></i>&nbsp;5 minutes&nbsp;</div></div><div class="details toc" id=toc-static data-kept><div class="details-summary toc-title"><span>Contents</span>
<span><i class="details-icon fas fa-angle-right" aria-hidden=true></i></span></div><div class="details-content toc-content" id=toc-content-static><nav id=TableOfContents><ul><li><a href=#序言>序言</a></li><li><a href=#redis-全文检索>Redis 全文检索</a><ul><li><a href=#全文检索>全文检索</a></li><li><a href=#使用示例>使用示例</a><ul><li><a href=#数据集>数据集</a></li><li><a href=#构建索引>构建索引</a></li><li><a href=#使用全文检索>使用全文检索</a><ul><li><a href=#检索基础>检索基础</a></li><li><a href=#多条件组合>多条件组合</a></li><li><a href=#前缀后缀模糊搜索>前缀后缀、模糊搜索</a></li><li><a href=#自定义分词>自定义分词</a></li><li><a href=#自定义打分>自定义打分</a></li><li><a href=#索引别名>索引别名</a></li></ul></li><li><a href=#go-示例代码>Go 示例代码</a></li></ul></li></ul></li><li><a href=#总结>总结</a></li></ul></nav></div></div><div class=content id=content><h2 id=序言>序言</h2><p><code>Redis</code> 除了我们所熟知的缓存功能之外,还通过 <code>RedisJSON</code><code>RediSearch</code><code>RedisTimeSeries</code><code>RedisBloom</code> 等模块支持了 JSON 数据、查询与搜索(包括全文检索、向量搜索、GEO 地理位置等)、时序数据、概率计算等等扩展功能。这些模块既可以按需导入,也被全部打包到了 <code>Redis Stack</code> 中方便我们直接使用。</p><p>本文将会简述如何使用 Redis 进行全文检索。</p><h2 id=redis-全文检索>Redis 全文检索</h2><h3 id=全文检索>全文检索</h3><p>全文检索是一种文本检索技术,其根据用户输入的词语或句子,在大量的文档数据中快速找到相关的内容。</p><p>全文检索的核心概念包括:</p><ul><li>分词:将文档(文本内容)拆分为一个个独立的词。</li><li>倒排索引:一种索引类型,将词与文档进行关联,以便后续查询。</li><li>相关度评分:对搜索结果的相关性进行评分。</li></ul><h3 id=使用示例>使用示例</h3><p>本文将会使用一个公开的电影数据集,构建一个电影搜索系统。</p><h4 id=数据集>数据集</h4><p>数据集来源于 <a href=https://github.com/jlshix/movielens-douban-dataset/blob/master/movie.json target=_blank rel="noopener noreffer">https://github.com/jlshix/movielens-douban-dataset/blob/master/movie.json</a></p><p>数据格式如下图所示:</p><p><img class=lazyload src=/svg/loading.min.svg data-src=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png data-srcset="https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png, https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png 1.5x, https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png 2x" data-sizes=auto alt=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png title=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png></p><p>为了行文方便,本文只会使用以下几个字段:</p><ul><li>_id:唯一标识</li><li>title:电影标题</li><li>directors:导演</li><li>genres:电影类型</li><li>summary:内容摘要</li><li>rating:评分</li></ul><p>我们使用 Redis 的 JSON 格式存储数,导入数据使用的是 <code>JSON.SET</code> 命令:</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre tabindex=0 class=chroma><code><span class=lnt>1
<span><i class="details-icon fas fa-angle-right" aria-hidden=true></i></span></div><div class="details-content toc-content" id=toc-content-static><nav id=TableOfContents><ul><li><a href=#序言>序言</a></li><li><a href=#redis-全文检索>Redis 全文检索</a><ul><li><a href=#全文检索>全文检索</a></li><li><a href=#使用示例>使用示例</a><ul><li><a href=#数据集>数据集</a></li><li><a href=#构建索引>构建索引</a></li><li><a href=#使用全文检索>使用全文检索</a><ul><li><a href=#检索基础>检索基础</a></li><li><a href=#多条件组合>多条件组合</a></li><li><a href=#前缀后缀模糊搜索>前缀后缀、模糊搜索</a></li><li><a href=#自定义分词>自定义分词</a></li><li><a href=#自定义打分>自定义打分</a></li><li><a href=#索引别名>索引别名</a></li></ul></li><li><a href=#go-示例代码>Go 示例代码</a></li></ul></li></ul></li><li><a href=#总结>总结</a></li></ul></nav></div></div><div class=content id=content><h2 id=序言>序言</h2><p><code>Redis</code> 除了我们所熟知的缓存功能之外,还通过 <code>RedisJSON</code><code>RediSearch</code><code>RedisTimeSeries</code><code>RedisBloom</code> 等模块支持了 JSON 数据、查询与搜索(包括全文检索、向量搜索、GEO 地理位置等)、时序数据、概率计算等等扩展功能。这些模块既可以按需导入,也被全部打包到了 <code>Redis Stack</code> 中方便我们直接使用。</p><p>本文将会简述如何使用 Redis 进行全文检索。</p><h2 id=redis-全文检索>Redis 全文检索</h2><h3 id=全文检索>全文检索</h3><p>全文检索是一种文本检索技术,其根据用户输入的词语或句子,在大量的文档数据中快速找到相关的内容。</p><p>全文检索的核心概念包括:</p><ul><li>分词:将文档(文本内容)拆分为一个个独立的词。</li><li>倒排索引:一种索引类型,将词与文档进行关联,以便后续查询。</li><li>相关度评分:对搜索结果的相关性进行评分。</li></ul><h3 id=使用示例>使用示例</h3><p>本文将会使用一个公开的电影数据集,构建一个电影搜索系统。</p><h4 id=数据集>数据集</h4><p>数据格式如下图所示:</p><p><img class=lazyload src=/svg/loading.min.svg data-src=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png data-srcset="https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png, https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png 1.5x, https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png 2x" data-sizes=auto alt=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png title=https://raw.githubusercontent.com/RifeWang/images/master/redis/movie-dataset.png></p><p>为了行文方便,本文只会使用以下几个字段:</p><ul><li>_id:唯一标识</li><li>title:电影标题</li><li>directors:导演</li><li>genres:电影类型</li><li>summary:内容摘要</li><li>rating:评分</li></ul><p>我们使用 Redis 的 JSON 格式存储数,导入数据使用的是 <code>JSON.SET</code> 命令:</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre tabindex=0 class=chroma><code><span class=lnt>1
</span></code></pre></td><td class=lntd><pre tabindex=0 class=chroma><code class=language-fallback data-lang=fallback><span class=line><span class=cl>JSON.SET movieID:1 $ &#39;{&#34;directors&#34;:&#34;马丁·里特&#34;,&#34;genres&#34;:[&#34;剧情&#34;,&#34;动作&#34;,&#34;西部&#34;],&#34;rating&#34;:8.0,&#34;title&#34;:&#34;野狼 Hombre&#34;,&#34;summary&#34;:&#34;约翰·罗塞尔自幼是老罗塞尔先生从战俘中带回来并抚养他长大的,但是他生性豪放不羁……&#34;}&#39;
</span></span></code></pre></td></tr></table></div></div><p>需要说明的是,Redis 是一个 key-value 数据库,JSON 只是 value 的格式之一,而 key 总是一个字符串,key 在本文中定义为了 movieID:12345 这种固定前缀加 ID 的格式。</p><p>使用 Go 批量导入的部分代码如下:</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre tabindex=0 class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
Expand Down
Loading

0 comments on commit 8c5e879

Please sign in to comment.