RavenDB-使用;分析仪“;在;“动态查询”;

RavenDB-使用;分析仪“;在;“动态查询”;,ravendb,Ravendb,这与我问的另一篇关于如何不使用动态查询的帖子相反,但是为了准备我可能需要的想法,我正在尝试学习更多 我有这样的情况,有时我想分析一个字段,有时我不想。我现在通过使用两个单独的索引来解决这个问题 /// <summary> /// Query for an entity by its identity or name, using full indexing to search for specific parts of either. /// </summary> pub

这与我问的另一篇关于如何不使用动态查询的帖子相反,但是为了准备我可能需要的想法,我正在尝试学习更多

我有这样的情况,有时我想分析一个字段,有时我不想。我现在通过使用两个单独的索引来解决这个问题

/// <summary>
/// Query for an entity by its identity or name, using full indexing to search for specific parts of either.
/// </summary>
public class [ENTITY]__ByName : AbstractIndexCreationTask<[ENTITY]> {
    public [ENTITY]__ByName() {
        Map = results => from result in results
                         select new {
                             Id = result.Id,
                             Name = result.Name
                         };

        Index(n => n.Name, FieldIndexing.Analyzed);
    }
}

/// <summary>
/// Query for an entity by its full name, or full identity without any analyzed results, forcing
/// all matches to be absolutely identical.
/// </summary>
public class [ENTITY]__ByFullName : AbstractIndexCreationTask<[ENTITY]> {
    public [ENTITY]__ByFullName() {
        Map = results => from result in results
                         select new {
                             Id = result.Id,
                             Name = result.Name
                         };
    }
}
//
///通过实体的标识或名称查询实体,使用完整索引搜索实体的特定部分。
/// 
公共类[实体]\uuuu名称:AbstractIndexCreationTask{
公共[实体]\uuuu ByName(){
Map=results=>from result in results
选择新的{
Id=result.Id,
Name=result.Name
};
索引(n=>n.Name,fieldindex.analysis);
}
}
/// 
///按实体的全名或完整标识查询实体,但无任何分析结果,强制
///所有匹配都必须完全相同。
/// 
公共类[实体]\uuuu按全名:AbstractIndexCreationTask{
公共[实体]\uuuu ByFullName(){
Map=results=>from result in results
选择新的{
Id=result.Id,
Name=result.Name
};
}
}
然而,有人告诉我应该使用“动态索引”(对我来说,这违背了创建索引的目的,但这句话来自一位我非常尊重的高级开发人员,因此我很乐意接受)

所以,我需要弄清楚如何将analyzer中的首选项传递给动态查询。现在我的查询看起来像是

RavenQueryStatistics statistics;

var query = RavenSession
    .Query<[ENTITY], [INDEX]>()
    .Customize(c => c.WaitForNonStaleResultsAsOfNow())
    .Statistics(out statistics)
    .Search(r => r.Name, [name variable])
    .Skip((request.page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToList();

var totalResults = statistics.TotalResults;
RavenQueryStatistics;
var query=RavenSession
.Query()
.Customize(c=>c.WaitForNonSalesultsAsOffow())
.统计数字(外统计数字)
.Search(r=>r.Name,[Name变量])
.Skip((request.page-1)*request.PageSize)
.Take(请求.PageSize)
.ToList();
var totalResults=statistics.totalResults;
好吧,既然我被告知有这么多索引不是我应该做的,我需要转到动态查询?那么它会更像这样

RavenQueryStatistics statistics;

var query = RavenSession
    .Query<[ENTITY]>()
    .Customize(c => c.WaitForNonStaleResultsAsOfNow())
    .Statistics(out statistics)
    .Search(r => r.Name, [name variable])
    .Skip((request.page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToList();

var totalResults = statistics.TotalResults;
RavenQueryStatistics;
var query=RavenSession
.Query()
.Customize(c=>c.WaitForNonSalesultsAsOffow())
.统计数字(外统计数字)
.Search(r=>r.Name,[Name变量])
.Skip((request.page-1)*request.PageSize)
.Take(请求.PageSize)
.ToList();
var totalResults=statistics.totalResults;
但问题是,有时我需要一个
分析器,有时我不需要。例如

在6000个结果的网格中,如果用户对
One
进行“搜索”,我希望它能在名称中的任何位置找到
One
。分析仪允许这样做

在设计用于确保用户不会添加与另一个实体名称完全相同的新实体的验证器上,我不需要这种灵活性。如果用户正在键入
项目编号
作为名称,并且
项目编号2
存在,我不希望它匹配,因为其中一个或两个单词匹配。如果他们输入的是完全相同的单词,我希望它匹配


那么,有没有一种方法可以将其合并到
动态
查询中?还是我聪明地继续使用不同的查询?

只是为了确认;您想在运行时选择分析器(已分析与未分析),而不是作为静态索引定义的一部分?分析索引将提供您想要的内容(将单词分开进行搜索),因此您不需要同时定义分析索引和未分析索引,因为分析索引更密集,因此更灵活。