-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DFE-Digital/AW-CL/Refactor-part-word-search
Aw cl/refactor part word search
- Loading branch information
Showing
9 changed files
with
239 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Dfe.Data.Common.Infrastructure.CognitiveSearch/SearchByKeyword/ISearchRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.Options; | ||
|
||
namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword; | ||
|
||
/// <summary> | ||
/// Definition of a provider of search rule that can be used by the search implementation | ||
/// </summary> | ||
public interface ISearchRule | ||
{ | ||
/// <summary> | ||
/// Apply optional search rules set in the <see cref="SearchRuleOptions"/> to the search keyword | ||
/// </summary> | ||
/// <param name="keyword"> | ||
/// the search keyword | ||
/// </param> | ||
/// <returns></returns> | ||
public string ApplySearchRules(string keyword); | ||
} |
12 changes: 0 additions & 12 deletions
12
Dfe.Data.Common.Infrastructure.CognitiveSearch/SearchByKeyword/Options/SearchIndexOptions.cs
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
Dfe.Data.Common.Infrastructure.CognitiveSearch/SearchByKeyword/Options/SearchRuleOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.Options; | ||
|
||
/// <summary> | ||
/// Configuration options used to define how the search should be performed | ||
/// </summary> | ||
public sealed class SearchRuleOptions | ||
{ | ||
/// <summary> | ||
/// The name of the search rule to be used. | ||
/// </summary> | ||
public string? SearchRule { get; set; } | ||
} |
38 changes: 38 additions & 0 deletions
38
...Common.Infrastructure.CognitiveSearch/SearchByKeyword/SearchRules/PartialWordMatchRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.Options; | ||
using System.Text; | ||
|
||
namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.SearchRules; | ||
|
||
/// <summary> | ||
/// Facilitates search rules to be specified when running a search | ||
/// </summary> | ||
public class PartialWordMatchRule : ISearchRule | ||
{ | ||
private SearchRuleOptions _ruleOptions; | ||
|
||
/// <summary> | ||
/// Construct the saerch rules provider, injecting into it the <see cref="SearchRuleOptions"/> to be applied | ||
/// </summary> | ||
/// <param name="searchRuleOptions"></param> | ||
public PartialWordMatchRule(SearchRuleOptions searchRuleOptions) | ||
{ | ||
_ruleOptions = searchRuleOptions; | ||
} | ||
|
||
/// <summary> | ||
/// Apply search rules as specified in the options | ||
/// </summary> | ||
/// <param name="searchKeyword"></param> | ||
/// <returns></returns> | ||
public string ApplySearchRules(string searchKeyword) | ||
{ | ||
if (_ruleOptions.SearchRule == "PartialWordMatch") // only 1 search rule so far | ||
{ | ||
return new StringBuilder(searchKeyword.TrimEnd()) | ||
.Replace(" ", "* ") | ||
.Append('*') | ||
.ToString(); | ||
} | ||
return searchKeyword; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...rastructure.CognitiveSearch/Tests/Filtering/TestDoubles/PartialWordMatchRuleTestDouble.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword; | ||
using Moq; | ||
|
||
namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.TestDoubles; | ||
|
||
public static class PartialWordMatchRuleTestDouble | ||
{ | ||
public static ISearchRule MockFor(string keywordIn, string keywordOut) | ||
{ | ||
var mock = new Mock<ISearchRule>(); | ||
mock.Setup(provider => provider.ApplySearchRules(keywordIn)) | ||
.Returns(keywordOut) | ||
.Verifiable(); | ||
return mock.Object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...frastructure.CognitiveSearch/Tests/SearchByKeyword/Providers/PartialWordMatchRuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.Options; | ||
using Dfe.Data.Common.Infrastructure.CognitiveSearch.SearchByKeyword.SearchRules; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.SearchByKeyword.Providers; | ||
|
||
public class PartialWordMatchRuleTests | ||
{ | ||
[Theory] | ||
[InlineData("searchKeyword", "searchKeyword*")] | ||
[InlineData("searchKeyword ", "searchKeyword*")] | ||
[InlineData("searchTerm1 searchTerm2", "searchTerm1* searchTerm2*")] | ||
void ApplySearchRules_WithPartialWordMatchRuleOption_AppliesRule(string searchInput, string expected) | ||
{ | ||
// arrange | ||
var searchRulesOptions = new SearchRuleOptions() | ||
{ | ||
SearchRule = "PartialWordMatch" | ||
}; | ||
|
||
var provider = new CognitiveSearch.SearchByKeyword.SearchRules.PartialWordMatchRule(searchRulesOptions); | ||
|
||
// act | ||
var searchKeywordResult = provider.ApplySearchRules(searchInput); | ||
|
||
// assert | ||
searchKeywordResult.Should().Be(expected); | ||
searchInput.Should().NotBe(expected); | ||
} | ||
|
||
[Fact] | ||
void ApplySearchRules_WithNotApplicableRuleOption_ReturnsUnmodified() | ||
{ | ||
// arrange | ||
var searchRulesOptions = new SearchRuleOptions() | ||
{ | ||
SearchRule = "a nonexistent rule" | ||
}; | ||
|
||
var provider = new PartialWordMatchRule(searchRulesOptions); | ||
|
||
// act | ||
var searchKeywordResult = provider.ApplySearchRules("searchKeyword"); | ||
|
||
// assert | ||
searchKeywordResult.Should().Be("searchKeyword"); | ||
} | ||
|
||
[Fact] | ||
void ApplySearchRules_WithNoOption_ReturnsUnmodified() | ||
{ | ||
// arrange | ||
var searchRulesOptions = new SearchRuleOptions(); | ||
|
||
var provider = new PartialWordMatchRule(searchRulesOptions); | ||
|
||
// act | ||
var searchKeywordResult = provider.ApplySearchRules("searchKeyword"); | ||
|
||
// assert | ||
searchKeywordResult.Should().Be("searchKeyword"); | ||
} | ||
} |