Linq query with multiple Contains/Any for RavenDB

后端 未结 1 1053
一个人的身影
一个人的身影 2020-12-29 07:22

I have a document class that contains a list of \"tags\". Something like:

class Item {
  string Name { get; set; }
  List Tags {get; set;}
}


        
相关标签:
1条回答
  • 2020-12-29 07:39

    Contains is indeed not yet supported (Perhaps it should be, but that's another matter entirely - we only really add support for various operators when its asked for)

    As for multiple queries against Any, I assume you're trying to do dynamic data and you want to achieve something like

    "X OR Y OR Z"
    

    That's a tricky one, and the LINQ provider by default will aggregate those multiple WHERE clauses with AND, so your example looks like

    "X AND Y AND Z"
    

    Which will obviously never be the case.

    Your best option for this one is to drop down to the Lucene query (at least for now) and do something like this:

    var results = s.Advanced.LuceneQuery<Item>()
                       .Where(string.Format("Tags,:({0})", string.Join(" OR ", tags))); 
    

    Make sense?

    The query above will look something like

    "Tags,:(X OR Y OR Z)"
    

    Note: "Tags," informs RavenDB that Tags is an array

    Okay, [edit]!

    The easiest way to get what you actually want is to do something along these lines

                    new IndexDefinition<Item, Item>()
                    {
                        Map = docs => from doc in docs
                                      select new
                                      {
                                          Tags = doc.Tags
                                      },
                        Indexes = {{ x => x.Tags, FieldIndexing.Analyzed }}
                    }.ToIndexDefinition(store.Conventions));
    

    Then to query for your ands, you can do something like this:

                    var results = s.Advanced.LuceneQuery<Item, WhateverYouCalledThatIndex>()
                       .Where(string.Format("Tags:({0})", string.Join(" AND ", tags)));
    

    Now, things to be aware of

           Tags = doc.Tags
    

    Will serialize that entire array into one giant blob, as it's just strings that will work for this example.

    I am looking at better ways of expressing this, it is unlikely that we'll come up with a LINQ-ish way of doing this, as it doesn't really map across very well - but it is an answer that will work :)

    I think I'd quite like to be able to at least do

      Map = docs => from doc in docs
                                      select new
                                      {
                                          Tags = String.Join(" ", doc.Tags)
                                      },
    

    (This won't work so don't try it), but it is a bit more explicit about what you want to achieve.

    0 讨论(0)
提交回复
热议问题