SoftadminApi.AiSearchIndex_Search

Not available in Cloud edition.
Type: Stored procedure

Searches the HNSW index, full-text index, or both and returns relevant chunks (see AI Search). It requires one or both of the following tables, which the procedure will populate with rows.

CREATE TABLE #SearchResultChunk
(
	SchemaName sysname NOT NULL,
	TableName sysname NOT NULL,
	RowId bigint NOT NULL,
	ChunkText nvarchar(MAX) NOT NULL,
	Score float NOT NULL,
	Rank int NOT NULL,
	CosineDistance float NULL,
	FullTextRank int NULL
);

CREATE TABLE #SearchResultEntry
(
	SchemaName sysname NOT NULL,
	TableName sysname NOT NULL,
	RowId bigint NOT NULL,
	Score float NOT NULL,
	Rank int NOT NULL,
	CosineDistance float NULL,
	FullTextRank int NULL
);

SchemaName

Schema name of the chunk/entry.

TableName

Table name of the chunk/entry.

RowId

Row ID of the chunk/entry.

ChunkText

The chunk text found, prefixed by its entry's chunk prefix.

Score

How relevant the found chunk/entry is. Higher score is better. Can be used to filter the result further.

Rank

The rank of each chunk/entry from the search. Useful when merging this search result with another search result. For example, using reciprocal rank fusion.

CosineDistance

The cosine distance between the query vector and the chunk embedding. The distance is a measure of how good the semantic match is. A lower distance is better. The semantic search will always return some chunks, even if it is a bad match. This column can be used to filter out bad matches.

FullTextRank

The rank returned by the full-text index search. It is a measure of how good the full-text index match is. A higher rank is better. Can be used to filter out search results further. See CONTAINSTABLE for more details.

Parameters

@SearchPhrase<N> optional nvarchar(500)
Search phrases to use in the full-text index search. A full-text index search is only performed if at least one is specified. <N> must be an integer in the range 1-5 (inclusive).
@SearchPhrase<N>Embedding optional varbinary
Binary embedding to search for in the HNSW index. The embedding needs to be generated with the same model used by the AI Search and must be 384 dimensions long. An HNSW index search is only performed if at least one is specified. <N> must be an integer in the range 1-5 (inclusive).
@IndexName mandatory nvarchar(300)
Name of the HNSW index to search.
@TopN mandatory int
Number of results (chunks) to return.
@UserId mandatory int
UserId who performs the search.
@DatetimeFrom optional datetime
If specified, the search will only return chunks with a timestamp after this date.
@DatetimeTo optional datetime
If specified, the search will only return chunks with a timestamp up to this date.
@EfSearch optional int
Overrides the number of potential candidates the HNSW index will consider when searching. A higher number increases the likelihood to find the right result, but increases the search time. If unspecified, it is set to 4 * TopN.
@PreferOlder optional bit
If specified, prioritizes newly added entries. Can not be used together with @PreferRecent.
@PreferRecent optional bit
If specified, prioritizes newly added entries. Can not be used together with @PreferOlder.
@SearchKey optional nvarchar(100)
Used together with SearchValue. If both are specified, the search will only return entries that has a matching registered (key, value) pair.
@SearchValue optional nvarchar(1000)
See SearchKey.