Skip to content

Vector search

Hybrid search

Full-text search and vector search find different things, and the two together usually rank better than either alone. This page compares them on one dataset, then fuses their rankings with search::rrf and related helpers.

SurrealDB supports full-text search and Vector Search. Full-text search (FTS) involves indexing documents using an FTS index that makes use of an analyzer that breaks down text using tokenizers and filters.

Google search for the word 'lead'

The image above is a Google search for the word “lead”, a word with more than one definition (and pronunciation!). Lead can mean 'taking initiative', as well as the chemical element with the symbol 'Pb'.

Consider this in the context of a database of liquid samples which note down harmful chemicals that are found in them.

In the example below, we have a table called liquids with a sample field and a content field. Next, we can define a full-text index on the content field by first defining an analyzer called liquid_analyzer. We can then define an index on the content field in the liquid table and set our custom analyzer (liquid_analyzer) to search through the index.

Then, using the select statement to retrieve all the samples containing the chemical lead will also bring up samples that mention the word lead.

If you read through the content of the tap water sample, you’ll notice that it does not contain any lead in it but it has the mention of the word lead under “The team lead by Dr. Rose…” which means that the team was guided by Dr. Rose.

The search pulled up both the records although the tap water sample had no lead in it. This example shows us that while full-text search does a great job at matching query terms with indexed documents, on its own it may not be the best solution for use cases where the query terms have deeper context and scope for ambiguity.

A vector search compares meaning instead of words, so it can tell the metal from the other meaning of the word. For vector-side retrieval on its own, see Similarity search.

As mentioned above, full-text search and vector search can both be used in SurrealDB. In addition, some functions exist inside the search:: namespace that take both full-text and vector arguments in order to produce a single unified output.

To try them on the liquid samples, each sample needs an embedding next to its text. An embedding model produces vectors with hundreds of dimensions; the three hand-written dimensions below stand for "chemical contamination", "people and organisations" and "water in general", so the sea water and sewage samples point the same way and the tap water sample does not.

INSERT INTO liquids [
	{
		id: liquids:sea,
		sample: 'Sea water',
		content: 'The sea water contains some amount of lead',
		embedding: [0.9, 0.1, 0.3]
	},
	{
		id: liquids:tap,
		sample: 'Tap water',
		content: 'The team lead by Dr. Rose found out that the tap water was potable',
		embedding: [0.1, 0.9, 0.4]
	},
	{
		id: liquids:sewage,
		sample: 'Sewage water',
		content: 'High amounts of heavy metals were found in the sewage water',
		embedding: [0.8, 0.1, 0.4]
	}
] RETURN NONE;

DEFINE ANALYZER liquid_analyzer TOKENIZERS blank, class, camel, punct FILTERS snowball(english);
DEFINE INDEX liquid_content ON liquids FIELDS content FULLTEXT ANALYZER liquid_analyzer BM25;

The vector index goes on the embedding field:

DEFINE INDEX liquid_embedding ON liquids FIELDS embedding HNSW DIMENSION 3 DIST COSINE;

The function search::rrf() combines ranked lists with an algorithm called reciprocal rank fusion. The query below searches for lead both ways: the vector search ranks all three samples by how close they are to the query vector, and the full-text search returns the two best matches for the word lead.

-- Setup from the examples above
INSERT INTO liquids [
	{ id: liquids:sea, content: 'The sea water contains some amount of lead', embedding: [0.9, 0.1, 0.3] },
	{ id: liquids:tap, content: 'The team lead by Dr. Rose found out that the tap water was potable', embedding: [0.1, 0.9, 0.4] },
	{ id: liquids:sewage, content: 'High amounts of heavy metals were found in the sewage water', embedding: [0.8, 0.1, 0.4] }
] RETURN NONE;
DEFINE ANALYZER liquid_analyzer TOKENIZERS blank, class, camel, punct FILTERS snowball(english);
DEFINE INDEX liquid_content ON liquids FIELDS content FULLTEXT ANALYZER liquid_analyzer BM25;
DEFINE INDEX liquid_embedding ON liquids FIELDS embedding HNSW DIMENSION 3 DIST COSINE;

-- Query vector (what the embedding model produced for "lead contamination")
LET $qvec = [0.85, 0.05, 0.35];

-- Vector search: the 3 nearest neighbours
LET $vs = SELECT id FROM liquids WHERE embedding <|3,100|> $qvec;

-- Full-text search: the 2 best lexical matches
LET $ft = SELECT id, search::score(1) AS score FROM liquids
	WHERE content @1@ 'lead' ORDER BY score DESC LIMIT 2;

-- Fuse with reciprocal rank fusion (k defaults to 60 if omitted)
SELECT id, rrf_score FROM search::rrf([$vs, $ft], 3, 60);
Output
[
	{ id: liquids:sea, rrf_score: 0.03278688524590164f },
	{ id: liquids:tap, rrf_score: 0.03200204813108039f },
	{ id: liquids:sewage, rrf_score: 0.016129032258064516f }
]

The vector search returns sea water, sewage and then tap water. The full-text search returns sea water and then tap water, because both mention the word. Each list gives a record 1 / (k + rank), and the fused score is the sum, so sea water comes first with a share from both lists. Tap water comes second: it is last in the vector list, but its share from the full-text list is enough to pass sewage, which only one list contains.

Two ranked lists fused by search::rrf with k = 60. The vector search ranks liquids:sea first, worth 1 / 61, liquids:sewage second, worth 1 / 62, and liquids:tap third, worth 1 / 63. The full-text search for 'lead' ranks liquids:sea first, worth 1 / 61, and liquids:tap second, worth 1 / 62. The fused list ranks liquids:sea first with 0.0328 and liquids:tap second with 0.0320, each the sum of its shares from both lists, and liquids:sewage third with 0.0161 from the vector list alone.Two ranked lists fused by search::rrf with k = 60. The vector search ranks liquids:sea first, worth 1 / 61, liquids:sewage second, worth 1 / 62, and liquids:tap third, worth 1 / 63. The full-text search for 'lead' ranks liquids:sea first, worth 1 / 61, and liquids:tap second, worth 1 / 62. The fused list ranks liquids:sea first with 0.0328 and liquids:tap second with 0.0320, each the sum of its shares from both lists, and liquids:sewage third with 0.0161 from the vector list alone.

Reciprocal rank fusion uses only the position of a record in each list, and rewards a record that several lists agree on. Where one list is more reliable, search::linear() combines the scores instead, with a weight for each list. A weight of 2 for the vector list and 1 for the full-text list moves the sewage sample above the tap water sample, which only matched a word:

-- Setup from the examples above
INSERT INTO liquids [
	{ id: liquids:sea, content: 'The sea water contains some amount of lead', embedding: [0.9, 0.1, 0.3] },
	{ id: liquids:tap, content: 'The team lead by Dr. Rose found out that the tap water was potable', embedding: [0.1, 0.9, 0.4] },
	{ id: liquids:sewage, content: 'High amounts of heavy metals were found in the sewage water', embedding: [0.8, 0.1, 0.4] }
] RETURN NONE;
DEFINE ANALYZER liquid_analyzer TOKENIZERS blank, class, camel, punct FILTERS snowball(english);
DEFINE INDEX liquid_content ON liquids FIELDS content FULLTEXT ANALYZER liquid_analyzer BM25;
DEFINE INDEX liquid_embedding ON liquids FIELDS embedding HNSW DIMENSION 3 DIST COSINE;
LET $qvec = [0.85, 0.05, 0.35];
LET $vs = SELECT id FROM liquids WHERE embedding <|3,100|> $qvec;
LET $ft = SELECT id, search::score(1) AS score FROM liquids
	WHERE content @1@ 'lead' ORDER BY score DESC LIMIT 2;

-- Weight the vector list twice as much as the full-text list
SELECT id, linear_score FROM search::linear([$vs, $ft], [2, 1], 3, 'minmax');
Output
[
	{ id: liquids:sea, linear_score: 3f },
	{ id: liquids:sewage, linear_score: 0.5f },
	{ id: liquids:tap, linear_score: 0f }
]

The vector rows carry no score field, so search::linear scores them by rank, and 'minmax' scales each list to the range 0 to 1 before the weights apply. Sea water is at the top of both lists and scores 2 + 1. Sewage is second in the vector list only, and tap water is last in the vector list and last of the two full-text matches, so both of its normalised scores are 0.

Note

Before SurrealDB 4.0, the lexical half of a hybrid query on a small dataset can contribute membership without contributing order. BM25 clamps the weight of any term appearing in half or more of the indexed documents to zero, so search::score returns 0 for every match and the ORDER BY score DESC above has nothing to sort on. Reciprocal rank fusion then folds in an arbitrary ordering of the matched records. From SurrealDB 4.0, every match scores above zero. See how common terms are weighted.

Was this page helpful?

Last updated