1

I have a 'Message' class:

public class User { public string Name { get;set; } /*Other properties*/ }
public class Message {
    public User From { get;set; }
    public ICollection<User> To { get;set; }
    public string Title { get;set; }
    /* Others */ 
}

And I have an index defined like so: (yes, straight out of the 'I've been following a tutorial video')

public class Message_ToFromTitle : AbstractIndexCreationTask
{
    public override IndexDefinition CreateIndexDefinition()
    {
        return new IndexDefinition
        {
            Map = "from m in docs.Messages select new { Query = new[] { m.Title, m.From.Name, m.To.Select(r => r.Name), } }",
            Indexes = {{"Query", FieldIndexing.Analyzed}}
        };
    }
}

Which in the Management Studio I can query quite successfully with things like Query:Chris and it'll bring all Messages with Chris in the 'To', 'From' or 'Title' which is great.

My problem is how do I query from .NET? I've tried:

session.Advanced.LuceneQuery<Message, Message_ToFromTitle>().Where("Query:Chris");

which works, but what I'd like to do is:

session.Query<Message, Message_ToFromTitle>().Where(m => m == "Chris");

But, raven (quite rightly) doesn't know what to do with 'm', as presumably I need to somehow query Query. Is the Advanced route the only one open to me? I'm quite happy to change the Index definition, I've used the non-generic AbstractIndexCreationTask as I'm doing a m.To.Select(r=>r.Name) bit at the end which won't compile in C#, but is interpreted in the way I want in Raven, but will and would happily change to a generic one if needed!

Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42

1 Answers1

0

You probably mean that you want to do:

session.Query<Message, Message_ToFromTitle>().Where(m => m.Query == "Chris");

And to be able to do this in a strongly typed manner you have to provide a class RavenDB's client API can work out the type from. Something like this will work:

session.Query<MessageClassForQuerying, Message_ToFromTitle>().Where(m => m.Query == "Chris");

Where:

public class MessageClassForQuerying : Message {
    public string Query { get; set; }
}

But using the Advanced.LuceneQuery part is perfectly ok.

Also, when using Advanced.LuceneQuery you want to use WhereEquals() or Search(), and not Where directly. There's a lot of escaping logic involved that you want to take advantage of.

synhershko
  • 4,472
  • 1
  • 30
  • 37