Raven4. IRavenQueryable: apply AND condition to a bundle of searches

55 views
Skip to first unread message

Alexander Klaus

unread,
Jun 21, 2018, 1:30:31 AM6/21/18
to RavenDB - 2nd generation document database
How to write in C# the following query with using IRavenQueryable ?

from index 'Contacts/ForList' 
where Type = 'person' 
  and ( boost(search(FullName, 'Connor'),  2000)
     or boost(search(FullName, 'Connor*'), 1000)
     or boost(search(Email,    'Connor*'),  500)
  )

Judging by the generated expression of IRavenQueryable, there is no way to apply AND condition to a bundle of searches. Am I right?

Alexander Klaus

unread,
Jun 21, 2018, 3:14:19 AM6/21/18
to RavenDB - 2nd generation document database
Hmm... there is Intersection() extension method. Silly me!

Arkadiusz Palinski

unread,
Jun 21, 2018, 9:01:52 AM6/21/18
to rav...@googlegroups.com
Intersection has different behavior -  it returns docs that match _all_ the provided sub-queries. You are looking for DocumentQuery:

var q = session.Advanced.DocumentQuery<User, Contacts_ForList>()
                        .WhereEquals(x => x.Type, "person")
                        .AndAlso()
                        .OpenSubclause()
                            .Search(x => x.LastName, "Conor").Boost(2000)
                            .Search(x => x.LastName, "Conor*").Boost(1000)
                            .Search(x => x.Email, "Conor").Boost(500)
                        .CloseSubclause();

You can apply AND to the query by calling .AndAlso()

--
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.
For more options, visit https://groups.google.com/d/optout.

Alexander Klaus

unread,
Jun 22, 2018, 1:52:52 AM6/22/18
to RavenDB - 2nd generation document database
Hi Arkadiusz,

I understand that intersection has a different behaviour. Comparing it to a similar SQL behaviour, it does a JOIN on itself.

However, it DOES return the correct result set for a problem I described above. The RQL would look like:
from index 'Contacts/ForList' 
where intersect(
     Type = 'person', 
        boost(search(FullName, 'Connor'),  2000)
     or boost(search(FullName, 'Connor*'), 1000)
     or boost(search(Email,    'Connor*'),  500)
    )

Therefore, I would like to ask two questions: 
  1. As usage of DocumentQuery is less scalable than Query, do you have plans on a better integration of the Search method in the WHERE expression of a query?
    The ultimate goal is to use Search/Boost inside the WHERE condition.

  2. What are performance implications of using intersect in the example above? Any known downsides?
Thank you,
Alex

Oren Eini (Ayende Rahien)

unread,
Jun 22, 2018, 2:03:36 AM6/22/18
to ravendb
1) Query is using DocumentQuery under the covers, they have the same exact performance. 
The API is built in layers, with the Query meant to provide 90% of the functionality, with the more advanced queries handed off to DocumentQuery.
The most complex stuff can be handled via RawQuery.
2) Typically intersect is used with fanout. What is the issue with using the DocumentQuery as Arek have shown?

Hibernating Rhinos Ltd  

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

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

 


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

Arkadiusz Palinski

unread,
Jun 22, 2018, 2:51:09 AM6/22/18
to rav...@googlegroups.com
In order to search when using Query you can use Search extension method (using Raven.Client.Documents;)

session.Query<User, Contacts_ForList>()
                        .Where(x => x.Type == "person")
                        .Search(x => x.LastName, "Conor", 2000)
                        .Search(x => x.LastName, "Conor*", 1000)
                        .Search(x => x.Email, "Conor", 500);

Under the hood it's the same thing as with the usage of DocumentQuery anyway.

Alexander Klaus

unread,
Jun 25, 2018, 4:03:32 AM6/25/18
to RavenDB - 2nd generation document database
Sorry Arkadiusz  and Oren, I withdraw my question.

Query is fine, it seems that my testing conditions were incorrect, as while I was using Query I didn't have the boost/search commands bundled up.

So instead of getting an RQL like 
from index 'contacts/Bla' 
where Type='Person' and
(search(LastName, "Conor") or search(LastName, "Conor*") or search(Email, "Conor"))
I was getting
from index 'contacts/Bla' 
where Type='Person' and 
search(LastName, "Conor") or search(LastName, "Conor*") or search(Email, "Conor")

I hope, it's not a bug, just my mistake somewhere, as now I get correct RQL.

Thank you for your help.
Reply all
Reply to author
Forward
0 new messages