RavenDB:如何使用多映射/Reduce 索引
我有非常简单的模型:
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 包含一些翻译,PhraseMeta 包含几个短语的元信息。 我正在尝试查找具有模块名称和语言的短语文本。 据我了解,RavenDB 的多映射/减少索引功能可以提供帮助,而不是使用WhereEntityIs。 我的索引是:
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);
}
}
这就是我尝试使用它的方式:
var entry = documentSession.Query<PhraseEntry, PhraseTranslationIndex>
.Where(p => p.ModuleName == "MyModule")
.Where(p => p.Language == "en")
.FirstOrDefault();
并且该索引没有结果。我正在使用 build 472。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您正在使用 First(),请尝试使用 FirstOrDefault()
The problem is probably that you are using First(), try using FirstOrDefault()