首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于属性的索引提示更改我的结果

问基于属性的索引提示更改我的结果
EN

Stack Overflow用户
提问于 2015-03-22 13:08:15
回答 1查看 403关注 0票数 1

自从我第一次使用它以来,我有一个没有改变的查询:

代码语言:javascript
复制
ISearchResponse<Series> response = await IndexManager.GetClient()
    .SearchAsync<Series>(r => r
        .Filter(f => f.Term<Role>(t => t.ReleasableTo.First(), Role.Visitor))
        .SortDescending(ser => ser.EndDate)
        .Size(1));

我的IndexManager.GetClient()只是负责设置到ElasticSearch的连接,并确保索引构建正确。代码的其余部分获得了可向公众发布的最新系列文章。

在IndexManager中,我设置了显式索引映射,当我这样做时,每次都从查询中获得结果。代码如下所示:

代码语言:javascript
复制
client.Map<Series>(m => m.Dynamic(DynamicMappingOption.Allow)
    .DynamicTemplates(t => t
        .Add(a => a.Name("releasableTo").Match("*releasableTo").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
        .Add(a => a.Name("id").Match("*id").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
        .Add(a => a.Name("services").Match("*amPm").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed)))
            .Match("*dayOfWeek").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
        .Add(a => a.Name("urls").Match("*Url").MatchMappingType("string").Mapping(map => map.String(s => s.Index(FieldIndexOption.NotAnalyzed))))
));

尽管所有这些都很好,但是对于我们存储的每一种类型来说,这样做都不是很好。因此,我有意识地决定使用这些属性并以这样的方式映射它:

代码语言:javascript
复制
// In IndexManager
client.Map<T>(m => m.MapFromAttributes());

// In the type definition
class Series
{
    // ....

    [DataMember]
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true)]
    public HashSet<Role> ReleasableTo { get; set; }

    // ....
}

一旦我这样做了,我就不能再得到结果了。当我查看我在Kibana中的索引时,我看到我的'releasableTo‘字段没有被分析并且被索引。但是,我编写的查询不再起作用了。如果删除filter子句,就会得到结果,但我确实需要这样做。

我遗漏了什么?如何让我的查询再次工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-26 00:31:56

提供索引提示的ElasticSearch属性似乎不知道如何处理enum。

事实证明,问题是Role类型是枚举。client.Map<Series>(m => m.MapFromAttributes())调用跳过了该属性。在运行时,它会动态地将属性映射到字符串。

代码语言:javascript
复制
// In the type definition
class Series
{
    // ....

    [DataMember]
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true)]
    public HashSet<Role> ReleasableTo { get; set; }

    // ....
}

为了使字段正确索引,我必须显式地设置它在ElasticProperty属性中的类型。将代码更改为:

代码语言:javascript
复制
// In the type definition
class Series
{
    // ....

    [DataMember]
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String, Store = true)]
    public HashSet<Role> ReleasableTo { get; set; }

    // ....
}

使我的查询再次生效。故事的寓意是,除非是原始类型,否则在设置字段类型时要明确。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29194750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档