基于属性的索引提示会改变我的结果

时间:2015-03-22 13:08:15

标签: elasticsearch elasticsearch-net

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

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内部我设置了显式索引映射,当我这样做时,我每次都从查询中得到结果。代码看起来像这样:

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

虽然一切都很好,但对于我们存储的每种类型都这样做并不是真的能够很好地扩展。所以我有意识地决定使用这些属性并以这种方式映射:

// 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'字段未被分析并被索引。但是我写的查询不再有效。如果我删除过滤条款,我会得到结果,但我真的需要这样才能工作。

我错过了什么?如何让我的查询再次运行?

1 个答案:

答案 0 :(得分:3)

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

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

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

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

    // ....
}

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

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

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

    // ....
}

让我的查询再次运行。这个故事的寓意是,除非它是一个原始类型,否则在设置字段类型时要明确。

相关问题