1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00
Files
node-redis/documentation/search/index.html
documentation-bot 6e93f4fb8c Updates
2022-12-26 10:23:37 +00:00

133 lines
21 KiB
HTML

<!DOCTYPE html><html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@redis/search</title><meta name="description" content="Documentation for @redis/search"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script async src="assets/search.js" id="search-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os"</script><header class="tsd-page-toolbar">
<div class="tsd-toolbar-contents container">
<div class="table-cell" id="tsd-search" data-base=".">
<div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M15.7824 13.833L12.6666 10.7177C12.5259 10.5771 12.3353 10.499 12.1353 10.499H11.6259C12.4884 9.39596 13.001 8.00859 13.001 6.49937C13.001 2.90909 10.0914 0 6.50048 0C2.90959 0 0 2.90909 0 6.49937C0 10.0896 2.90959 12.9987 6.50048 12.9987C8.00996 12.9987 9.39756 12.4863 10.5008 11.6239V12.1332C10.5008 12.3332 10.5789 12.5238 10.7195 12.6644L13.8354 15.7797C14.1292 16.0734 14.6042 16.0734 14.8948 15.7797L15.7793 14.8954C16.0731 14.6017 16.0731 14.1267 15.7824 13.833ZM6.50048 10.499C4.29094 10.499 2.50018 8.71165 2.50018 6.49937C2.50018 4.29021 4.28781 2.49976 6.50048 2.49976C8.71001 2.49976 10.5008 4.28708 10.5008 6.49937C10.5008 8.70852 8.71314 10.499 6.50048 10.499Z" fill="var(--color-text)"></path></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div>
<div class="field">
<div id="tsd-toolbar-links"></div></div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li></ul><a href="" class="title">@redis/search</a></div>
<div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="3" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="7" width="14" height="2" fill="var(--color-text)"></rect><rect x="1" y="11" width="14" height="2" fill="var(--color-text)"></rect></svg></a></div></div></header>
<div class="container container-main">
<div class="col-8 col-content">
<div class="tsd-page-title">
<h2>@redis/search</h2></div>
<div class="tsd-panel tsd-typography">
<a href="#redissearch" id="redissearch" style="color: inherit; text-decoration: none;">
<h1>@redis/search</h1>
</a>
<p>This package provides support for the <a href="https://redisearch.io">RediSearch</a> module, which adds indexing and querying support for data stored in Redis Hashes or as JSON documents with the RedisJSON module. It extends the <a href="https://github.com/redis/node-redis">Node Redis client</a> to include functions for each of the RediSearch commands.</p>
<p>To use these extra commands, your Redis server must have the RediSearch module installed. To index and query JSON documents, you&#39;ll also need to add the RedisJSON module.</p>
<a href="#usage" id="usage" style="color: inherit; text-decoration: none;">
<h2>Usage</h2>
</a>
<p>For complete examples, see <a href="https://github.com/redis/node-redis/blob/master/examples/search-hashes.js"><code>search-hashes.js</code></a> and <a href="https://github.com/redis/node-redis/blob/master/examples/search-json.js"><code>search-json.js</code></a> in the Node Redis examples folder.</p>
<a href="#indexing-and-querying-data-in-redis-hashes" id="indexing-and-querying-data-in-redis-hashes" style="color: inherit; text-decoration: none;">
<h3>Indexing and Querying Data in Redis Hashes</h3>
</a>
<a href="#creating-an-index" id="creating-an-index" style="color: inherit; text-decoration: none;">
<h4>Creating an Index</h4>
</a>
<p>Before we can perform any searches, we need to tell RediSearch how to index our data, and which Redis keys to find that data in. The <a href="https://oss.redis.com/redisearch/Commands/#ftcreate">FT.CREATE</a> command creates a RediSearch index. Here&#39;s how to use it to create an index we&#39;ll call <code>idx:animals</code> where we want to index hashes containing <code>name</code>, <code>species</code> and <code>age</code> fields, and whose key names in Redis begin with the prefix <code>noderedis:animals</code>:</p>
<pre><code class="language-javascript"><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">ft</span><span class="hl-1">.</span><span class="hl-3">create</span><span class="hl-1">(</span><span class="hl-4">&#39;idx:animals&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">type:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">TEXT</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">SORTABLE:</span><span class="hl-1"> </span><span class="hl-6">true</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">TAG</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">NUMERIC</span><br/><span class="hl-1">}, {</span><br/><span class="hl-1"> </span><span class="hl-2">ON:</span><span class="hl-1"> </span><span class="hl-4">&#39;HASH&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">PREFIX:</span><span class="hl-1"> </span><span class="hl-4">&#39;noderedis:animals&#39;</span><br/><span class="hl-1">});</span>
</code></pre>
<p>See the <a href="https://oss.redis.com/redisearch/Commands/#ftcreate"><code>FT.CREATE</code> documentation</a> for information about the different field types and additional options.</p>
<a href="#querying-the-index" id="querying-the-index" style="color: inherit; text-decoration: none;">
<h4>Querying the Index</h4>
</a>
<p>Once we&#39;ve created an index, and added some data to Redis hashes whose keys begin with the prefix <code>noderedis:animals</code>, we can start writing some search queries. RediSearch supports a rich query syntax for full-text search, faceted search, aggregation and more. Check out the <a href="https://oss.redis.com/redisearch/Commands/#ftsearch"><code>FT.SEARCH</code> documentation</a> and the <a href="https://oss.redis.com/redisearch/Query_Syntax/">query syntax reference</a> for more information.</p>
<p>Let&#39;s write a query to find all the animals where the <code>species</code> field has the value <code>dog</code>:</p>
<pre><code class="language-javascript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-5">results</span><span class="hl-1"> = </span><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">ft</span><span class="hl-1">.</span><span class="hl-3">search</span><span class="hl-1">(</span><span class="hl-4">&#39;idx:animals&#39;</span><span class="hl-1">, </span><span class="hl-4">&#39;@species:{dog}&#39;</span><span class="hl-1">);</span>
</code></pre>
<p><code>results</code> looks like this:</p>
<pre><code class="language-javascript"><span class="hl-1">{</span><br/><span class="hl-1"> </span><span class="hl-7">total</span><span class="hl-1">: </span><span class="hl-8">2</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">documents</span><span class="hl-1">: [</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">id:</span><span class="hl-1"> </span><span class="hl-4">&#39;noderedis:animals:4&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">value:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-4">&#39;Fido&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-4">&#39;dog&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-4">&#39;7&#39;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">id:</span><span class="hl-1"> </span><span class="hl-4">&#39;noderedis:animals:3&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">value:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">name:</span><span class="hl-1"> </span><span class="hl-4">&#39;Rover&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">species:</span><span class="hl-1"> </span><span class="hl-4">&#39;dog&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">age:</span><span class="hl-1"> </span><span class="hl-4">&#39;9&#39;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> ]</span><br/><span class="hl-1">}</span>
</code></pre>
<a href="#indexing-and-querying-data-with-redisjson" id="indexing-and-querying-data-with-redisjson" style="color: inherit; text-decoration: none;">
<h3>Indexing and Querying Data with RedisJSON</h3>
</a>
<p>RediSearch can also index and query JSON documents stored in Redis using the RedisJSON module. The approach is similar to that for indexing and searching data in hashes, but we can now use JSON Path like syntax and the data no longer has to be flat name/value pairs - it can contain nested objects and arrays.</p>
<a href="#creating-an-index-1" id="creating-an-index-1" style="color: inherit; text-decoration: none;">
<h4>Creating an Index</h4>
</a>
<p>As before, we create an index with the <code>FT.CREATE</code> command, this time specifying we want to index JSON documents that look like this:</p>
<pre><code class="language-javascript"><span class="hl-1">{</span><br/><span class="hl-1"> </span><span class="hl-7">name</span><span class="hl-1">: </span><span class="hl-4">&#39;Alice&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">age</span><span class="hl-1">: </span><span class="hl-8">32</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-7">coins</span><span class="hl-1">: </span><span class="hl-8">100</span><br/><span class="hl-1">}</span>
</code></pre>
<p>Each document represents a user in some system, and users have name, age and coins properties.</p>
<p>One way we might choose to index these documents is as follows:</p>
<pre><code class="language-javascript"><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">ft</span><span class="hl-1">.</span><span class="hl-3">create</span><span class="hl-1">(</span><span class="hl-4">&#39;idx:users&#39;</span><span class="hl-1">, {</span><br/><span class="hl-1"> </span><span class="hl-4">&#39;$.name&#39;</span><span class="hl-2">:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">type:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">TEXT</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">SORTABLE:</span><span class="hl-1"> </span><span class="hl-4">&#39;UNF&#39;</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">&#39;$.age&#39;</span><span class="hl-2">:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">type:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">NUMERIC</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">AS:</span><span class="hl-1"> </span><span class="hl-4">&#39;age&#39;</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">&#39;$.coins&#39;</span><span class="hl-2">:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-2">type:</span><span class="hl-1"> </span><span class="hl-2">SchemaFieldTypes</span><span class="hl-1">.</span><span class="hl-5">NUMERIC</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">AS:</span><span class="hl-1"> </span><span class="hl-4">&#39;coins&#39;</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">}, {</span><br/><span class="hl-1"> </span><span class="hl-2">ON:</span><span class="hl-1"> </span><span class="hl-4">&#39;JSON&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-2">PREFIX:</span><span class="hl-1"> </span><span class="hl-4">&#39;noderedis:users&#39;</span><br/><span class="hl-1">});</span>
</code></pre>
<p>Note that we&#39;re using JSON Path to specify where the fields to index are in our JSON documents, and the <code>AS</code> clause to define a name/alias for each field. We&#39;ll use these when writing queries.</p>
<a href="#querying-the-index-1" id="querying-the-index-1" style="color: inherit; text-decoration: none;">
<h4>Querying the Index</h4>
</a>
<p>Now we have an index and some data stored as JSON documents in Redis (see the <a href="https://github.com/redis/node-redis/tree/master/packages/json">JSON package documentation</a> for examples of how to store JSON), we can write some queries...</p>
<p>We&#39;ll use the <a href="https://oss.redis.com/redisearch/Query_Syntax/">RediSearch query language</a> and <a href="https://oss.redis.com/redisearch/Commands/#ftsearch"><code>FT.SEARCH</code></a> command. Here&#39;s a query to find users under the age of 30:</p>
<pre><code class="language-javascript"><span class="hl-0">await</span><span class="hl-1"> </span><span class="hl-2">client</span><span class="hl-1">.</span><span class="hl-2">ft</span><span class="hl-1">.</span><span class="hl-3">search</span><span class="hl-1">(</span><span class="hl-4">&#39;idx:users&#39;</span><span class="hl-1">, </span><span class="hl-4">&#39;@age:[0 30]&#39;</span><span class="hl-1">);</span>
</code></pre>
</div></div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Settings</h3></summary>
<div class="tsd-accordion-details">
<div class="tsd-filter-visibility">
<h4 class="uppercase">Member Visibility</h4><form>
<ul id="tsd-filter-options">
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li>
<li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div>
<div class="tsd-theme-toggle">
<h4 class="uppercase">Theme</h4><select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div>
<nav class="tsd-navigation primary">
<details class="tsd-index-accordion" open><summary class="tsd-accordion-summary">
<h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z" fill="var(--color-text)"></path></svg> Modules</h3></summary>
<div class="tsd-accordion-details">
<ul>
<li class="current selected"><a href="modules.html">@redis/search</a>
<ul>
<li class="tsd-kind-module"><a href="modules/commands.html">commands</a></li>
<li class="tsd-kind-module"><a href="modules/commands_AGGREGATE.html">commands/AGGREGATE</a></li>
<li class="tsd-kind-module"><a href="modules/commands_AGGREGATE_WITHCURSOR.html">commands/AGGREGATE_<wbr/>WITHCURSOR</a></li>
<li class="tsd-kind-module"><a href="modules/commands_ALIASADD.html">commands/ALIASADD</a></li>
<li class="tsd-kind-module"><a href="modules/commands_ALIASDEL.html">commands/ALIASDEL</a></li>
<li class="tsd-kind-module"><a href="modules/commands_ALIASUPDATE.html">commands/ALIASUPDATE</a></li>
<li class="tsd-kind-module"><a href="modules/commands_ALTER.html">commands/ALTER</a></li>
<li class="tsd-kind-module"><a href="modules/commands_CONFIG_GET.html">commands/CONFIG_<wbr/>GET</a></li>
<li class="tsd-kind-module"><a href="modules/commands_CONFIG_SET.html">commands/CONFIG_<wbr/>SET</a></li>
<li class="tsd-kind-module"><a href="modules/commands_CREATE.html">commands/CREATE</a></li>
<li class="tsd-kind-module"><a href="modules/commands_CURSOR_DEL.html">commands/CURSOR_<wbr/>DEL</a></li>
<li class="tsd-kind-module"><a href="modules/commands_CURSOR_READ.html">commands/CURSOR_<wbr/>READ</a></li>
<li class="tsd-kind-module"><a href="modules/commands_DICTADD.html">commands/DICTADD</a></li>
<li class="tsd-kind-module"><a href="modules/commands_DICTDEL.html">commands/DICTDEL</a></li>
<li class="tsd-kind-module"><a href="modules/commands_DICTDUMP.html">commands/DICTDUMP</a></li>
<li class="tsd-kind-module"><a href="modules/commands_DROPINDEX.html">commands/DROPINDEX</a></li>
<li class="tsd-kind-module"><a href="modules/commands_EXPLAIN.html">commands/EXPLAIN</a></li>
<li class="tsd-kind-module"><a href="modules/commands_EXPLAINCLI.html">commands/EXPLAINCLI</a></li>
<li class="tsd-kind-module"><a href="modules/commands_INFO.html">commands/INFO</a></li>
<li class="tsd-kind-module"><a href="modules/commands_PROFILE_AGGREGATE.html">commands/PROFILE_<wbr/>AGGREGATE</a></li>
<li class="tsd-kind-module"><a href="modules/commands_PROFILE_SEARCH.html">commands/PROFILE_<wbr/>SEARCH</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SEARCH.html">commands/SEARCH</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SPELLCHECK.html">commands/SPELLCHECK</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGADD.html">commands/SUGADD</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGDEL.html">commands/SUGDEL</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGGET.html">commands/SUGGET</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGGET_WITHPAYLOADS.html">commands/SUGGET_<wbr/>WITHPAYLOADS</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGGET_WITHSCORES.html">commands/SUGGET_<wbr/>WITHSCORES</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGGET_WITHSCORES_WITHPAYLOADS.html">commands/SUGGET_<wbr/>WITHSCORES_<wbr/>WITHPAYLOADS</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SUGLEN.html">commands/SUGLEN</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SYNDUMP.html">commands/SYNDUMP</a></li>
<li class="tsd-kind-module"><a href="modules/commands_SYNUPDATE.html">commands/SYNUPDATE</a></li>
<li class="tsd-kind-module"><a href="modules/commands_TAGVALS.html">commands/TAGVALS</a></li>
<li class="tsd-kind-module"><a href="modules/commands__LIST.html">commands/_<wbr/>LIST</a></li>
<li class="tsd-kind-module"><a href="modules/index.html">index</a></li></ul></li></ul></div></details></nav></div></div>
<div class="container tsd-generator">
<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div>
<div class="overlay"></div><script src="assets/main.js"></script></body></html>