按实体名称和上次修改日期搜索 [英] Searching by Entity Name and Last Modified Date

查看:32
本文介绍了按实体名称和上次修改日期搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RavenDb 中存储了许多命令,它们都实现了 ICommand.我希望能够搜索上次修改和 Raven-Entity-Name 的元数据.我目前正在对每个命令进行多映射,如下所示:

I have a number of commands stored in RavenDb and they all implement ICommand. I want to be able to search on the metadata of last modified and Raven-Entity-Name. I am currently doing a multi map on each command as below:

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);
        }
    }

我查询如下:

session.Query<CommandAuditSearch_Index.Results, CommandAuditSearch_Index>()
                              .Where(x => x.CommandType == commandType && x.LastModified >= DateTime.Today).OfType<ICommand>().ToList();

我知道 Raven 中已经内置了一个索引来获取标签(实体名称)和上次修改日期,但我似乎无法弄清楚如何获得上述索引所提供的结果.

I know there is an index already built into Raven to get the Tag (entity name) and last modified date but I cant seem to figure out how to get the results as my index above gives me.

任何人都可以指出我在静态索引的正确方向上,我不必为每个必须查询的命令提供如上所述的多映射,从而将结果作为 ICommand 列表?

can anyone point me in the right direction of a static index where I don't have to have the multi maps as above for each command I have to query that gives me the results as a list of ICommands?

谢谢

稻田

推荐答案

几点:

  • 除非您要使用 Search 查询方法进行全文搜索,否则您不需要将字段标记为已分析.如果你只是使用Where,那么分析索引词是没有优势的.

  • You don't need to mark a field as analyzed unless you are going to be doing fulltext searching with the Search query method. If you are just using Where, there is no advantage to analyzing the index term.

如果您要在结果中查找元数据值,您只需使用 GetMetadataFor 从元数据而不是文档数据中获取它们.参考此处.

If you are looking for metadata values in your results, you simply need to get them from the metadata instead of the document data using GetMetadataFor. Reference here.

正如您所说,已经有您需要的索引.查询它的最简单方法是使用 LuceneQuery API.

As you said, there's already an index for what you need. The easiest way to query it is using the LuceneQuery API.

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));
}

这篇关于按实体名称和上次修改日期搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆