ninjanye / SearchExtensions

Library of IQueryable extension methods to perform searching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to do an AND search for multiple params?

JamesReate opened this issue · comments

Hi, your libary is awesome, really helped me out!

So, given the following example:
query = query.Search(x => x.Code, x => x.Name, x => x.Brand) .Containing(stringArray);
How would I do to get the results filtered by the multiple strings in the stringArray ? Currently I see the result is additive. Or is this a feature you're still working on? I cloned your code but I don't have much experience with LINQ expressions, so i've been kind of lost trying to tweak it.

To clarify what i mean, lets say that for the two properties Name and Brand I have the following table:

| Name | Brand


1 | doggy | Nutram
2 | ring | Nutram

If the containg has params ("nutram", "ring") I should only get back result #2....

Thanks, appreciate any feedback

Hi @JamesReate

I see what you mean. I guess what you are after is a .ContainingAll() method that only returns results that contain all the supplied terms. Is my understanding correct?

Sounds like a great feature to add...

I'm just finishing off a new Levenshtein search feature so once that is complete I'll start working on this.

Thanks for getting in touch. I'll update soon

Hi @JamesReate

Not sure if you could possibly get round the issue by using the following...

query.Search(x => x.Name).Containing(stringArray)
     .Search(x => x.Brand).Containing(stringArray)

This will return results where Name contains any item in stringArray AND Brand contains any item in stringArray

UPDATE
Note, this is different to the ContainingAll() method I mentioned above.

Great thanks for your reply! I'll give your workaround a try and see what the result is, see what my customers think.

Yes, the ContainingAll method concept you explain is exactly what I meant. Awesome if you end up adding it as a feature :) I looked at your source code, but not exaclty sure how stuff worked, what would I need to learn to be able to help?

I tried the workaround, although didn't do as we needed since it would return 0 results with only one search param, ie. given above table if I search only "nutram" I dont get any results would have to search "nutram" & "doggy".
In our search input we just do a .Split(' ') to get the array of params.

Yeah the difference between the workaraound and new new ContainingAll() method will be as follows:

Given the following terms to search for:

var options = new[] {"contains", "all", "words"}

And the following data

Id Name Brand
1 contains all words
2 contains words
3 something strange

The workaround

query.Search(x => x.Name).Containing(options)
     .Search(x => x.Brand).Containing(options)

The workaround code will return both record #1 and #2 because the search requests that Name contains any of the options AND Brand contains any of the options

The new feature

The new feature would be something like the following

query.Search(x => x.Name).ContainingAll(options);

This would only return record one as it is the only record that mentions all the options.

Hope that helps to clear things up

Hi @JamesReate

Just seen you added a comment at the same time I was composing... Yeah, it seems you are going to need the .ContainingAll(). I think things are going to start to get tricksy pretty quickly with different variants of this, for instance:

query.Search(x => x.Name, x => x.Brand).ContainingAll("test", "search");

Should the result of the above return only records where a property (Name, Brand) contains all the search terms or should the code return records where all search terms were matched across all/any of the properties?

The second would have more results in certain cases since it will allow for all the terms to be matched across properties

Thoughts?

I would go for the 2nd option, which I think is more useful - at least it is what I would use all over my application.

Yeah, agreed. That was preferred option to. Ok let me crack on with it and
I'll keep you posted here.
On 24 Nov 2014 18:29, "James Reategui" notifications@github.com wrote:

I would go for the 2nd option, which I think is more useful - at least it
is what I would use all over my application.


Reply to this email directly or view it on GitHub
#1 (comment)
.

Hi @JamesReate

I have managed to complete this feature so will be pushing out as a package release later today.

I'm not sure what version of SearchExtensions you are using but version 1.3 will include this change. Alternatively, if you prefer, as part of developing the feature, I realised you are able to achieve this goal with the current code (albeit not as elegantly)

var searchTerms = new string[]{"test", "search", "code"};
var query = context.Search(x => Name, x => x.Brand);

foreach(string term in searchTerms){
    query = query.Containing(term);
}

var result = query.ToList();

Obviously this is not as nice as the new feature which will work as follows:

var searchTerms = new string[]{"test", "search", "code"};
var query = context.Search(x => Name, x => x.Brand).ContainingAll(searchTerms);

Will update when I get the package released to the new version.

Thanks again for putting this request in, I would not have realised the need for this feature had you not got in touch.

Cheers

That was quick! Thanks a lot, will update the nuget package later today and give it a try. This will really make the search feature in our web app a lot better.

Hi @JamesReate

Release 1.3 is now up on nuget, so feel free to update and check it out

Cheers

Yup, it does the trick exactly as expected!

Awesome, sorry, closed the issue a little early, should have waited for your confirmation