正确设置映射ElasticSearch和NEST

时间:2015-12-09 09:22:19

标签: elasticsearch nest

我在NEST的映射中遇到了一些问题 让我来描述我的设置。

我将 ElasticClient 设置为我的ioc容器中的单身人员,如下所示:

ElasticClient client = new ElasticClient(settings);
                client.CreateIndex("elasticsearch", c =>
                    c
                        .AddMapping<ProductDocument>(m => m.MapFromAttributes())
                        .AddMapping<PageDocument>(m => m.MapFromAttributes())
                        .AddMapping<MediaAsset>(ma => ma.MapFromAttributes()));

ProductDocument PageDocument MediaAsset 都从 ContentDocument 继承属性。在这个问题中,我将专注于让 ProductDocument 工作。 (我已经遗漏了一些不遗余力的领域)

public class ContentDocument
    {
        public string Id { get; set; }
        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        public string DocumentKey { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        public string Language { get; set; }
        public string Url { get; set; }
    }

public class ProductDocument : ContentDocument
    {
        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        public string GroupId { get; set; }
    }

要为文档编制索引,我只需将值放在属性中并使用

_elasticClient.Index(product);

但是当我在ElasticSearch中检查映射时,我看到的是:

{
  "elasticsearch" : {
    "mappings" : {
    "productdocument" : {
      "properties" : {
        ...
        "groupId" : {
          "type" : "string"
        },
        "language" : {
          "type" : "string"
        }
      }
    }
  }
}

我希望language和groupId成为索引:&#34; NotAnalyzed&#34;。 我在这做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试类级别注释:

[ElasticType(Name = "ContentDocument")]
public class ContentDocument
{
    public string Id { get; set; }
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string DocumentKey { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string Language { get; set; }
    public string Url { get; set; }
}

[ElasticType(Name = "ProductDocument")]
public class ProductDocument : ContentDocument
{
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string GroupId { get; set; }
}
相关问题