开发者

How to create a composite index in Ravendb

I have documents collection "Messages" in my base (RavenDB) Document definition like:

class Message
{
    string Content;
    Tag[] Tags;
    Location[] Location开发者_开发技巧s;
    string[] Actions;
    bool IsActive;
}

Tag class definition:

class Tag
{
    string Value;
    Translation[] Translations;
}

Location class:

class Location
{
    string Code;
    Translation[] Translations;
}

Translation class:

class Translation
{
    string LanguageCode;
    string Value;
}

So, I want to create a index that will allow me to make queries by several fields:

  1. Full-text search by Message.Content
  2. Only messages with IsActive==true
  3. Messages that contains my action in Message.Actions
  4. Messages that contains tag with myValue and myLanguageCode
  5. Locations that contains location with some myCode and myLanguageCode

I would like to query to be on all the conditions simultaneously

So, how should i define index for RavenDB?


Well, after a short studies of RavenDB auto dynamic indexes i created something like

new IndexDefinition
{
    Map = @"
     from doc in docs.Messages 
      where IsActive==true
     from docActionsItem in (IEnumerable<dynamic>)doc.Actions
     from docTagsItem in (IEnumerable<dynamic>)doc.Tags 
     from docTagsItemTranslationsItem in (IEnumerable<dynamic>)docTagsItem.Translations
     from docLocationsItem in (IEnumerable<dynamic>)doc.Locations
     from docLocationsItemTranslationsItem in (IEnumerable<dynamic>)docLocationsItem.Translations
     select new { 
      TagsValue = docTagsItem.Value, 
      Content = doc.Content, 
      Actions=docActionsItem, 
      TagsTranslationsLanguageCode = docTagsItemTranslationsItem.LanguageCode,
      TagsTranslationsValue = docTagsItemTranslationsItem.Value, 
      LocationsCode = docLocationsItem.Code, 
      LocationsTranslationsLanguageCode=docLocationsItemTranslationsItem.LanguageCode,
      LocationsTranslationsValue=docLocationsItemTranslationsItem.Value
     }",
    Analyzers =
        {
            {"Content", typeof(StandardAnalyzer).FullName},                                    
        }
}


There's no need to define an index upfront to do queries in RavenDB. Just create the linq-query - RavenDB will then dynamically create an index for you without any additional cost.

The only use-case where you would possible want to create an index by your own, would be if want do specify a different Lucene.NET analyzer to do what you call "full-text search" on the field Message.Content. But even that should be as simple as just creating the linq-query for the index and pass it into RavenDB on startup. If you want to know how to do that, I recommend to have a look into Ayende RaccoonBlog sample or the officials docs, which will be updated soon (FYI -> ravendb/docs has the new docs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜