Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented special character search #165

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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