C
C#3y ago
Alerin

Search for text and sort by most hits.

I wrote a function like this, it looks for words in the database. I would like to make it sort by the most hits. If the article finds 20 words that we entered, it will display in front of the article with, for example, 10 hits.
public async Task<List<Article>> ByTextAsync(List<string> search, Func<IQueryable<Models.Article>, IQueryable<Models.Article>>? parameter = null)
{
var query = _context.Article
.Include(x => x.Details)
.AsNoTracking();

if (parameter is not null)
query = parameter(query);

var articles = await query
.AsAsyncEnumerable()
.Where(x =>
search.Any(term =>
x.Details.Content.ToLower().Contains(term.ToLower()) ||
x.Details.Title.ToLower().Contains(term.ToLower())
)
)
.Select(async x => new
{
Article = await _article.GetAsync(x.Id),
})
.Select(x => x.Result.Article)
.ToListAsync();

return articles;
}
public async Task<List<Article>> ByTextAsync(List<string> search, Func<IQueryable<Models.Article>, IQueryable<Models.Article>>? parameter = null)
{
var query = _context.Article
.Include(x => x.Details)
.AsNoTracking();

if (parameter is not null)
query = parameter(query);

var articles = await query
.AsAsyncEnumerable()
.Where(x =>
search.Any(term =>
x.Details.Content.ToLower().Contains(term.ToLower()) ||
x.Details.Title.ToLower().Contains(term.ToLower())
)
)
.Select(async x => new
{
Article = await _article.GetAsync(x.Id),
})
.Select(x => x.Result.Article)
.ToListAsync();

return articles;
}
Anyone have an idea how to do this?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server