generated from Kentico/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #53 from Kentico/feat/selectable-analyzer
Feat/selectable analyzer
- Loading branch information
Showing
25 changed files
with
295 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Select Lucene text Analyzer | ||
|
||
In the admin UI you are able to select an `Analyzer` which will be used by selected strategy to rebuild an index. By default the only available `Analyzer` is the `StandardAnalyzer`. In order to use a different analyzer you will have to register it, along with the Lucene, to the application services. | ||
|
||
```csharp | ||
// Program.cs | ||
|
||
// Registers all services and enables custom indexing behavior | ||
services.AddKenticoLucene(builder => | ||
{ | ||
// Register strategies ... | ||
builder.RegisterAnalyzer<CzechAnalyzer>("Czech analyzer"); | ||
}); | ||
``` | ||
Now the `CzechAnalyzer` will be available for selection in the Admin UI under Lucene Analyzer. This analyzer will be used by selected strategy for reindexing. You can retrieve and use this analyzer for index quierying. The analyzer will be available on the instance of the `LuceneIndex` class under `LuceneAnalyzer`. | ||
```csharp | ||
public class SimpleSearchService | ||
{ | ||
// Other class members ... | ||
public LuceneSearchResultModel<DancingGoatSearchResultModel> GlobalSearch( | ||
string indexName, | ||
string? searchText, | ||
int pageSize = 20, | ||
int page = 1) | ||
{ | ||
var index = luceneIndexManager.GetRequiredIndex(indexName); | ||
var query = GetTermQuery(searchText, index); | ||
// ... | ||
} | ||
private Query GetTermQuery(string? searchText, LuceneIndex index) | ||
{ | ||
// Here we retrieve the analyzer instance which we can use for our queries | ||
var analyzer = index.LuceneAnalyzer; | ||
var queryBuilder = new QueryBuilder(analyzer); | ||
var booleanQuery = new BooleanQuery(); | ||
if (!string.IsNullOrWhiteSpace(searchText)) | ||
{ | ||
booleanQuery = AddToTermQuery(booleanQuery, queryBuilder.CreatePhraseQuery(nameof(DancingGoatSearchResultModel.Title), searchText, PHRASE_SLOP), 5); | ||
booleanQuery = AddToTermQuery(booleanQuery, queryBuilder.CreateBooleanQuery(nameof(DancingGoatSearchResultModel.Title), searchText, Occur.SHOULD), 0.5f); | ||
if (booleanQuery.GetClauses().Count() > 0) | ||
{ | ||
return booleanQuery; | ||
} | ||
} | ||
return new MatchAllDocsQuery(); | ||
} | ||
// Other class members ... | ||
} | ||
``` | ||
|
||
The `Analyzer` uses a `LuceneVersion` to match version compatibility accross releases of Lucene. By default we use the latest `LuceneVersion.LUCENE_48`. You can override the version when registering application services as follows. | ||
|
||
```csharp | ||
// Program.cs | ||
|
||
// Registers all services and enables custom indexing behavior | ||
services.AddKenticoLucene(builder => | ||
{ | ||
// Register strategies ... | ||
// Register analyzers | ||
builder.SetAnalyzerLuceneVersion(LuceneVersion.LUCENE_47); | ||
}); | ||
``` |
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
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
Binary file modified
BIN
+2.6 KB
(100%)
images/xperience-administration-search-index-edit-form-paths-edit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
14 changes: 14 additions & 0 deletions
14
src/Kentico.Xperience.Lucene.Admin/Providers/AnalyzerOptionsProvider.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,14 @@ | ||
using Kentico.Xperience.Admin.Base.FormAnnotations; | ||
using Kentico.Xperience.Lucene.Core.Indexing; | ||
|
||
namespace Kentico.Xperience.Lucene.Admin.Providers; | ||
|
||
internal class AnalyzerOptionsProvider : IDropDownOptionsProvider | ||
{ | ||
public Task<IEnumerable<DropDownOptionItem>> GetOptionItems() => | ||
Task.FromResult(AnalyzerStorage.Analyzers.Keys.Select(x => new DropDownOptionItem() | ||
{ | ||
Value = x, | ||
Text = x | ||
})); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/Kentico.Xperience.Lucene.Core/Indexing/AnalyzerStorage.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,25 @@ | ||
using Lucene.Net.Analysis; | ||
using Lucene.Net.Analysis.Standard; | ||
using Lucene.Net.Util; | ||
|
||
namespace Kentico.Xperience.Lucene.Core.Indexing; | ||
|
||
internal static class AnalyzerStorage | ||
{ | ||
public static Dictionary<string, Type> Analyzers { get; private set; } | ||
public static LuceneVersion AnalyzerLuceneVersion { get; private set; } | ||
static AnalyzerStorage() => Analyzers = []; | ||
|
||
|
||
public static void SetAnalyzerLuceneVersion(LuceneVersion matchVersion) => AnalyzerLuceneVersion = matchVersion; | ||
|
||
|
||
public static void AddAnalyzer<TAnalyzer>(string analyzerName) where TAnalyzer : Analyzer | ||
=> Analyzers.Add(analyzerName, typeof(TAnalyzer)); | ||
|
||
|
||
public static Type GetOrDefault(string analyzerName) => | ||
Analyzers.TryGetValue(analyzerName, out var type) | ||
? type | ||
: typeof(StandardAnalyzer); | ||
} |
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
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
Oops, something went wrong.