开发者

RavenDB: How to use Multi Maps / Reduce indexes

I have quite simple model:

public class PhraseMeta:
{
 public 开发者_运维百科int Id { get; set; }
 public string ModuleName { get; set; }
 public string Description { get; set; }
 public DateTime ModifiedDate { get; set; }
}

public class Phrase
{
  public int Id { get; set; }
  public int PhraseMetaId { get; set; } //reference to PhraseMeta
  public string Language { get; set; }
  public string Text { get; set; }
}

Phrase contains some translations and PhraseMeta has meta information for several Phrases. I am trying to find Phrase's Text having ModuleName and Language. As I understood RavenDB's Multi Maps / Reduce indexes feature can help with it instead of using WhereEntityIs. My index is:

public class PhraseEntry
{
  public string MetaId { get; set; }
  public string ModuleName { get; set; }
  public string Language { get; set; }
  public string Text { get; set; } 
}

public class PhraseTranslationIndex : AbstractMultiMapIndexCreationTask<PhraseEntry>
{
  public PhraseTranslationIndex()
  {
    this.AddMap<PhraseMeta>(phraseMetas => from pm in phraseMetas
                 select new
                 {
                   MetaId = pm.Id,
                   ModuleName = pm.ModuleName,
                   Language = (string)null,
                   Text = (string)null
                 });

    this.AddMap<Phrase>(phrases => from phrase in phrases
               select new
               {
                 MetaId = phrase.PhraseMetaId,
                 ModuleName = (string)null,
                 Language = phrase.Language,
                 Text = phrase.Text
               });

    this.Reduce = results => from entry in results
                 group entry by entry.MetaId
                   into g
                   select new
                   {
                     MetaId = g.Key,
                     ModuleName = g.Select(x => x.ModuleName).Where(x => x != null).First(),
                     Language = g.Select(x => x.Language).Where(x => x != null).First(),
                     Text = g.Select(x => x.Text).Where(x => x != null).First()
                   };

    this.Index(x => x.ModuleName, FieldIndexing.Analyzed);
    this.Index(x => x.Language, FieldIndexing.Analyzed);
    this.Index(x => x.Text, FieldIndexing.Analyzed);
  }
}

This is how I am trying to use it:

var entry = documentSession.Query<PhraseEntry, PhraseTranslationIndex>
                           .Where(p => p.ModuleName == "MyModule")
                           .Where(p => p.Language == "en")
                           .FirstOrDefault();

And this index has no results. I am using build 472.

Any ideas?


The problem is probably that you are using First(), try using FirstOrDefault()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜