Skip to content

Commit

Permalink
feat: implementated special character seach (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashukequinor authored Oct 30, 2024
1 parent b837b00 commit e53a3cc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Oplog.Core/AzureSearch/SearchOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure.Search.Documents;
using Oplog.Core.Utils;
using System.Text;
using System.Text.RegularExpressions;

namespace Oplog.Core.AzureSearch;

Expand Down Expand Up @@ -64,7 +65,7 @@ public void AddSearchTextFilter(string searchText)
string[] keywords = searchText.Split(' ');
if (keywords.Length == 1)
{
_fieldsFilter.Append(@$" and (search.ismatch('//{searchText.ToLower()}*//', 'Text','full','any'))");
_fieldsFilter.Append(@$" and (search.ismatch('{SearchOptionsBuilder.EscapeSpecialCharacters(searchText).ToLower()}', 'Text','full','any'))");
}
else
{
Expand All @@ -76,7 +77,7 @@ public void AddSearchTextFilter(string searchText)
{
continue;
}
query += @$"search.ismatch('//{keyword.ToLower()}*//', 'Text','full','any') or ";
query += @$"search.ismatch('{SearchOptionsBuilder.EscapeSpecialCharacters(keyword).ToLower()}', 'Text','full','any') or ";
}

//Remove the "or" logical operator
Expand All @@ -86,6 +87,14 @@ public void AddSearchTextFilter(string searchText)
}
}

// Function to escape special characters
public static string EscapeSpecialCharacters(string input)
{
string escapedPattern = Regex.Replace(input, @"([!@#%^&*()_+\-=\[\]{}|;:"",\\./<>?~\\])", @"\$1");
string regexPattern = $"/.*{escapedPattern.Replace("'", "''")}.*/";
return regexPattern;
}

public void AddLogTypeFilter(int[] logTypeIds)
{
if (logTypeIds == null || logTypeIds.Length == 0) return;
Expand Down

0 comments on commit e53a3cc

Please sign in to comment.