Browse Source

Update POST.md

pull/19585/head
Engincan VESKE 2 years ago
parent
commit
34e37f9c5e
  1. 23
      docs/en/Community-Articles/2024-04-19-Performing-Case-Insensitive-Search-In-Postgresql/POST.md

23
docs/en/Community-Articles/2024-04-19-Performing-Case-Insensitive-Search-In-Postgresql/POST.md

@ -1,16 +1,23 @@
# Performing Case-Insensitive Search in PostgreSQL: Using `citext` and Collation # Performing Case-Insensitive Search in ABP Based-PostgreSQL Application: Using `citext` and Collation
PostgreSQL, by default, is a case-sensitive database. This means that text data stored in the database is treated as case-sensitive. However, in many cases, users may need to perform searches regardless of case sensitivity. PostgreSQL, by default, is a case-sensitive database. This means that text data stored in the database is treated as case-sensitive. However, in many cases, users may need to perform searches regardless of case sensitivity. Since PostgreSQL is case-sensitive this creates some questions in the mind, while when selecting and and using it within an ABP-based application. For example:
In this article, I will address two possible solutions to perform case-insensitive operations: **Using the `citext` data type for text fields** and **using collations**. * Does not ABP Framework support case-sensitive queries for the PostgreSQL database?
* ABP Framework supports PostgreSQL but does not support case-insensitive search for it?
* [But PostgreSQL still a challenge to work with Accent and Case Insensitive Searches with ABP](https://twitter.com/iSephit/status/1780568810291913029)
* ...
None of these questions are related to ABP Framework but PostgreSQL is being case-sensitive database by default and in this article, I will try to answer to these questions and I will address two possible solutions to perform case-insensitive operations: **Using the `citext` data type for text fields** and **using collations**.
> As you would know, ABP Framework provides a [EF Core PostgreSQL Provider package](https://docs.abp.io/en/abp/latest/Entity-Framework-Core-PostgreSQL) called `Volo.Abp.EntityFrameworkCore.PostgreSql`. Throughout this article, I will give the example codes, by assuming that you created an ABP-based application with PostgreSQL as the database option, however all the steps in this article, also applicable to any .NET based application.
## Using the `citext` Data Type ## Using the `citext` Data Type
`citext` is a PostgreSQL-specific data type that stands for "case-insensitive text". This data type stores text data while ignoring case differences, effectively making searches case-insensitive. `citext` is a PostgreSQL-specific data type that stands for "case-insensitive text". This data type stores text data while ignoring case differences, effectively making searches case-insensitive. When you create an ABP-based application with PostgreSQL as the database option, you can easily use the `citext` data type.
To use the `citext` data type, you mainly need to do two things: To use the `citext` data type, you mainly need to do two things:
1. The `citext` data type is available in a PostgreSQL-bundled extension, so you'll first have to install it. For that purpose, you should use the `HasPostgresExtension` method, 1. The `citext` data type is available in a PostgreSQL-bundled extension, so you'll first have to install it. For that purpose, you should use the [`HasPostgresExtension`](https://www.npgsql.org/efcore/api/Microsoft.EntityFrameworkCore.NpgsqlModelBuilderExtensions.html) method,
2. Then, you should map all of your text fields to the `citext` datatype in your `*DbContext.cs` file as follows: 2. Then, you should map all of your text fields to the `citext` datatype in your `*DbContext.cs` file as follows:
```csharp ```csharp
@ -58,7 +65,7 @@ In addition to that, you should configure the `AbpDbContextOptions` in the modul
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
//other configurations //other configurations...
Configure<AbpDbContextOptions>(options => Configure<AbpDbContextOptions>(options =>
{ {
@ -81,7 +88,7 @@ After you make these changes, you can create a new migration and apply it to you
## Using Collations ## Using Collations
Collation is a set of rules that determine how text data is sorted and compared in a dataset. PostgreSQL provides different collation settings for various languages and cultures. These settings can determine how text data is compared and can be configured to ignore case differences. **Collation** is a set of rules that determine how text data is sorted and compared in a dataset. PostgreSQL provides different collation settings for various languages and cultures. These settings can determine how text data is compared and can be configured to ignore case differences.
To perform case-insensitive or accent-insensitive operations, you should choose one of the [non-deterministic collations](https://postgresql.verite.pro/blog/2019/10/14/nondeterministic-collations.html). For example, you can define a collation as follows (in your `*DbContext.cs` file): To perform case-insensitive or accent-insensitive operations, you should choose one of the [non-deterministic collations](https://postgresql.verite.pro/blog/2019/10/14/nondeterministic-collations.html). For example, you can define a collation as follows (in your `*DbContext.cs` file):
@ -115,7 +122,7 @@ However, this solution comes with some problems, for example, by using non-deter
## Conclusion ## Conclusion
In PostgreSQL, you can perform case-insensitive searches by using the citext data type or by utilizing collation settings. To make a decision to pick one of these options, you can follow the following points: In PostgreSQL, you can perform case-insensitive searches by using the citext data type or by utilizing collation settings. Nevertheless, if you have an ABP-based PostgreSQL application or a plain .NET application with PostgreSQL as the database option, to make a decision to pick one of these options, you can follow the following points:
* If the accent is not important for you and the only thing you want to do, is make the PostgreSQL queries case-insensitive, using the `citext` data type option should be selected * If the accent is not important for you and the only thing you want to do, is make the PostgreSQL queries case-insensitive, using the `citext` data type option should be selected
* If the accent is really important to you, and you don't use LINQ methods (such as `StartsWith` and `EndsWith` methods), you can use collations. * If the accent is really important to you, and you don't use LINQ methods (such as `StartsWith` and `EndsWith` methods), you can use collations.

Loading…
Cancel
Save