Search for multiple values in a field

38 views
Skip to first unread message

Kaspar Tilk

unread,
May 21, 2018, 8:28:56 AM5/21/18
to RavenDB - 2nd generation document database
Hey

In the documentation (https://ravendb.net/docs/article-page/4.0/csharp/indexes/querying/searching#querying--searching) it says: "Here is a sample code that uses the Search extension to get users with the name John OR Adam"
What I want to do is get users with names John AND Adam. 
For example a field has a value: "The quick brown fox jumps over the lazy dog" and the search term is "fox dog" then it returns the field. But for search term "fox cat" it wouldn't.

Thanks in advance,
Kaspar

Oren Eini (Ayende Rahien)

unread,
May 21, 2018, 8:31:24 AM5/21/18
to ravendb
from Users
where search(Name, 'John Adam', and)

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kaspar Tilk

unread,
May 22, 2018, 3:01:40 AM5/22/18
to RavenDB - 2nd generation document database
Ahh, I think I know whats causing my problem. Wehn I use boosts in .NET the AND doesn't work, but the Raven .NET library doesn't let me use AND without boost.
I tried the query you suggested in RavenDB web client and it worked as expected, but as soon as I added boosts the AND didn't seem to work anymore. The boosts I added had the same weight.
Example queries:
from index 'Index/Persons/ByName'
where search(Name, 'John Adam', and) or search(Name, 'John* Adam*', and)   <- works as expected

from index 'Index/Persons/ByName'
where search(Name, 'John Adam', 1, and) or search(Name, 'John* Adam*', 1, and) <- seems to use OR instead of AND
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Kaspar Tilk

unread,
May 22, 2018, 3:41:05 AM5/22/18
to RavenDB - 2nd generation document database
I figured out how to use Search without boosts, but it still seems to OR the query terms in .NET library.
My code:
var searchedPeople = session
    .Query<Person, Index_Persons_ByName>()
    .Search(x => x.Name, "Adam John", options: SearchOptions.And)
    .Search(x => x.Name, "Adam* John*", options: SearchOptions.And).ToList();

It would return me all Adams instead of Adam John, Adamson Johnson, Adam Johnson, John Adamson, etc.

Index code:
    public class Index_Persons_ByName : AbstractIndexCreationTask<Person>
    {
        public Index_Persons_ByName()
        {
            Map = persons => from person in persons
                             select new
                             {
                                 Name = person.Name
                             };

            Index(x => x.Name, FieldIndexing.Search);
            
        }
    }


Kaspar Tilk

unread,
May 22, 2018, 3:43:32 AM5/22/18
to RavenDB - 2nd generation document database
Also, is there a way to see what is the actual Raven query the C# code is translated into?

Oren Eini (Ayende Rahien)

unread,
May 22, 2018, 3:54:49 AM5/22/18
to ravendb
Call ToString on this

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Idan Shalom

unread,
May 22, 2018, 4:39:32 AM5/22/18
to rav...@googlegroups.com
Hi
"Each of the search terms (separated by space character) will be checked independently." this is from the documentation.
The option parameter is to tell ravendb how to connect the field with the other search in the query (AND, OR ,etc).
So between the two search you will get an AND but ravendb still going to check the search terms independently.

Something like this :

var searchedPeople = session
                        .Query<Person, Index_Persons_ByName>()
                        .Search(x => x.Name, "*Adam*")
                        .Search(x => x.Name, "*John*", options: SearchOptions.And).ToList();

will return the right results for you

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Idan Haim Shalom l Core Team Developer

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811 l Skype: shalomidanhaim

RavenDB paving the way to "Data Made Simplehttp://ravendb.net

Kaspar Tilk

unread,
May 22, 2018, 4:54:19 AM5/22/18
to RavenDB - 2nd generation document database
But how would I solve it if I don't know how many search terms I get. I'm using it for a search box on my site. If the user searches for "Adam John Megan" I'd need three searches in code.
I'd need something like this:
var searchedPeople = session
                        .Query<Person, Index_Persons_ByName>()
                        .Search(x => x.Name, "*Adam*")
                        .Search(x => x.Name, "*John*", options: SearchOptions.And)
                        .Search(x => x.Name, "*Megan*", options: SearchOptions.And).ToList();
but the number of names in this case is unknown.

As I said the query:

from index 'Index/Persons/ByName'
where search(Name, 'John Adam', and) or search(Name, 'John* Adam*', and) - works as expected

But I seem to struggle with the .NET equivalent of the above.
Call ToString on this
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
May 22, 2018, 5:25:44 AM5/22/18
to ravendb
It would be something like:

var searchedPeople = session.Advanced.DocumentQuery<Person, Index_Persons_ByName>()
                        .Search(x => x.Name, "*Adam*").OrElse()
                        .Search(x => x.Name, "*John*", options: SearchOptions.And) .OrElse()
                        .Search(x => x.Name, "*Megan*", options: SearchOptions.And).ToList();

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Kaspar Tilk

unread,
May 22, 2018, 5:46:06 AM5/22/18
to RavenDB - 2nd generation document database
So as I understand it's not possible to do:
var searchedPeople = session
                        .Query<Person, Index_Persons_ByName>()
                        .Search(x => x.Name, "*Adam* *John* *Megan*").ToList();
to get persons thats names contain all three names: Adam AND John AND Megan.
I don't think I can use your example to build queries dynamically because I don't know how many names I need to search for beforehand.

Oren Eini (Ayende Rahien)

unread,
May 22, 2018, 6:38:24 AM5/22/18
to ravendb
The AND should do that, IIRC.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


Oren Eini (Ayende Rahien)

unread,
May 22, 2018, 6:39:44 AM5/22/18
to ravendb
And my example would be:

var query =   session.Advanced.DocumentQuery<Person, Index_Persons_ByName>()
       .Search( x=> x.Name, names[0]);

for(var i = 1; i < names.Length; i++ )
     query = query.OrElse().Search(x=>x.Name, names[i]);

query.ToList()

Kaspar Tilk

unread,
May 23, 2018, 2:12:31 AM5/23/18
to RavenDB - 2nd generation document database
Thank you so much! Similar solution worked for me and I'm getting the results I expected from the beginning.
Reply all
Reply to author
Forward
0 new messages