Skip to content

Commit

Permalink
feat: added withHighlight parameter to BaseSearchProvider
Browse files Browse the repository at this point in the history
Refs: SITKO-CORE-T-24
  • Loading branch information
IgorAlymov committed Aug 8, 2024
1 parent 4b4eecd commit cd28c45
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Sitko.Core.Search.ElasticSearch/ElasticSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task<long> CountAsync(string indexName, string term, CancellationTo
return resultsCount.Count;
}

public async Task<TSearchModel[]> SearchAsync(string indexName, string term, int limit, SearchType searchType,
public async Task<TSearchModel[]> SearchAsync(string indexName, string term, int limit, SearchType searchType, bool withHighlight = false,
CancellationToken cancellationToken = default)
{
indexName = $"{Options.Prefix}_{indexName}";
Expand Down
27 changes: 18 additions & 9 deletions src/Sitko.Core.Search.OpenSearch/OpenSearchSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public async Task<long> CountAsync(string indexName, string term, CancellationTo
}

public async Task<TSearchModel[]> SearchAsync(string indexName, string term, int limit,
SearchType searchType, CancellationToken cancellationToken = default)
SearchType searchType, bool withHighlight = false, CancellationToken cancellationToken = default)
{
indexName = $"{Options.Prefix}_{indexName}";
var searchResponse = await GetClient()
Expand All @@ -111,15 +111,24 @@ public async Task<TSearchModel[]> SearchAsync(string indexName, string term, int
}

var result = searchResponse.Hits.Select(h =>
new TSearchModel
{
Id = h.Source.Id,
Content = h.Source.Content,
Date = h.Source.Date,
Title = h.Source.Title,
Url = h.Source.Url,
Highlight = h.Highlight
}).ToArray();
var searchModel = new TSearchModel
{
Id = h.Source.Id,
Content = h.Source.Content,
Date = h.Source.Date,
Title = h.Source.Title,
Url = h.Source.Url
};
if (withHighlight)
{
searchModel.Highlight = h.Highlight;
}
return searchModel;
}
).ToArray();
return result;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Sitko.Core.Search/BaseSearchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public Task<long> CountAsync(string term, CancellationToken cancellationToken =
public Task InitAsync(CancellationToken cancellationToken = default) =>
searcher.InitAsync(IndexName, cancellationToken);

public async Task<(T entity, TSearchModel searchResult)[]> SearchAsync(string term, int limit, SearchType searchType, CancellationToken cancellationToken = default)
public async Task<(T entity, TSearchModel searchResult)[]> SearchAsync(string term, int limit, SearchType searchType, bool withHighlight = false, CancellationToken cancellationToken = default)
{
var result = await searcher.SearchAsync(IndexName, term, limit, searchType, cancellationToken);
var result = await searcher.SearchAsync(IndexName, term, limit, searchType,withHighlight, cancellationToken);
return await LoadEntities(result, cancellationToken);
}

public async Task<TEntityPk[]> GetIdsAsync(string term, int limit, SearchType searchType,
public async Task<TEntityPk[]> GetIdsAsync(string term, int limit, SearchType searchType, bool withHighlight = false,
CancellationToken cancellationToken = default)
{
var result = await searcher.SearchAsync(IndexName, term, limit, searchType, cancellationToken);
var result = await searcher.SearchAsync(IndexName, term, limit, searchType, withHighlight, cancellationToken);
return result.Select(m => ParseId(m.Id)).ToArray();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Sitko.Core.Search/ISearchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Task<TEntityPk[]> GetSimilarIdsAsync(string id, int limit,
public interface ISearchProvider<TEntity, TEntityPk, TSearchModel> : ISearchProvider<TEntity>
where TEntity : class where TSearchModel : BaseSearchModel
{
Task<(TEntity entity, TSearchModel searchResult)[]> SearchAsync(string term, int limit, SearchType searchType,
Task<(TEntity entity, TSearchModel searchResult)[]> SearchAsync(string term, int limit, SearchType searchType, bool withHighlight = false,
CancellationToken cancellationToken = default);

Task<TEntityPk[]> GetIdsAsync(string term, int limit, SearchType searchType,
Task<TEntityPk[]> GetIdsAsync(string term, int limit, SearchType searchType, bool withHighlight = false,
CancellationToken cancellationToken = default);

Task<(TEntity entity, TSearchModel searchResult)[]> GetSimilarAsync(string id, int limit,
Expand Down
2 changes: 1 addition & 1 deletion src/Sitko.Core.Search/ISearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Task<bool> DeleteAsync(string indexName, IEnumerable<T> searchModels,

Task<bool> DeleteAsync(string indexName, CancellationToken cancellationToken = default);
Task<long> CountAsync(string indexName, string term, CancellationToken cancellationToken = default);
Task<T[]> SearchAsync(string indexName, string term, int limit, SearchType searchType, CancellationToken cancellationToken = default);
Task<T[]> SearchAsync(string indexName, string term, int limit, SearchType searchType, bool withHighlight = false, CancellationToken cancellationToken = default);

Task<T[]> GetSimilarAsync(string indexName, string id, int limit,
CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task HighlightingTestAsync()
await searchProvider.AddOrUpdateEntitiesAsync(provider.Models.ToArray());
await Task.Delay(TimeSpan.FromSeconds(5));

var result = await searchProvider.SearchAsync("играют", 10, SearchType.Morphology);
var result = await searchProvider.SearchAsync("играют", 10, SearchType.Morphology, true);
result.Length.Should().Be(1);
result.First().searchResult.Highlight.Count.Should().Be(1);
result.First().searchResult.Highlight.First().Value.Contains("<span class='highlight'>");
Expand Down

0 comments on commit cd28c45

Please sign in to comment.