Vector Search

Vector Search enables searching data using natural language queries. We can enable the Vector Field for your fields properties, which will automatically calculate embeddings and generate vector query params. Currently, Vector Search can only be enabled for #TypeString and cannot be used in conjunction with nested or array types.

<span><span style="color: var(--shiki-color-text)">Product: tailordb.#Type </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  Name:        </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Product</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  Description: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Product model</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  Fields: {</span></span>
<span><span style="color: var(--shiki-color-text)">    </span><span style="color: var(--shiki-token-comment)">// ...</span></span>
<span><span style="color: var(--shiki-color-text)">    description: {</span></span>
<span><span style="color: var(--shiki-color-text)">      Type:        tailordb.#TypeString</span></span>
<span><span style="color: var(--shiki-color-text)">      Description: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Product description</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">      </span><span style="color: var(--shiki-token-comment)">// enable vector search</span></span>
<span><span style="color: var(--shiki-color-text)">      Vector: </span><span style="color: var(--shiki-token-constant)">true</span></span>
<span><span style="color: var(--shiki-color-text)">    }</span></span>
<span><span style="color: var(--shiki-color-text)">  }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span></span>

We can use Vector Search specifying vector: {<field name>: "natural language query"}, which returns results in the order they match the query:

<span><span style="color: var(--shiki-token-keyword)">query</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-function)">VectorSearchProduct</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  products(</span></span>
<span><span style="color: var(--shiki-color-text)">    vector: {</span><span style="color: var(--shiki-token-string)">description</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-string-expression)">&quot;What products are associated with A and B?&quot;</span><span style="color: var(--shiki-color-text)">}</span></span>
<span><span style="color: var(--shiki-color-text)">  )</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

It is useful to specify the first and to use a query filter to narrow down the results with Vector Search.

<span><span style="color: var(--shiki-token-keyword)">query</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-function)">VectorSearchProduct</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  products(</span></span>
<span><span style="color: var(--shiki-color-text)">    first: </span><span style="color: var(--shiki-token-constant)">10</span></span>
<span><span style="color: var(--shiki-color-text)">    query: { </span><span style="color: var(--shiki-token-string)">name</span><span style="color: var(--shiki-color-text)">: { </span><span style="color: var(--shiki-token-string)">contains</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-string-expression)">&quot;XX&quot;</span><span style="color: var(--shiki-color-text)"> } }</span></span>
<span><span style="color: var(--shiki-color-text)">    vector: {</span><span style="color: var(--shiki-token-string)">description</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-string-expression)">&quot;What products are associated with A and B?&quot;</span><span style="color: var(--shiki-color-text)">}</span></span>
<span><span style="color: var(--shiki-color-text)">  )</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>