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
39 Replies
my example code:
Did I do something wrong?
Oh, sorry. That's my bad.
When you're using LINQ with EF, it turns your calls into expression trees, and converts those into SQL.
It doesn't run your lambdas as C#.
Let me think about this a bit more.
Ok thank you for help 😄
See whether this works any better:
Hmm, actually I need to do a bit more to that... editing...
That's definitely not optimized, but I think it will work.
If it finds your first term in one of the entries, it will still look for your second in that entry. And your third, etc. Which accomplishes nothing, because the entry will be in the results regardless.
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.
What type is AsNoTracking() returning?
IQueryable<Models.Article>
Try:
Join<Models.Article, string>
That will only change the error message, but it might become more useful.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?).
Urgh. Not the change I wanted.
Queryable.Join Method (System.Linq)
Correlates the elements of two sequences based on matching keys.
I'm not sure why we aren't calling that.
How about this?
Sorry, I had to edit that one too.
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.
Requires arguments of type... 4? ???
Oh, gotcha.
Yes, I have no idea what this is about
So it wants to know what type we're using to compare the outer and inner, and what type we're returning.
The problem with the join that I wrote above is that I intended to just stuff both values in an object (in SQL, a temporary table, which come to think of it may not be standard SQL and therefore not available in LINQ) rather than comparing them.
@Alerin
LINQ query syntax is not my strong suit, and of course this is untested.
Thanks for your help, I'll have a look. I am also looking to see if Linq can do it.
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.
OK that's a new one.
What type does it say term is?
string
hmm
And x.Details.Content?
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
yes, x.Details.Content and Title string
Ugh.
I give up on doing it the ideal way.
That will load all the articles into memory and then filter them.
AsEnumerable manages that difference.
I wanted to filter in the database, but may not be possible with the restrictions of IQueryable.
I'm surprised that ef core doesn't support this by default.
what is "o"?
I may just not be aware of some technique. I felt like it should work, too.
Ah, shoot.
Edited.
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 😄
Ready code as if someone was looking.
You can simplify those two Select calls into one.
You would need parens around the await, though.
Or a separate variable.
if you call AsAsyncEnumerable you will load that on memory whilel it is selecting
Will there be a performance issue?
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