我的基础(RavenDB)中有文档集“消息” 文档定义如:
class Message
{
string Content;
Tag[] Tags;
Location[] Locations;
string[] Actions;
bool IsActive;
}
标记类定义:
class Tag
{
string Value;
Translation[] Translations;
}
位置等级:
class Location
{
string Code;
Translation[] Translations;
}
翻译课程:
class Translation
{
string LanguageCode;
string Value;
}
所以,我想创建一个索引,允许我通过几个字段进行查询:
我想查询同时处理所有条件
那么,我应该如何为RavenDB定义索引?
答案 0 :(得分:2)
嗯,经过对RavenDB自动动态索引的简短研究后,我创建了类似
的东西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},
}
}
答案 1 :(得分:1)
没有必要在RavenDB中预先定义索引来执行查询。只需创建linq查询 - 然后RavenDB将为您动态创建索引而无需任何额外费用。
您可能希望自己创建索引的唯一用例,如果想要指定一个不同的Lucene.NET分析器来执行您在字段Message.Content上所谓的“全文搜索” 。但即便如此,只需创建索引的linq查询并在启动时将其传递到RavenDB即可。如果您想知道如何做到这一点,我建议您查看Ayende RaccoonBlog样本或官方文档,这些文档将很快更新(仅供参考 - &nbsp; ravendb / docs有新文档)。