Ravendb 按实体名称和上次修改日期搜索
我在RavenDb中存储了许多命令,它们都实现了ICommand。我希望能够搜索上次修改的元数据和Raven实体名称。我目前正在对每个命令执行多重映射,如下所示:Ravendb 按实体名称和上次修改日期搜索,ravendb,Ravendb,我在RavenDb中存储了许多命令,它们都实现了ICommand。我希望能够搜索上次修改的元数据和Raven实体名称。我目前正在对每个命令执行多重映射,如下所示: public class CommandAuditSearch_Index : AbstractMultiMapIndexCreationTask<CommandAuditSearch_Index.Results> { public class Results {
public class CommandAuditSearch_Index : AbstractMultiMapIndexCreationTask<CommandAuditSearch_Index.Results>
{
public class Results
{
public string CommandType { get; set; }
public DateTime LastModified { get; set; }
}
public CommandAuditSearch_Index()
{
AddMap<NewEmployeeStartCommand>(employees => employees.Select(x => new
{
CommandType = MetadataFor(x).Value<string>("Raven-Entity-Name"),
LastModified = MetadataFor(x).Value<DateTime>("Last-Modified")
}));
AddMap<EmployeeLeaverCommand>(employees => employees.Select(x => new
{
CommandType = MetadataFor(x).Value<string>("Raven-Entity-Name"),
LastModified = MetadataFor(x).Value<DateTime>("Last-Modified")
}));
Index(results => results.CommandType, FieldIndexing.Analyzed);
}
}
公共类CommandAuditSearch\u索引:AbstractMultiMapIndexCreationTask
{
公开课成绩
{
公共字符串CommandType{get;set;}
公共日期时间LastModified{get;set;}
}
公共命令AuditSearch_索引()
{
AddMap(employees=>employees.Select(x=>new
{
CommandType=MetadataFor(x).Value(“Raven实体名称”),
LastModified=MetadataFor(x).Value(“上次修改”)
}));
AddMap(employees=>employees.Select(x=>new
{
CommandType=MetadataFor(x).Value(“Raven实体名称”),
LastModified=MetadataFor(x).Value(“上次修改”)
}));
索引(results=>results.CommandType,FieldIndexing.analysis);
}
}
我的质疑如下:
session.Query<CommandAuditSearch_Index.Results, CommandAuditSearch_Index>()
.Where(x => x.CommandType == commandType && x.LastModified >= DateTime.Today).OfType<ICommand>().ToList();
session.Query()
其中(x=>x.CommandType==CommandType&&x.LastModified>=DateTime.Today).OfType().ToList();
我知道Raven中已经内置了一个索引,用于获取标签(实体名称)和上次修改日期,但我似乎不知道如何获得上面的索引所给出的结果
有人能给我指出一个静态索引的正确方向吗?在这里,我不必像上面那样为我查询的每个命令提供多个映射,这些命令会将结果作为ICommand列表提供给我
谢谢
帕迪有几点:
- 除非要使用
query方法进行全文搜索,否则不需要将字段标记为已分析。如果只使用Search
,则分析索引项没有任何好处Where
- 如果要在结果中查找元数据值,只需使用
从元数据而不是文档数据中获取它们GetMetadataFor
- 正如你所说的,已经有了你所需要的索引。查询它的最简单方法是使用
APILuceneQuery
var tag = documentStore.Conventions.GetTypeTagName(theCommand.GetType()); var results = session.Advanced .LuceneQuery<ICommand, RavenDocumentsByEntityName>() .WhereEquals("Tag", tag) .AndAlso() .WhereGreaterThanOrEqual("LastModified", DateTime.Today .ToUniversalTime()) .ToList(); foreach (var result in results) { var md = session.Advanced.GetMetadataFor(result); var entityName = md.Value<string>("Raven-Entity-Name"); var lastModified = md.Value<DateTime>("Last-Modified"); // you can see all the metadata if you like Debug.WriteLine(md.ToString(Formatting.Indented)); }
var tag=documentStore.Conventions.GetTypeTagName(command.GetType()); var results=session.Advanced .LuceneQuery() .WhereEquals(“Tag”,Tag) .AndAlso() 。其中大于或等于(“上次修改”,日期时间。今天 .ToUniversalTime()) .ToList(); foreach(结果中的var结果) { var md=session.Advanced.GetMetadataFor(结果); var entityName=md.Value(“Raven实体名称”); var lastModified=md.Value(“上次修改”); //如果愿意,可以查看所有元数据 WriteLine(md.ToString(Formatting.Indented)); }