Adam
Adam

Reputation: 4227

Raven query returns 0 results for collection contains

I have a basic schema

Post {
    Labels: [
        { Text: "Mine" }
        { Text: "Incomplete" }
    ]
}

And I am querying raven, to ask for all posts with BOTH "Mine" and "Incomplete" labels.

queryable.Where(candidate => candidate.Labels.Any(label => label.Text == "Mine"))
    .Where(candidate => candidate.Labels.Any(label => label.Text == "Incomplete"));

This results in a raven query (from Raven server console)

Query: (Labels,Text:Incomplete) AND (Labels,Text:Mine)
Time: 3 ms
Index: Temp/XWrlnFBeq8ENRd2SCCVqUQ==
Results: 0 returned out of 0 total.

Why is this? If I query for JUST containing "Incomplete", I get 1 result. If I query for JUST containing "Mine", I get the same result - so WHY where I query for them both, I get 0 results?

EDIT:

Ok - so I got a little further. The 'automatically generated index' looks like this

from doc in docs.FeedAnnouncements
from docLabelsItem in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
select new { CreationDate = doc.CreationDate, Labels_Text = docLabelsItem.Text }

So, I THINK the query was basically testing the SAME label for 2 different values. Bad.

I changed it to this:

from doc in docs.FeedAnnouncements
from docLabelsItem1 in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
from docLabelsItem2 in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
select new { CreationDate = doc.CreationDate, Labels1_Text = docLabelsItem1.Text, Labels2_Text = docLabelsItem2.Text }

Now my query (in Raven Studio) Labels1_Text:Mine AND Labels2_Text:Incomplete WORKS!

But, how do I address these phantom fields (Labels1_Text and Labels2_Text) when querying from Linq?

Upvotes: 3

Views: 363

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Adam, You got the reason right. The default index would generate 2 index entries, and your query is executing on a single index entry.

What you want is to either use intersection, or create your own index like this:

from doc in docs.FeedAnnouncements
select new { Labels_Text = doc.Labels.Select(x=>x.Text)}

And that would give you all the label's text in a single index entry, which you can execute a query on.

Upvotes: 1

Related Questions