LiteCore
Couchbase Lite cross-platform core implementation
Loading...
Searching...
No Matches
Indexes

Data Structures

struct  C4IndexOptions
 Options for indexes; these each apply to specific types of indexes. More...
 

Enumerations

enum  C4IndexType : uint32_t {
  kC4ValueIndex , kC4FullTextIndex , kC4ArrayIndex , kC4PredictiveIndex ,
  kC4VectorIndex
}
 

Functions

NODISCARD CBL_CORE_API bool c4coll_createIndex (C4Collection *collection, C4String name, C4String indexSpec, C4QueryLanguage queryLanguage, C4IndexType indexType, const C4IndexOptions *indexOptions, C4Error *outError)
 Creates a collection index, of the values of specific expressions across all documents.
 
CBL_CORE_API C4Indexc4coll_getIndex (C4Collection *collection, C4String name, C4Error *outError)
 Returns an object representing an existing index.
 
NODISCARD CBL_CORE_API bool c4coll_deleteIndex (C4Collection *collection, C4String name, C4Error *outError)
 Deletes an index that was created by c4coll_createIndex.
 
CBL_CORE_API C4SliceResult c4coll_getIndexesInfo (C4Collection *collection, C4Error *outError)
 Returns information about all indexes in the collection.
 
CBL_CORE_API C4SliceResult c4db_getIndexesInfo (C4Database *database, C4Error *outError)
 Returns information about all indexes in the database.
 
CBL_CORE_API C4Slice c4index_getName (C4Index *index)
 Returns the name of this index.
 
CBL_CORE_API C4Collectionc4index_getCollection (C4Index *index)
 Returns the collection this index belongs to.
 
CBL_CORE_API C4IndexType c4index_getType (C4Index *)
 Returns the index's type.
 
CBL_CORE_API C4QueryLanguage c4index_getQueryLanguage (C4Index *)
 Returns the index's query language (JSON or N1QL).
 
CBL_CORE_API C4String c4index_getExpression (C4Index *)
 Returns the indexed expression.
 
CBL_CORE_API bool c4index_getOptions (C4Index *index, C4IndexOptions *outOpts)
 Gets the index's FTS/vector options, if any.
 
NODISCARD CBL_CORE_API bool c4db_createIndex (C4Database *database, C4String name, C4String indexSpecJSON, C4IndexType indexType, const C4IndexOptions *indexOptions, C4Error *outError)
 
NODISCARD CBL_CORE_API bool c4db_createIndex2 (C4Database *database, C4String name, C4String indexSpec, C4QueryLanguage queryLanguage, C4IndexType indexType, const C4IndexOptions *indexOptions, C4Error *outError)
 
NODISCARD CBL_CORE_API bool c4db_deleteIndex (C4Database *database, C4String name, C4Error *outError)
 
CBL_CORE_API bool c4coll_isIndexTrained (C4Collection *collection, C4String name, C4Error *outError)
 Returns whether a vector index has been trained yet or not.
 

Detailed Description

Enumeration Type Documentation

◆ C4IndexType

enum C4IndexType : uint32_t
Enumerator
kC4ValueIndex 

Regular index of property value.

kC4FullTextIndex 

Full-text index.

kC4ArrayIndex 

Index of array values, for use with UNNEST.

kC4PredictiveIndex 

Index of prediction() results (Enterprise Edition only)

kC4VectorIndex 

Index of ML vector similarity (Enterprise Edition only)

Function Documentation

◆ c4coll_createIndex()

NODISCARD CBL_CORE_API bool c4coll_createIndex ( C4Collection * collection,
C4String name,
C4String indexSpec,
C4QueryLanguage queryLanguage,
C4IndexType indexType,
const C4IndexOptions * indexOptions,
C4Error * outError )

Creates a collection index, of the values of specific expressions across all documents.

The name is used to identify the index for later updating or deletion; if an index with the same name already exists, it will be replaced unless it has the exact same expressions.

Currently five types of indexes are supported:

Value indexes speed up queries by making it possible to look up property (or expression) values without scanning every document. They're just like regular indexes in SQL or N1QL. Multiple expressions are supported; the first is the primary key, second is secondary. Expressions must evaluate to scalar types (boolean, number, string). Full-Text Search (FTS) indexes enable fast search of natural-language words or phrases by using the MATCH operator in a query. A FTS index is required for full-text search: a query with a MATCH operator will fail to compile unless there is already a FTS index for the property/expression being matched. Only a single expression is currently allowed, and it must evaluate to a string. Array indexes optimize UNNEST queries, by materializing an unnested array property (across all documents) as a table in the SQLite database, and creating a SQL index on it. Predictive indexes optimize queries that use the PREDICTION() function, by materializing the function's results as a table and creating a SQL index on a result property. Vector indexes store high-dimensional vectors/embeddings and support efficient Approximate Nearest Neighbor (ANN) queries for finding the nearest vectors to a query vector.

Note: If some documents are missing the values to be indexed, those documents will just be omitted from the index. It's not an error.

In an array index, the first expression must evaluate to an array to be unnested; it's usually a property path but could be some other expression type. If the array items are nonscalar (dictionaries or arrays), you should add a second expression defining the sub- property (or computed value) to index, relative to the array item.

In a predictive index, the expression is a PREDICTION() call in JSON query syntax, including the optional 3rd parameter that gives the result property to extract (and index.)

The indexSpec argument is an expression, relative to a document, that describes what to index. It can be in either the JSON query schema, or in N1QL syntax. It usually names a property, but may also be a computed value based on properties.

Note
The caller must use a lock for Database when this function is called.
Parameters
collectionThe collection to index.
nameThe name of the index. Any existing index with the same name will be replaced, unless it has the identical expressions (in which case this is a no-op.)
indexSpecThe definition of the index in JSON or N1QL form. (See above.)
queryLanguageThe language of indexSpec, either JSON or N1QL.
indexTypeThe type of index (value full-text, etc.)
indexOptionsOptions for the index. If NULL, each option will get a default value.
outErrorOn failure, will be set to the error status.
Returns
True on success, false on failure.

◆ c4coll_deleteIndex()

NODISCARD CBL_CORE_API bool c4coll_deleteIndex ( C4Collection * collection,
C4String name,
C4Error * outError )

Deletes an index that was created by c4coll_createIndex.

Note
The caller must use a lock for Database when this function is called.
Parameters
collectionThe collection to index.
nameThe name of the index to delete
outErrorOn failure, will be set to the error status.
Returns
True on success, false on failure.

◆ c4coll_getIndex()

CBL_CORE_API C4Index * c4coll_getIndex ( C4Collection * collection,
C4String name,
C4Error * outError )

Returns an object representing an existing index.

Note
The caller must use a lock for Database when this function is called.

◆ c4coll_getIndexesInfo()

CBL_CORE_API C4SliceResult c4coll_getIndexesInfo ( C4Collection * collection,
C4Error * outError )

Returns information about all indexes in the collection.

The result is a Fleece-encoded array of dictionaries, one per index. Each dictionary has keys "name", "type" (a C4IndexType), and "expr" (the source expression).

Note
The caller must use a lock for Database when this function is called.
Parameters
collectionThe collection to check
outErrorOn failure, will be set to the error status.
Returns
A Fleece-encoded array of dictionaries, or NULL on failure.

◆ c4coll_isIndexTrained()

CBL_CORE_API bool c4coll_isIndexTrained ( C4Collection * collection,
C4String name,
C4Error * outError )

Returns whether a vector index has been trained yet or not.

If the index doesn't exist, or is not a vector index, then this method will return false with an appropriate error set. Otherwise, in the absence of errors, this method will zero the error and set the return value.

Note
The caller must use a lock for Database when this function is called.
Parameters
collectionThe collection to index.
nameThe name of the index
outErrorOn failure, will be set to the error status.
Returns
True if the index is trained.

◆ c4db_createIndex()

NODISCARD CBL_CORE_API bool c4db_createIndex ( C4Database * database,
C4String name,
C4String indexSpecJSON,
C4IndexType indexType,
const C4IndexOptions * indexOptions,
C4Error * outError )

◆ c4db_createIndex2()

NODISCARD CBL_CORE_API bool c4db_createIndex2 ( C4Database * database,
C4String name,
C4String indexSpec,
C4QueryLanguage queryLanguage,
C4IndexType indexType,
const C4IndexOptions * indexOptions,
C4Error * outError )

◆ c4db_deleteIndex()

NODISCARD CBL_CORE_API bool c4db_deleteIndex ( C4Database * database,
C4String name,
C4Error * outError )

◆ c4db_getIndexesInfo()

CBL_CORE_API C4SliceResult c4db_getIndexesInfo ( C4Database * database,
C4Error * outError )

Returns information about all indexes in the database.

The result is a Fleece-encoded array of dictionaries, one per index. Each dictionary has keys "name", "type" (a C4IndexType), and "expr" (the source expression).

Note
The caller must use a lock for Database when this function is called.
Parameters
databaseThe database to check
outErrorOn failure, will be set to the error status.
Returns
A Fleece-encoded array of dictionaries, or NULL on failure.

◆ c4index_getCollection()

CBL_CORE_API C4Collection * c4index_getCollection ( C4Index * index)

Returns the collection this index belongs to.

Note
This function is thread-safe.

◆ c4index_getExpression()

CBL_CORE_API C4String c4index_getExpression ( C4Index * )

Returns the indexed expression.

Note
This function is thread-safe.

◆ c4index_getName()

CBL_CORE_API C4Slice c4index_getName ( C4Index * index)

Returns the name of this index.

Note
This function is thread-safe.

◆ c4index_getOptions()

CBL_CORE_API bool c4index_getOptions ( C4Index * index,
C4IndexOptions * outOpts )

Gets the index's FTS/vector options, if any.

Note
This function is thread-safe.
Parameters
indexThe index.
outOptsThe options will be written here, if they exist.
Returns
True if there are options, false if not.

◆ c4index_getQueryLanguage()

CBL_CORE_API C4QueryLanguage c4index_getQueryLanguage ( C4Index * )

Returns the index's query language (JSON or N1QL).

Note
This function is thread-safe.

◆ c4index_getType()

CBL_CORE_API C4IndexType c4index_getType ( C4Index * )

Returns the index's type.

Note
This function is thread-safe.