Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple keyword database search using c# (linq to sql)

I am trying to build a search that accepts multiple keywords (space delimited or comma, that is not the issue). I currently have this

public IQueryable<Post> Search(string criteria, int x)
    {
        return
            (_db.Posts.Where(p => p.IsActive && 
                (p.PostText.Contains(criteria) || p.Weather.Contains(criteria) || p.Location.Contains(criteria))
            ).OrderByDescending(p => p.PostDate)).Take(x);
    }

but that would return only exact matches. How would I search for each keyword and return x results? It's supposed to be an OR by the way.

Thanks

like image 213
odle Avatar asked Sep 03 '26 11:09

odle


1 Answers

Try this, I am using parallelism to speed up the search. I am checking in a series of strings, of any of the keys passed in the string[] match. In my example, I am searching for "test" and "1", and you will see 3 results.

static void Main(string[] args)
    {            
        List<string> list = new List<string>();
        list.Add("b lue number 1");
        list.Add("test number 234");
        list.Add("yello number 2334");
        list.Add("yes whippea number 324234");
        list.Add("test number asdf");  

        var results = Program.Search(list,"test","1");

        Console.ReadLine();
    }

To follow your example, you can do something like this.

public List<Post> Search(string criteria, int x)
    {
        // Split all the search keys by space, so if you have "Search Word", you will get
        // the occurances of [Search] and also [Word]
        List<string> searchKeys = criteria.Split(' ').ToList<string>();

        // Filter active
        _db = _db.Where(p => p.IsActive);

        // Go through each key
        foreach (string str in searchKeys)
        {
            _db = _db.Where(p => p.Location.Contains(str)
                    || p.PostText.Contains(str)
                    || p.Weather.Contains(str));
        }

        // Return number wanted - and items will only be extracted here on the ToList()
        return _db.OrderByDescending(p => p.PostDate).Take(x).ToList();
    }

I am looping the criteria and adding the conditions one by one. Criteria has to be a string of the search keys that you are doing and are split between spaces to get all occurrences.

like image 132
Mez Avatar answered Sep 05 '26 23:09

Mez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!