Skip to main content

Conduct a Hybrid Search

Hybrid search is a combination of vector search and attribute filtering. By specifying expressions that filter on scalar fields you can narrow down the results of a vector search making them more relevant.

The following example demonstrates how to perform a hybrid search. Suppose you want to search for product reviews based on vector embeddings, but you only want those for a certain product category. You can do this by performing a vector search as usual, but this time also specifying a filter expression that filters on the product category.

Create the Vector Database client

import { VectorDocumentStore } from "@tigrisdata/vector";

const vectorDocStore = new VectorDocumentStore({
connection: {
serverUrl: "region_uri_here",
projectName: "vectordemo",
clientId: "clientId_here",
clientSecret: "clientSecret_here",
},
indexName: "products",
numDimensions: 3,
});

Add documents to the index

await vectorDocStore.addDocumentsWithVectors({
ids: ["1", "2", "3"],
embeddings: [
[1.2, 2.3, 4.5],
[6.7, 8.2, 9.2],
[1.0, 2.1, 3.2],
],
documents: [
{
content: "This is a great product. I would recommend it to anyone.",
metadata: {
ProductCategory: "Electronics",
Rating: 5,
},
},
{
content: "This is a bad product. I would not recommend it to anyone.",
metadata: {
ProductCategory: "Books",
Rating: 1,
},
},
{
content: "This is a good product. I would recommend it to anyone.",
metadata: {
ProductCategory: "Electronics",
Rating: 4,
},
},
],
});

Perform a hybrid search query

Now we can query the index for similar documents by providing the query vector. But we will limit the search results to documents that have a product category of "Electronics".

const results = await vectorDocStore.similaritySearchVectorWithScore({
query: [1.1, 2.2, 3.3],
k: 10,
filter: {
"metadata.ProductCategory": "Electronics",
},
});
console.log(JSON.stringify(results, null, 2));

Here the filter option is used to specify the attribute filtering expression. The filter option is an object where the keys are the metadata attributes of the documents and the values are the values to filter on.

The filter option can also be used to filter on multiple attributes. For example, if you wanted to filter on both the product category and the rating, you could do the following:

const results = await vectorDocStore.similaritySearchVectorWithScore({
query: [1.1, 2.2, 3.3],
k: 10,
filter: {
$and: [
{
"metadata.ProductCategory": "Electronics",
},
{
"metadata.Rating": {
$gt: 4,
},
},
],
},
});
console.log(JSON.stringify(results, null, 2));