C
C#2y ago
Alerin

A ready-made ef core data search solution

Hi, I need to do a database content search engine. I would like the search engine to look for a word not a sentence. There is something ready? Currently I have contains but it searches for the entire string. I'd like to do it on a word board basis
37 Replies
Alerin
Alerin2y ago
my example code:
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()
.Where(x =>
x.Details.Content.Contains(search) ||
x.Details.Title.Contains(search)
);

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

var articles = await query
.AsAsyncEnumerable()
.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()
.Where(x =>
x.Details.Content.Contains(search) ||
x.Details.Title.Contains(search)
);

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

var articles = await query
.AsAsyncEnumerable()
.Select(async x => new
{
Article = await _article.GetAsync(x.Id),
})
.Select(x => x.Result.Article)
.ToListAsync();

return articles;
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
Did I do something wrong?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
Ok thank you for help 😄
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
I have a problem with Join: Error CS0411 Unable to infer the type arguments for the method 'Queryable.Join <TOuter, TInner, TKey, TResult> (IQueryable <TOuter>, IEnumerable <TInner>, Expression <Func <TOuter, TKey >>, Expression <Func <TInner, TKey >>, Expression <Func <TOuter, TInner, TResult >>) ”based on usage. Try to explicitly specify type arguments.
Alerin
Alerin2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
Alerin
Alerin2y ago
IQueryable<Models.Article>
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
copilot 😄 Error CS1061 "IQueryable <Article>" does not contain a definition of "Join" and an available extension method "Join" that takes the first argument of type "IQueryable <Article>" was not found (are you missing a using directive or an assembly reference?).
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
CS0305 Use generic element method "Queryable.Join <TOuter, TInner, TKey, TResult> (IQueryable <TOuter>, IEnumerable <TInner>, Expression <Func <TOuter, TKey >>, Expression <Func <TInner, TKey >>, Expression <Func <TOuter, TInner, TResult >>) "requires arguments of type" 4 " This is the bug, I've seen a similar solution somewhere on stackoverflow before but can't find it.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
Yes, I have no idea what this is about
[DynamicDependency("Join`4", typeof(Enumerable))]
public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
{
ArgumentNullException.ThrowIfNull(outer);
ArgumentNullException.ThrowIfNull(inner);
ArgumentNullException.ThrowIfNull(outerKeySelector);
ArgumentNullException.ThrowIfNull(innerKeySelector);
ArgumentNullException.ThrowIfNull(resultSelector);

return outer.Provider.CreateQuery<TResult>(
Expression.Call(
null,
CachedReflectionInfo.Join_TOuter_TInner_TKey_TResult_5(typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, GetSourceExpression(inner), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector)));
}
[DynamicDependency("Join`4", typeof(Enumerable))]
public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
{
ArgumentNullException.ThrowIfNull(outer);
ArgumentNullException.ThrowIfNull(inner);
ArgumentNullException.ThrowIfNull(outerKeySelector);
ArgumentNullException.ThrowIfNull(innerKeySelector);
ArgumentNullException.ThrowIfNull(resultSelector);

return outer.Provider.CreateQuery<TResult>(
Expression.Call(
null,
CachedReflectionInfo.Join_TOuter_TInner_TKey_TResult_5(typeof(TOuter), typeof(TInner), typeof(TKey), typeof(TResult)), outer.Expression, GetSourceExpression(inner), Expression.Quote(outerKeySelector), Expression.Quote(innerKeySelector), Expression.Quote(resultSelector)));
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
Thanks for your help, I'll have a look. I am also looking to see if Linq can do it.
Alerin
Alerin2y ago
Error CS1937 The name "term" is outside the range of the left side of the equality operator. Consider swapping expressions on both sides of the equality operator.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
string
Alerin
Alerin2y ago
Alerin
Alerin2y ago
hmm
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
https://github.com/ninjanye/SearchExtensions I found a library here but it doesn't seem to be updated.
GitHub
GitHub - ninjanye/SearchExtensions: Library of IQueryable extension...
Library of IQueryable extension methods to perform searching - GitHub - ninjanye/SearchExtensions: Library of IQueryable extension methods to perform searching
Alerin
Alerin2y ago
yes, x.Details.Content and Title string
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
I'm surprised that ef core doesn't support this by default.
Alerin
Alerin2y ago
what is "o"?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Alerin
Alerin2y ago
I think it works, thank you so much for your help. I think I will have to think about elastic search and look for results there 😄
Alerin
Alerin2y ago
Ready code as if someone was looking.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PontiacGTX
PontiacGTX2y ago
if you call AsAsyncEnumerable you will load that on memory whilel it is selecting
Alerin
Alerin2y ago
Will there be a performance issue?
PontiacGTX
PontiacGTX2y ago
maybe copare suing the AsAsync Enumerable below the where above the first select also why not select the article before hand... so you dont have to query twice the article
Want results from more Discord servers?
Add your server
More Posts