# Sql Server Full Text Search

## Description

SQL Server full-text search can be used as the backing implementation for Flex search operations. Your application code should depend on `IFlexSearchStore`, while Flex provides the provider bridge and wiring.

## Important concepts

* **`IFlexSearchStore` is the contract**: app code performs searches and indexing through the shared interface.
* **Table + column configuration**: SQL Server full-text search requires a target table, an ID column, and searchable columns.
* **Provider bridge**: the SQL Server implementation is exposed via an `IFlexSearchStoreBridge` internally, but most consumers only need `IFlexSearchStore`.

## Configuration in DI

Add the provider in your DI composition root (commonly in `EndPoints/...CommonConfigs/OtherApplicationServicesConfig.cs` or wherever you centralize registrations).

```csharp
// using Sumeru.Flex; // IFlexSearchStore

public static class OtherApplicationServicesConfig
{
	public static IServiceCollection AddOtherApplicationServices(
		this IServiceCollection services,
		IConfiguration configuration)
	{
		// Registers SQL Server full-text search as the IFlexSearchStore bridge.
		// Flex auto-wires generated Queries/Plugins that *use* IFlexSearchStore.
		services.AddFlexSqlServerSearchStore(configuration);

		return services;
	}
}
```

## appsettings.json

Configuration is read from `FlexBase:DataStores:Search:SqlServer`.

```json
{
  "FlexBase": {
	"DataStores": {
	  "Search": {
		"SqlServer": {
		  "ConnectionString": "Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;",
		  "TableName": "dbo.Articles",
		  "IdColumn": "Id",
		  "SearchableColumns": [ "Title", "Body" ],
		  "FullTextCatalog": "FlexSearchCatalog",
		  "Language": "English"
		}
	  }
	}
  }
}
```

## Examples (template-based)

These examples mirror the generated Query and PostBus plugin templates. You do **not** register these types manually—Flex discovers and wires generated Queries/Handlers/Plugins automatically.

### Search a single document (Query)

```csharp
using Microsoft.Extensions.Logging;
using Sumeru.Flex;
using System.Linq;
using System.Threading.Tasks;

namespace {YourApplication}.Queries.Search;

public class SearchPoliciesGetSingle : FlexiQueryBridgeAsync<PolicyDto>
{
	protected readonly ILogger<SearchPoliciesGetSingle> _logger;
	protected readonly IFlexHost _flexHost;
	protected readonly IFlexSearchStore _searchStore;
	protected SearchPoliciesGetSingleParams _params;
	protected FlexAppContextBridge _flexAppContext;

	public SearchPoliciesGetSingle(ILogger<SearchPoliciesGetSingle> logger, IFlexHost flexHost, IFlexSearchStore searchStore)
	{
		_logger = logger;
		_flexHost = flexHost;
		_searchStore = searchStore;
	}

	public virtual SearchPoliciesGetSingle AssignParameters(SearchPoliciesGetSingleParams @params)
	{
		_params = @params;
		return this;
	}

	public virtual async Task<PolicyDto?> Fetch()
	{
		_flexAppContext = _params.GetAppContext();

		var results = await _searchStore.SearchAsync<PolicySearchDocument>(
			_params.Query,
			take: 1);

		var first = results.FirstOrDefault();
		// TODO: map PolicySearchDocument -> PolicyDto
		return first as PolicyDto;
	}
}

public class SearchPoliciesGetSingleParams : DtoBridge
{
	public string Query { get; set; }
}

public class PolicySearchDocument
{
	public string Id { get; set; }
}
```

## SQL Server considerations

* Ensure SQL Server Full-Text Search is installed/enabled for the instance.
* The configured database identity must be able to query the full-text catalog and underlying table(s).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flexbase.in/data-and-providers/data-stores/search-store-full-text/sql-server-full-text-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
