<a id="fts-working-with"></a>

# Working with Full Text Search

Full text search retrieves rows by matching the *terms* in a query against the
text stored in a column, and ranks the matches by relevance using the
[BM25](https://en.wikipedia.org/wiki/Okapi_BM25) algorithm. It is the right
tool when the exact words matter — searching articles, product descriptions,
support tickets, or log messages.

Full text search is one half of ScyllaDB’s
[Vector and Text Search](https://cloud.docs.scylladb.com/stable/vector-search/index.md) feature. It runs on the
same indexing nodes as [vector search](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md)
and is enabled by the same deployment steps, so everything in
[Vector and Text Search Deployments](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-clusters.md)
applies here as well.

<a id="fts-prerequisites"></a>

## Prerequisites

* A cluster with Vector and Text Search enabled, running a ScyllaDB version
  that supports the full text search features you need — see
  [Feature Compatibility Matrix](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-clusters.md#vs-feature-compatibility). See [Creating a New Cluster with Vector and Text Search Enabled](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-clusters.md#vs-create-cluster) to create a
  cluster, or [Enabling Vector and Text Search on an Existing Cluster](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-clusters.md#vs-enable-existing) to add indexing nodes to a cluster you
  already have.
* `cqlsh`, the web-based CQL console in the ScyllaDB Cloud UI, or any CQL
  driver.

<a id="fts-workflow"></a>

## Workflow

1. Create a table with a `text`, `varchar`, or `ascii` column holding the
   text you want to search.
2. Create a full-text index on that column.
3. Wait for the index to finish building — see [Checking Index Status](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-index-status).
4. Run `BM25()` queries against the indexed column.

<a id="fts-create-index"></a>

## Creating a Full-Text Index

Create a full-text index with `CREATE CUSTOM INDEX` and the
`fulltext_index` class:

```cql
CREATE KEYSPACE blog
  WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': 3};

CREATE TABLE blog.articles (
  id int PRIMARY KEY,
  author text,
  body text
);

CREATE CUSTOM INDEX articles_body_fts ON blog.articles(body)
  USING 'fulltext_index';
```

`IF NOT EXISTS` is supported, and the index name may be omitted — ScyllaDB
then derives one from the table and column names, such as
`articles_body_idx`.

Use `DESCRIBE INDEX` to inspect an existing index:

```cql
DESCRIBE INDEX blog.articles_body_fts;
```

<a id="fts-indexable-columns"></a>

### Which Columns Can Be Indexed

A full-text index can only be created on a regular (non-key, non-static)
text-typed column:

| Column                                          | Supported                                                                                                                                                                                             |
|-------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `text`, `varchar`, `ascii` regular column       | Yes.                                                                                                                                                                                                  |
| Partition key, clustering key, or static column | No. ScyllaDB rejects the statement with `Creating a fulltext index<br/>requires a regular column, but column <name> is a clustering key<br/>column` (or `a partition key column`, `a static column`). |
| Any non-text type, such as `int` or `blob`      | No. ScyllaDB rejects the statement with `Fulltext index is only<br/>supported on text, varchar, or ascii columns, but column <name> has an<br/>incompatible type`.                                    |

A single table can carry several full-text indexes, as long as each one
targets a different column. A full-text index and a
[vector index](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-vector-index) can also coexist on the same table —
see [Combining Full Text and Vector Search](#fts-hybrid).

<a id="fts-index-options"></a>

### Index Options

Two options control how the index treats text. Both are set at creation time
with `WITH OPTIONS` and cannot be changed afterwards:

| Option      | Default    | Description                                                                                                                                                                                                                                         |
|-------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `analyzer`  | `standard` | How text is split into terms and normalized. See<br/>[Choosing an Analyzer](#fts-analyzers).                                                                                                                                                        |
| `positions` | `true`     | Whether token positions are stored. **Phrase queries need them**: an<br/>index created with `false` answers term and boolean queries but<br/>rejects a phrase of more than one term. See [Phrase Queries and the positions Option](#fts-positions). |
```cql
CREATE CUSTOM INDEX articles_body_fts ON blog.articles(body)
  USING 'fulltext_index'
  WITH OPTIONS = {'analyzer': 'english'};
```

Option values are case-insensitive, so `'english'` and `'ENGLISH'` are
equivalent. An unknown option, or an unsupported value, is rejected when the
index is created:

```none
Invalid value in option 'analyzer' for fulltext index: 'klingon'. Supported
are case-insensitive: standard, english, german, french, spanish, italian,
portuguese, russian, simple, whitespace

Invalid value in option 'positions' for fulltext index: 'sometimes'.
Supported are case-insensitive: false, true

Unsupported option nonsense for fulltext index
```

There is no `ALTER INDEX` statement in CQL, so changing either option means
dropping the index and creating it again. The replacement index is rebuilt from
the base table, and queries against the column fail until it is serving again.

<a id="fts-index-build"></a>

### Index Building

Full-text indexes are populated through Change Data Capture, exactly like
vector indexes, so the same requirements apply: the keyspace must use tablets
and CDC is enabled automatically on the base table. See
[Tablets Requirement](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vector-search-tablets-info) and
[CDC Requirements](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-vector-index-cdc).

After you create the index, the indexing node discovers it and scans the base
table. Queries fail until that scan completes:

```none
# Immediately after CREATE, before the indexing node has picked up the index
Vector Store error: HTTP status 404 Not Found, message: missing index: blog.articles_body_fts

# While the base table is being scanned
Vector Store error: HTTP status 503 Service Unavailable, message: Index
blog.articles_body_fts is not available yet as it is still being constructed,
progress: 39.804%
```

Retry until the query succeeds, or follow the build in
`system.custom_indexes` — the index is ready when its `index_state` is
`SERVING`. See [Checking Index Status](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-index-status). Build time scales with the number of
rows and the size of the indexed text. The progress in the 503 message is the share of
the token range scanned so far rather than of the rows indexed. It tracks the
build closely on most tables, but can be misleading when data is concentrated
in a few large partitions — see [Index build progress monitoring](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-troubleshooting.md#vs-index-build-progress).

#### NOTE
`Vector Store` in these messages refers to the **indexing node** that
serves both vector and full-text indexes. The messages are reproduced here
as the server emits them.

<a id="fts-queries"></a>

## Running Full Text Search Queries

A full text search query has this shape:

```cql
SELECT <columns> FROM <table>
  WHERE BM25(<column>, '<query>') > 0
  ORDER BY BM25(<column>, '<query>')
  LIMIT <n>;
```

For example:

```cql
SELECT id, body FROM blog.articles
  WHERE BM25(body, 'photosynthesis') > 0
  ORDER BY BM25(body, 'photosynthesis')
  LIMIT 10;
```

Every clause of that shape is mandatory. Besides ordinary columns, the
`SELECT` list may return each row’s relevance score and rank
([Returning Scores and Ranks](#fts-scores)) and a highlighted excerpt ([Highlighting Matches](#fts-highlighting)). A
query that ranks rows by a full text search and a vector search together has a
different shape, described in [Combining Full Text and Vector Search](#fts-hybrid).

ScyllaDB rejects any other deviation. The rules, with the errors that
ScyllaDB 2026.4 returns when they are broken (earlier versions word some of
them differently), are:

| Rule                                                                                                                                                                                                                            | Error when violated                                                                                                                                                                                                               |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| The query must include a `WHERE BM25()` restriction — an<br/>`ORDER BY BM25()` on its own is not enough.                                                                                                                        | `Full-text search queries require a WHERE BM25() > 0 clause`                                                                                                                                                                      |
| The `WHERE BM25()` restriction must match an `ORDER BY BM25()`<br/>search on the same column with the same search term.                                                                                                         | `BM25() in WHERE must match a BM25 search in ORDER BY, with the same<br/>column and search term;` followed by `ORDER BY has no BM25 search`,<br/>`ORDER BY has no BM25 search on column <name>`, or `the search term<br/>differs` |
| The query must include a `LIMIT`.                                                                                                                                                                                               | `Full-text search queries must have a limit specified`                                                                                                                                                                            |
| `LIMIT` must not exceed 1000.                                                                                                                                                                                                   | `Full-text search queries require a LIMIT that is not greater than<br/>1000. LIMIT was 1001`                                                                                                                                      |
| The comparison value must be the literal `0`.                                                                                                                                                                                   | `BM25 function comparison value must be the literal 0`                                                                                                                                                                            |
| The only supported operator is `>`.                                                                                                                                                                                             | `Unsupported ">=" relation for BM25 function restriction, only ">" is<br/>supported`                                                                                                                                              |
| No other `WHERE` restriction is allowed, including on the partition<br/>key, and `ALLOW FILTERING` does not help.                                                                                                               | `Full-text search queries do not support additional WHERE<br/>restrictions`                                                                                                                                                       |
| Only one `BM25()` restriction is allowed per query.                                                                                                                                                                             | `Full-text search queries support only one WHERE BM25() restriction`                                                                                                                                                              |
| The targeted column must have a full-text index.                                                                                                                                                                                | `No fulltext index found for full-text search query`                                                                                                                                                                              |
| A score, rank, or excerpt in the `SELECT` list must refer to the same<br/>column and search term as `ORDER BY BM25()`.                                                                                                          | `BM25_SCORE() in SELECT must match a BM25 search in ORDER BY, with<br/>the same column and search term; the search term differs` (and the<br/>same for `BM25()`, `BM25_RANK()`, and `BM25_HIGHLIGHT()`)                           |
| `PER PARTITION LIMIT` is not supported.                                                                                                                                                                                         | `Full-text search queries do not support per-partition limits`                                                                                                                                                                    |
| Aggregate functions and `GROUP BY` are not supported.                                                                                                                                                                           | `Full-text search queries cannot be run with aggregation`                                                                                                                                                                         |
| `ORDER BY BM25()` cannot be combined with any other ordering — a<br/>regular column, a second `BM25()`, or `ANN OF`. To rank rows by a<br/>full text search and a vector search together, use<br/>[hybrid search](#fts-hybrid). | `bm25 ordering does not support any other ordering`,<br/>`Similarity ordering does not support any other ordering`, or<br/>`Cannot specify more than one similarity ordering`, depending on<br/>the clause order                  |

`ORDER BY BM25()` does not accept `ASC` or `DESC`; results always come
back with the highest relevance first.

#### NOTE
Because a full text search query cannot carry any additional `WHERE`
restriction, it cannot be narrowed to a partition or filtered on another
column. Retrieve the top matches first, then apply any further filtering in
your application.

<a id="fts-paging"></a>

### Result Size and Paging

`LIMIT` is capped at 1000, and full text search results are never paged —
the server always returns the whole result set in a single response. If the
result set is larger than the page size your driver requested, ScyllaDB
returns all of the rows anyway and attaches a warning to the response:

```none
Paging is not supported for Full-text search queries. The entire result set
has been returned.
```

Size the `LIMIT` to what your application will actually use, since every
matching row is transferred at once. To reach beyond 1000 matches, narrow the
query with additional terms rather than trying to page through it.

<a id="fts-query-language"></a>

## Text Query Language

The string you pass to `BM25()` is written in its own text query language —
it is not CQL, and it is not treated as a literal phrase. The following
constructs are supported.

<a id="fts-terms"></a>

### Terms

A bare term matches rows whose indexed text contains that term. Under the
default `standard` analyzer — and every analyzer except `whitespace` —
matching is case-insensitive, because both the stored text and the query are
lowercased:

```cql
-- These two queries return the same rows.
SELECT id FROM blog.articles
  WHERE BM25(body, 'photosynthesis') > 0
  ORDER BY BM25(body, 'photosynthesis') LIMIT 10;

SELECT id FROM blog.articles
  WHERE BM25(body, 'PHOTOSYNTHESIS') > 0
  ORDER BY BM25(body, 'PHOTOSYNTHESIS') LIMIT 10;
```

With the `whitespace` analyzer, which lowercases nothing, the two queries
match different rows. See [Choosing an Analyzer](#fts-analyzers).

Several terms separated by whitespace are combined with an implicit **OR**,
so a row matching any one of them is returned — `jupiter saturn` is
equivalent to `jupiter OR saturn`. Rows matching more of the terms rank
higher. Use an explicit [AND](#fts-boolean) when every term must be
present.

<a id="fts-boolean"></a>

### Boolean Operators

`AND`, `OR`, and `NOT` combine terms, and parentheses group them.

#### IMPORTANT
**Operators must be uppercase.** Unlike CQL, which is case-insensitive, the
text query language only recognizes `AND`, `OR`, and `NOT` in upper
case. A lowercase `and`, `or`, or `not` is not an operator — it is
treated as an ordinary search term. Under `standard` and `english` it is
then dropped as a stop word; under `simple`, `whitespace`, and the
non-English language analyzers it stays in the query as a term to match.

Either way the effect is the same and it fails silently: the terms end up
joined by the implicit `OR`, so `database and distributed` does not
narrow anything — it returns the same rows as
`database OR distributed`.

A `-` at the start of a term excludes it, like `NOT`. A `+` makes the
term required, but the other terms stay optional, so it is not the same as
`AND`: `planet +jupiter` matches every row that contains *jupiter*, while
`planet AND jupiter` matches only the rows that contain both terms.

```cql
-- Rows that contain both terms.
SELECT id FROM blog.articles
  WHERE BM25(body, 'database AND distributed') > 0
  ORDER BY BM25(body, 'database AND distributed') LIMIT 10;

-- Rows that contain either term.
SELECT id FROM blog.articles
  WHERE BM25(body, 'jupiter OR saturn') > 0
  ORDER BY BM25(body, 'jupiter OR saturn') LIMIT 10;

-- Rows that contain the first term but not the second.
SELECT id FROM blog.articles
  WHERE BM25(body, 'python NOT snake') > 0
  ORDER BY BM25(body, 'python NOT snake') LIMIT 10;

-- Grouping.
SELECT id FROM blog.articles
  WHERE BM25(body, '(jupiter OR saturn) AND planet NOT rings') > 0
  ORDER BY BM25(body, '(jupiter OR saturn) AND planet NOT rings') LIMIT 10;
```

<a id="fts-phrases"></a>

### Phrases

Double quotes match a sequence of adjacent terms in order. Because the CQL
string itself is single-quoted, the double quotes go inside it:

```cql
SELECT id FROM blog.articles
  WHERE BM25(body, '"theory of relativity"') > 0
  ORDER BY BM25(body, '"theory of relativity"') LIMIT 10;
```

Without the quotes the words become independent terms combined with an
implicit `OR`, so word order and adjacency stop mattering —
`theory of relativity` also matches text that says *relativity theory*,
while the quoted phrase does not.

To search for text that contains special characters, wrap it in double quotes.
Inside the double quotes, `+`, `-` and the characters listed in
[Unsupported Query Syntax](#fts-unsupported-syntax) have no special meaning, and the text is searched
as a phrase. Write `\"` for a double quote and `\\` for a backslash. A
single quote inside the CQL string is written as `''`:

```cql
SELECT id FROM blog.articles
  WHERE BM25(body, '"don''t panic"') > 0
  ORDER BY BM25(body, '"don''t panic"') LIMIT 10;
```

Quoted text that is analyzed into a single term, such as `"panic"`, matches
like that term. Quoted text that is analyzed into more terms is a phrase, even
if it looks like one word: `"don't"` is analyzed into *don* and *t*. Phrase
matching relies on the `positions` option, which is on by default. An index
created with `'positions': 'false'` rejects such a phrase — see
[Phrase Queries and the positions Option](#fts-positions).

<a id="fts-boost"></a>

### Boosting

A `^` and a number after a term or a phrase, such as `database^2` or
`"distributed database"^3`, boost it: its contribution to the score is
multiplied by that number, so rows that match it rank higher:

```cql
SELECT id FROM blog.articles
  WHERE BM25(body, 'database^2 latency') > 0
  ORDER BY BM25(body, 'database^2 latency') LIMIT 10;
```

<a id="fts-analysis"></a>

### How Text Is Analyzed

The index applies the same analysis to the stored text when it is indexed and
to your query when it is parsed — which is why the two always agree on what a
term is. Analysis is made up of up to four steps:

| Step         | Behavior                                                                                                                                                                                                                                                                                                                                                                        |
|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Tokenization | Splits text into terms. Every analyzer except `whitespace` splits on<br/>non-alphanumeric characters, so punctuation separates terms:<br/>`wide-column` is indexed as `wide` and `column`, and each matches<br/>on its own, while `high-throughput,` matches `throughput`. The<br/>`whitespace` analyzer splits on whitespace only, keeping<br/>`wide-column` as a single term. |
| Lowercasing  | Makes matching case-insensitive, so `PHOTOSYNTHESIS` and<br/>`photosynthesis` return the same rows. Performed by every analyzer<br/>except `whitespace`.                                                                                                                                                                                                                        |
| Stop words   | Drops very common words that carry little search signal. Performed by<br/>`standard` and the language analyzers, each in its own language, and<br/>**not** by `simple` or `whitespace`. Under the default analyzer,<br/>searching for `the` alone matches nothing, and `the database`<br/>returns the same rows as `database`.                                                  |
| Stemming     | Reduces words to a common root so that different inflections of the<br/>same word match each other, such as `run` matching *running*.<br/>Performed only by the language analyzers.                                                                                                                                                                                             |

Which of those steps run — and in which language — is set by the
[analyzer](#fts-analyzers).

<a id="fts-analyzers"></a>

### Choosing an Analyzer

The `analyzer` option selects the pipeline. The default, `standard`, is
**English-only**: it removes English stop words and does no stemming at all,
which makes it a reasonable choice for English text that should match on exact
word forms — and a poor one for any other language. For text in another
language, pick that language’s analyzer:

| Analyzer                                                                         | Splits on       | Lowercases   | Stop words    | Stemming      |
|----------------------------------------------------------------------------------|-----------------|--------------|---------------|---------------|
| `standard` (default)                                                             | punctuation     | Yes          | English       | No            |
| `simple`                                                                         | punctuation     | Yes          | None          | No            |
| `whitespace`                                                                     | whitespace only | No           | None          | No            |
| `english`, `german`, `french`, `spanish`, `italian`,<br/>`portuguese`, `russian` | punctuation     | Yes          | that language | that language |

Pick by what you need:

* `standard` — the default. Exact word forms, English stop words removed.
  <br/>
* A language analyzer — when you want different inflections of a word to
  match each other. With `english`, a search for `run` matches *running*
  and *runners*, and `walk` matches *walks*:
  <br/>
  ```cql
  CREATE CUSTOM INDEX articles_body_fts ON blog.articles(body)
    USING 'fulltext_index'
    WITH OPTIONS = {'analyzer': 'english'};
  ```
* `simple` — like `standard` but keeps stop words, so a query for a
  common word such as `the` can still match.
  <br/>
* `whitespace` — no normalization at all beyond splitting on whitespace.
  Case and punctuation are preserved, so `Quick` matches only *Quick*, and
  `hello` does not match *hello,* — the stored term includes the comma.
  Use it for text where case or symbols are meaningful, such as identifiers
  or codes.
  <br/>

#### IMPORTANT
Choose the analyzer that matches the language of your text. The default
`standard` analyzer uses **English** stop words and does no stemming, so
on German text it indexes *die* (not an English stop word) and does not
match `lauf` against *laufen*. The `german` analyzer drops *die* and
stems *laufen* to `lauf`.

Because the analyzer is fixed for the life of the index, and because changing
it means a full rebuild, it is worth deciding before you load data.

<a id="fts-positions"></a>

### Phrase Queries and the positions Option

[Phrase queries](#fts-phrases) match on the recorded position of each
term, so they need the `positions` option, which is on by default. Setting it
to `false` makes the index smaller, at the cost of no longer being able to
answer phrase queries:

```cql
CREATE CUSTOM INDEX articles_body_fts ON blog.articles(body)
  USING 'fulltext_index'
  WITH OPTIONS = {'positions': 'false'};
```

Term and boolean queries work as usual against such an index, but a phrase of
more than one term is rejected rather than silently returning nothing:

```none
Vector Store error: HTTP status 400 Bad Request, message: index.bm25 request
error: fts: failed to parse query: The field 'text' does not have positions
indexed
```

Leave `positions` at its default unless you have measured the memory saving
and know that no query will ever need a phrase.

<a id="fts-unsupported-syntax"></a>

### Unsupported Query Syntax

Fuzzy matching (`term~1`) is parsed without error but matches nothing. A
`*` after a term does not match by prefix: it is ignored, so `jupit*`
matches nothing and `jupiter*` matches the same rows as `jupiter`. Do not
rely on them.

A `*` after a quoted phrase of two or more words matches the last word by
prefix: `"theory of rel"*` matches *theory of relativity*. After a one-word
phrase, such as `"jupit"*`, it is rejected.

Some characters are meaningful to the query parser and make the query fail
rather than match literally:

| Input                                                                 | Result                                                                                                                                                                                    |
|-----------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `:`                                                                   | Read as a field separator. `colon:term` fails with `failed to parse<br/>query: Field does not exist: 'colon'`.                                                                            |
| `(`, `)`                                                              | Grouping. Balanced parentheses are valid syntax, but an unbalanced one<br/>is rejected with `failed to parse query: Syntax Error: <input>`, so<br/>`database (distributed` fails.         |
| `[` `]` `{` `}` and the quote characters `'` `"` and<br/>the backtick | Rejected with `failed to parse query: Syntax Error: <input>`.                                                                                                                             |
| A bare `AND`, `OR`, or `NOT`                                          | Rejected with `failed to parse query: Syntax Error: <input>`.                                                                                                                             |
| A `+` or `-` with no term after it                                    | Rejected with `failed to parse query: Syntax Error: <input>`, so<br/>`planet -` and `planet --rings` fail.                                                                                |
| Only excluded terms                                                   | Rejected with `Invalid query: Only excluding terms given`, so<br/>`-rings` and `NOT rings` fail.                                                                                          |
| A `^` anywhere but between a term or phrase and a number              | Rejected with `failed to parse query: Syntax Error: <input>`, so<br/>`data^base`, `database^` and `^database` fail. `term^N` and<br/>`"phrase"^N` [boost](#fts-boost) the term or phrase. |
| A `\` at the end of the search term                                   | Rejected with `failed to parse query: Syntax Error: <input>`, so<br/>`planet\` fails.                                                                                                     |

Of the ASCII punctuation characters, exactly the eleven in `"'():[]^`{}` are
rejected when they appear inside a term. The rest are passed through to the
analyzer, which splits on them, except a `+` or `-` at the start of a term,
which is an [operator](#fts-boolean).

#### WARNING
Passing raw end-user input straight into `BM25()` can fail on these
characters. Using a bound parameter protects you from CQL injection, but the
value is still parsed as a query expression — so strip or replace the
characters above before searching, or wrap the value in double quotes, with
`"` and `\` escaped, to search it as a [phrase](#fts-phrases).
Quoting a value that is analyzed into more than one term needs the default
[positions](#fts-positions) option. See [Using Full Text Search from a Driver](#fts-drivers).

<a id="fts-ranking"></a>

## Relevance Ranking

Matches are ordered by their BM25 score, highest first. BM25 scores a row on
how often the query terms occur in it, offset by how common those terms are
across the whole index and by the length of the indexed text: a term that
appears in nearly every row contributes little, and a short row that mentions
the term as often as a long one scores higher.

Scores are relative to the current contents of the index: adding or removing
rows changes how common a term is, and with it the score of every row that
contains the term. Use them to compare the rows of one result, not as an
absolute measure of relevance across queries or over time.

<a id="fts-scores"></a>

### Returning Scores and Ranks

Three functions return what the index computed for each row. They can appear
in the `SELECT` list of a full text search query:

* `BM25_SCORE(<column>, '<query>')` — the row’s BM25 relevance score, a
  `float`.
* `BM25_RANK(<column>, '<query>')` — the row’s position in the index’s
  result, an `int` counted from 1.
* `BM25(<column>, '<query>')` — both values as a `tuple<float, int>`.

The examples in this section and the next one use these rows of the
`blog.articles` table created in [Creating a Full-Text Index](#fts-create-index):

```cql
INSERT INTO blog.articles (id, author, body) VALUES (1, 'ann',
  'ScyllaDB is a distributed database built for low latency. A distributed database spreads data across many nodes.');
INSERT INTO blog.articles (id, author, body) VALUES (2, 'bob',
  'Photosynthesis turns light into chemical energy in plants.');
INSERT INTO blog.articles (id, author, body) VALUES (3, 'ann',
  'When choosing a database, consider latency, throughput and how the data is distributed.');
INSERT INTO blog.articles (id, author, body) VALUES (4, 'eve',
  'Plants need light, water and patience.');
```

```cql
SELECT id,
       BM25_SCORE(body, 'database') AS score,
       BM25_RANK(body, 'database') AS rank
  FROM blog.articles
  WHERE BM25(body, 'database') > 0
  ORDER BY BM25(body, 'database')
  LIMIT 10;
```

```none
 id | score    | rank
----+----------+------
  1 | 0.820252 |    1
  3 | 0.668293 |    2
```

Each function must use the same column and search term as the
`ORDER BY BM25()` clause, and the query still makes a single request to the
index however many of them it selects. Give each one an alias with `AS`:
without it, the result column gets a generated name such as
`system_bm25_score_body___database`.

The rank is the row’s position in the index’s result, not in the rows you get
back. If the index returns a row that no longer exists in the table — because
it was deleted a moment ago, for example — that row is left out of the result,
and the ranks after it are not renumbered.

In the `WHERE` clause, `BM25_SCORE(<column>, '<query>') > 0` is accepted
as an alternative spelling of `BM25(<column>, '<query>') > 0`.
`BM25_RANK()` is not accepted there.

<a id="fts-highlighting"></a>

### Highlighting Matches

`BM25_HIGHLIGHT(<column>, '<query>')` returns an excerpt of the searched text
with the matched terms marked, so that a search results page can show why each
row was returned:

```cql
SELECT id,
       BM25_HIGHLIGHT(body, 'distributed database') AS excerpt
  FROM blog.articles
  WHERE BM25(body, 'distributed database') > 0
  ORDER BY BM25(body, 'distributed database')
  LIMIT 10;
```

```none
 id | excerpt
----+---------------------------------------------------------------------------------------------------------------------------------------------
  1 | ScyllaDB is a <b>distributed</b> <b>database</b> built for low latency. A <b>distributed</b> <b>database</b> spreads data across many nodes
  3 |                                        When choosing a <b>database</b>, consider latency, throughput and how the data is <b>distributed</b>
```

The excerpt is a single fragment of the column’s text, of type `text`, and
it is HTML: each matched term is wrapped in `<b>` and `</b>`, and the rest
of the text is HTML-escaped — a stored `<script> & "quotes"` comes back as
`&lt;script&gt; &amp; &quot;quotes&quot;`. Insert the excerpt into a page as
HTML, as returned; escaping it again would display the `<b>` tags as text,
and un-escaping it would undo the protection against markup stored in your
data. Because matching goes through the index’s
[analyzer](#fts-analyzers), the marked words are those the analyzer
matched — with a stemming analyzer, *databases* is marked for a search on
*database*.

`BM25_HIGHLIGHT()` follows the same rules as the scoring functions: it is
accepted only in the `SELECT` list, and must use the same column and search
term as `ORDER BY BM25()`. Its value is `null` for a row in which no
fragment could be found; the row itself is still returned.

Asking for an excerpt costs one extra round trip to the indexing nodes, made
after the matching rows have been read and carrying the text of those rows.
Queries that do not select `BM25_HIGHLIGHT()` are not affected.

<a id="fts-write-latency"></a>

## Write-to-Query Latency

Inserts, updates, and deletes reach the full-text index asynchronously through
CDC, so a write becomes searchable a short time after it is acknowledged —
expect a few seconds. Deleting a row removes it from the index, and updating an
indexed column replaces the old text, so the previous terms stop matching.

If a query must reflect a write immediately, read the row by primary key
instead.

#### WARNING
**Cell-level TTL leaves stale entries in the index.** Expiration through
`USING TTL` on an `INSERT` or `UPDATE`, or through the
`default_time_to_live` table property, does not generate a CDC event. The
value becomes unreadable at its deadline but the indexing node never hears
about it, so the index keeps an entry for the expired row.

Query results stay correct — ScyllaDB filters the expired rows out — but
the stale entries still occupy memory and, more visibly, consume candidate
slots against your `LIMIT`. A query may then return fewer rows than both
the `LIMIT` and the number of live matches, and where expired rows
outnumber live ones it may return only a small fraction of them.

Use [per-row TTL](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-ttl) instead. It stores an absolute expiration
time in a dedicated column, and ScyllaDB deletes the row explicitly when
that time passes, which does emit a CDC event and does remove the entry
from the index. See [Cell-Level TTL Is Not Supported](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-ttl-cell-level).

<a id="fts-hybrid"></a>

## Combining Full Text and Vector Search

Full text search finds the rows that contain the words of a query; vector
search finds the rows whose meaning is closest to it. Hybrid search ranks rows
by both at once, which usually returns more relevant results than either
search alone — a row that matches the exact words *and* is semantically close
rises to the top. It is a common retrieval step for retrieval-augmented
generation (RAG) and AI-powered search.

A table can carry both a full-text index and a vector index:

```cql
CREATE TABLE blog.docs (
  id int PRIMARY KEY,
  body text,
  embedding vector<float, 3>
);

CREATE CUSTOM INDEX docs_body_fts ON blog.docs(body) USING 'fulltext_index';
CREATE CUSTOM INDEX docs_embedding_ann ON blog.docs(embedding) USING 'vector_index';

INSERT INTO blog.docs (id, body, embedding) VALUES (1, 'A distributed database for real-time workloads', [0.9, 0.1, 0.0]);
INSERT INTO blog.docs (id, body, embedding) VALUES (2, 'Apples, pears and other fruit', [0.0, 1.0, 0.1]);
INSERT INTO blog.docs (id, body, embedding) VALUES (3, 'Storing fruit prices in a database', [0.5, 0.6, 0.0]);
INSERT INTO blog.docs (id, body, embedding) VALUES (4, 'Vector search finds similar items', [1.0, 0.0, 0.2]);
INSERT INTO blog.docs (id, body, embedding) VALUES (5, 'Nothing relevant here', [-0.1, 0.0, 1.0]);
```

<a id="fts-hybrid-query"></a>

### Hybrid Search Queries

A hybrid query names each search in `ORDER BY` and fuses their results with
`RRF()`, Reciprocal Rank Fusion. The full text search is written as
`BM25(<column>, '<query>')`, and the vector search as
`ANN(<column>, <vector>)` — the function form of `<column> ANN OF <vector>`:

```cql
SELECT id, body FROM blog.docs
  ORDER BY RRF(ANN(embedding, [1.0, 0.0, 0.0]), BM25(body, 'database'))
  LIMIT 10;
```

```none
 id | body
----+------------------------------------------------
  1 | A distributed database for real-time workloads
  3 |             Storing fruit prices in a database
  4 |              Vector search finds similar items
  2 |                  Apples, pears and other fruit
  5 |                          Nothing relevant here
```

Each search runs on its own index and ranks its matches. `RRF()` then gives
every row `1 / (60 + rank)` for each search that returned it, adds these up,
and orders the rows by the sum, highest first. Only ranks are used, so the two
searches’ scores — a BM25 score and a similarity — never have to be comparable.
A row that ranks well in both searches beats one that ranks first in only one
of them, which is why rows 1 and 3, the only rows that mention *database*,
come before row 4, the second-closest vector.

The scoring functions of both searches can be selected to show how each search
saw a row — `ANN()`, `ANN_SCORE()` and `ANN_RANK()` for the vector search,
`BM25()`, `BM25_SCORE()` and `BM25_RANK()` for the full text search, and
`BM25_HIGHLIGHT()`. Each must use the same column and query value as its
search in `ORDER BY`, and returns `null` for a row that search did not
return:

```cql
SELECT id,
       ANN_RANK(embedding, [1.0, 0.0, 0.0]) AS vector_rank,
       BM25_RANK(body, 'database') AS text_rank,
       BM25_HIGHLIGHT(body, 'database') AS excerpt
  FROM blog.docs
  ORDER BY RRF(ANN(embedding, [1.0, 0.0, 0.0]), BM25(body, 'database'))
  LIMIT 10;
```

```none
 id | vector_rank | text_rank | excerpt
----+-------------+-----------+-------------------------------------------------------
  1 |           1 |         2 | A distributed <b>database</b> for real-time workloads
  3 |           3 |         1 |             Storing fruit prices in a <b>database</b>
  4 |           2 |      null |                                                  null
  2 |           4 |      null |                                                  null
  5 |           5 |      null |                                                  null
```

`RRF()` takes two or more searches. They need not be of different kinds: a
search is identified by its kind and its column, so
`RRF(BM25(title, 'database'), BM25(body, 'database'))` fuses full text
searches on two columns, each with its own full-text index. Two calls on the
same column are the same search and must use the same query value.

ScyllaDB can also fuse searches with a user-defined function in place of
`RRF()`, but user-defined functions are not enabled on ScyllaDB Cloud
clusters, so `RRF()` is the fusion function to use.

<a id="fts-hybrid-limitations"></a>

### Hybrid Search Limitations

* A hybrid query takes no `WHERE` clause — not even a `BM25() > 0`
  restriction. It is rejected with `A query running several searches does
  not support a WHERE clause`.
* `LIMIT` is required and must not exceed 1000 (`Hybrid search queries must
  have a limit specified`, `Hybrid search queries require a LIMIT that is not
  greater than 1000`). Each search contributes at most `LIMIT` rows to the
  fusion. A vector index with `oversampling` is still asked for `LIMIT`
  times the oversampling factor, but its answer is cut to `LIMIT` before the
  fusion.
* A quantized vector index with `'rescoring': 'true'` cannot take part in a
  hybrid query, because rescoring discards the ranks that the fusion needs:
  `On a rescoring vector index, ORDER BY supports only ANN() or ANN_SCORE()
  called directly`. See [Quantization and Rescoring](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-quantization.md#vs-quantization).
* Results are not paged: the whole result comes back in one response, with the
  warning `Paging is not supported for Hybrid search queries` when the
  driver’s page size is smaller than `LIMIT`. `PER PARTITION LIMIT` is not
  supported.
* As in a full text search query, a rank is the position in that search’s
  answer. A row that the index returns but that no longer exists in the table
  is left out, and the ranks after it are not renumbered.

On a cluster whose ScyllaDB version does not support hybrid queries yet (see
[Feature Compatibility Matrix](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-clusters.md#vs-feature-compatibility)), run the full text search and the vector
search as two separate queries and fuse the two result lists in your
application with the same `1 / (60 + rank)` formula.

<a id="fts-drivers"></a>

## Using Full Text Search from a Driver

Full text search needs no driver-side support beyond ordinary CQL. Prepared
statements accept bind markers for the search term — in the `WHERE` and
`ORDER BY` positions and in any scoring or highlighting function in the
`SELECT` list — and for the `LIMIT`.

The example below assumes an open `session`. Connecting to a ScyllaDB Cloud
cluster requires authentication, TLS, and a datacenter-aware load balancing
policy — see [Driver Examples](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-driver-python-example) for a complete connection
example.

```python
search = session.prepare(
    "SELECT id, "
    "BM25_SCORE(body, ?) AS score, "
    "BM25_HIGHLIGHT(body, ?) AS excerpt "
    "FROM articles "
    "WHERE BM25(body, ?) > 0 "
    "ORDER BY BM25(body, ?) "
    "LIMIT ?"
)


def full_text_search(query, limit=10):
    # The same term has to be bound to every search-term marker; the server
    # rejects the statement if the values differ.
    return session.execute(search, (query, query, query, query, limit))


for row in full_text_search("database AND distributed"):
    print(row.id, round(row.score, 3), row.excerpt)
```

A hybrid query is prepared the same way, with a bind marker for the query
vector as well:

```python
hybrid = session.prepare(
    "SELECT id, body FROM docs "
    "ORDER BY RRF(ANN(embedding, ?), BM25(body, ?)) "
    "LIMIT ?"
)

# query_embedding is the query text embedded with the same model as the
# stored vectors, as a list of floats.
for row in session.execute(hybrid, (query_embedding, "database", 10)):
    print(row.id, row.body)
```

Because the bound value is parsed as a query expression, sanitize end-user
input before passing it in — see [Unsupported Query Syntax](#fts-unsupported-syntax). To search for
the words a user typed without interpreting them as query syntax, keep only
word characters and whitespace, then drop anything the parser would read as an
operator:

```python
import re

# Keep letters, digits and underscore; replace every other character with a
# space. A whitelist is safer here than a list of reserved characters: it
# cannot fall behind the parser, and it cannot leave an unbalanced
# parenthesis or a stray operator behind.
NON_WORD = re.compile(r"[^\w\s]", re.UNICODE)
OPERATORS = {"AND", "OR", "NOT"}


def as_terms(user_input):
    """Reduce free-form input to a list of plain, safe search terms."""
    cleaned = NON_WORD.sub(" ", user_input)
    return [term for term in cleaned.split() if term not in OPERATORS]


terms = as_terms(user_input)
if terms:
    # Whitespace joins the terms with an implicit OR, which keeps the search
    # forgiving; join with " AND " instead when every term must be present.
    for row in full_text_search(" ".join(terms)):
        print(row.id, row.excerpt)
```

Order matters in that function: characters are replaced *before* the operator
check. Stripping first turns `AND)` into `AND`, which the operator filter
then removes — whereas filtering first would leave `AND)` in place, and the
query would fail on the unbalanced parenthesis. Because `\w` is
Unicode-aware, accented and non-Latin words survive intact.

Stripping punctuation can split a word — `don't` becomes the two
terms `don` and `t` — but under the default `standard` analyzer that
costs you nothing on its own, because the indexed text is tokenized the same
way: a row containing *don’t* is indexed under `don` and `t` too, so it
still matches. Note that this does not hold for the `whitespace` analyzer,
which keeps punctuation, so there the indexed term is `don't` and neither
`don` nor `don t` matches it.

The reason to prefer the implicit `OR` for free-form input is that `AND`
requires *every* term to be present, so a single word the user typed that
appears in no document reduces the whole result set to nothing:

```cql
-- 'unicorn' appears in no document, so the AND query matches nothing
-- while the implicit OR still returns the rows that mention a database.
SELECT id FROM blog.articles
  WHERE BM25(body, 'database AND unicorn') > 0
  ORDER BY BM25(body, 'database AND unicorn') LIMIT 10;

SELECT id FROM blog.articles
  WHERE BM25(body, 'database unicorn') > 0
  ORDER BY BM25(body, 'database unicorn') LIMIT 10;
```

Note also that sanitizing changes only the surface form of the words, not
their meaning: a search for *don’t* will not reach text that says *do not*,
because those are different terms. Matching by meaning rather than by word is
what [vector search](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md) is for, and
[Combining Full Text and Vector Search](#fts-hybrid) combines the two.

See [ScyllaDB Drivers](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-drivers) for the list of supported drivers and
connection details.

<a id="fts-drop-index"></a>

## Dropping a Full-Text Index

```cql
DROP INDEX blog.articles_body_fts;
```

Dropping the index releases the memory it used on the indexing nodes and leaves
the base table untouched. Full text search queries against the column then fail
with `No fulltext index found for full-text search query`.

Drop the index before you drop the column it indexes. While the index exists,
`ALTER TABLE ... DROP` on that column is rejected with `Cannot drop column
body from base table blog.articles because it has dependent indexes
(articles_body_fts)`.

<a id="fts-limitations"></a>

## Limitations

* A full-text index can only target a regular `text`, `varchar`, or
  `ascii` column — not a partition key, clustering key, or static column.
* `LIMIT` is mandatory and must not exceed 1000. Results are not paged.
* A full text search query cannot carry any other `WHERE` restriction, so
  it cannot be narrowed to a partition or filtered on another column. A
  [hybrid query](#fts-hybrid) takes no `WHERE` clause at all.
* Only one `BM25()` restriction is allowed per query.
* `SELECT DISTINCT` is accepted but does not remove duplicates: the query
  returns one row for each matching row, so the same partition key can appear
  more than once.
* `ORDER BY BM25()` cannot be combined with `ANN OF` or any other
  ordering. To rank rows by full text and vector search together, use a
  [hybrid query](#fts-hybrid-query).
* A quantized vector index with `'rescoring': 'true'` cannot take part in a
  hybrid query.
* Fuzzy matching (`term~1`) is not available, and prefix matching works only
  on the last word of a quoted phrase (`"theory of rel"*`).
* Stemming is available only through the language analyzers, and the analyzer
  applies to the whole index — it cannot be chosen per query.
* Index options cannot be changed after creation — there is no
  `ALTER INDEX` statement. Drop the index and create it again instead.
* Cell-level TTL (`USING TTL`, `default_time_to_live`) is not supported:
  expired rows leave stale entries in the index. Use
  [per-row TTL](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md#vs-ttl) instead.

<a id="fts-next-steps"></a>

## Next Steps

* [Working with Vector and Text Search](https://cloud.docs.scylladb.com/stable/vector-search/work-with-vector-search.md)
  — the vector search half of the feature.
* [Full Text Search CQL Reference](https://cloud.docs.scylladb.com/stable/vector-search/reference-vector-search.md#fts-cql-reference) — the statements and index options on one page.
* [Troubleshooting](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-troubleshooting.md) — common
  full text search problems.
* [FAQ](https://cloud.docs.scylladb.com/stable/vector-search/vector-search-faq.md)
