LINQ Search Keywords

If I have a text box that has a title and I have a list of keywords, how can I search for a title for (n) the number of keywords in the title?

So, if my heading is “Bake chicken, bacon and leeks” and the user searches for “chicken-bacon-turnip”, I would like to return the above recipe.

Essentially, I would like to say that if the title indicates 2 or more search terms, then it is considered valid and must be returned. but if it contains only 1, then ignore it.

Ideally, I would like them to be weighted so that the more terms, the higher the list, but maybe version 2. :)

change

At this point I should mention that I would like it to be native .net and C #.

+3
source share
4 answers

Well, I know you said, "Do it in Link." ASSuming, you say that you use the native .Net string and execute it using Linq for objects, then, I think, the most obvious solution is to split the text into a regular expression that works on word boundaries; and then iterate over each result matched against the input phrases.

However...

Judging by your idea for "v2", I think you should probably look at something more powerful and oriented around text search - so what about using Lucene.Net text index?

- ; , , .

.

- Linq to Sql

SQL Full-Text , : Linq To Sql CONTAINSTABLE .

, , DataContext.ExecuteQuery<TResult>. , , , .

, , , ;)

+4

, Andras, , , . : , 1 , . . :

var entries = new[] { new Entry{ ID = 1,  Title = "Baking a chicken, bacon and leek pie"} }.AsQueryable();
var search = "chicken bacon turnip";
var q = entries.Select(GetSelector(search));
var matches = q.Where(e => e.MatchCount > 1);

public Expression<Func<Entry, EntryMatchCount>> GetSelector(string search)
{
    var searchWords = search.Split(new[] {' '});
    // Rather than creating the selector explicitly as below, you'll want to
    // write code to generate this expression tree.
    Expression<Func<Entry, EntryMatchCount>> selector = e =>
            new EntryMatchCount
            {
                ID = e.ID,
                MatchCount = (e.Title.Contains(searchWords[0]) ? 1 : 0) +
                            (e.Title.Contains(searchWords[1]) ? 1 : 0) +
                            (e.Title.Contains(searchWords[2]) ? 1 : 0)
            };
    return selector;
}
+1

If it were me, I would just do something like this ....

Create a helper class that does 2 things, splits the title, and returns a score based on keyword matching ....

public static class Helper
{

  public static int GetScore(string Title, params string[] keywords)
  {
    // your routine that calcs a score based on the matchs against the Title.
  }
}

then you can use a linq statement like....

var matches = from t in GetYourTitles
              let score = Helper.GetScore(t, keywordlist)
              where score >= 2
              orderby score
              select t;
0
source

AODBDataContext db = new AODBDataContext ();

        var fItems = from item in db.Items
                     where SqlMethods.Like(item.Name, l)
                     where cats.Contains(item.ItemType)
                     where item.QL >= minQL
                     where item.QL <= maxQL
                     select item;
0
source

Source: https://habr.com/ru/post/1755623/


All Articles