2017-06-06 155 views
0

我陷入困境与巢。我在Elasticsearch 5.1.1上有一个全新的索引,我试图用dotnet core定义一个类型映射。巢映射两次相同的类型

我的类看起来像:

public class Review 
{ 
    public Guid Id { get; set; } 
    public User User { get; set; } 
    public Movie Movie { get; set; } 
    public int Grade { get; set; } 
    public string Title { get; set; } 
    public string Comment { get; set; } 
    public bool HasSpoiler { get; set; } 
    public bool BoughtInIngresso { get; set; } 
    public ReviewStatus Status { get; set; } 
    public DateTime Date { get; set; } 
} 

public class User 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
    } 


public class Movie 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
    } 

在我的申请,我试图定义类型映射(只是为了测试)的缩写形式类似:

VAR池=新StaticConnectionPool(节点);

var settings = new ConnectionSettings(pool); 
    settings.DefaultIndex(elasticSettings.IndexName); 

    var client = new ElasticClient(settings); 

    client.Map<Review>(m => 
     m.Index("my_index") 
     .Type("reviews") 
     .Properties(ps=> 
      ps.Keyword(k=> 
        k.Name("title")) 
      .Text(t=> 
        t.Name("comment")) 
     ) 
    ); 

而最终的结果是这样的。观察正在创建的评论和评论映射。我只想要“评论”,而不是“评论”。

{ 
    "my_index": { 
    "mappings": { 
     "reviews": { 
     "properties": { 
      "comment": { 
      "type": "text" 
      }, 
      "title": { 
      "type": "keyword" 
      } 
     } 
     }, 
     "review": { 
     "properties": { 
      "boughtInSite": { 
      "type": "boolean" 
      }, 
      "comment": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "date": { 
      "type": "date" 
      }, 
      "grade": { 
      "type": "long" 
      }, 
      "hasSpoiler": { 
      "type": "boolean" 
      }, 
      "id": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "movie": { 
      "properties": { 
       "id": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       }, 
       "name": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       } 
      } 
      }, 
      "status": { 
      "type": "long" 
      }, 
      "title": { 
      "type": "keyword" 
      }, 
      "user": { 
      "properties": { 
       "id": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       }, 
       "name": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我在做什么错了?如果我使用AutoMap(),也会发生这种情况。我不想映射属性,因为我想保留我的POCO类,但如果是唯一的方法,我可以做。

有帮助吗?

回答

0

您尚未显示如何为Review类型建立索引,但我怀疑在索引时没有将类型指定为"reviews";在这种情况下,NEST将从C#POCO类型名称中推断出该类型。

NEST提供要使用InferMappingFor<T>() C#的POCO类型ReviewConnectionSettings

public class Review 
{ 
    public Guid Id { get; set; } 
    public User User { get; set; } 
    public Movie Movie { get; set; } 
    public int Grade { get; set; } 
    public string Title { get; set; } 
    public string Comment { get; set; } 
    public bool HasSpoiler { get; set; } 
    public bool BoughtInIngresso { get; set; } 
    public ReviewStatus Status { get; set; } 
    public DateTime Date { get; set; } 
} 

public enum ReviewStatus 
{ 
    Good, 
    Bad, 
    NotTooShabby 
} 

public class User 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
} 


public class Movie 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
} 

void Main() 
{ 
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); 
    var defaultIndex = "default-index"; 
    var connectionSettings = new ConnectionSettings(pool) 
     .InferMappingFor<Review>(m => m 
      .TypeName("reviews") 
     ) 
     .DefaultIndex(defaultIndex); 

    var client = new ElasticClient(connectionSettings); 

    client.CreateIndex("my_index", c => c 
     .Mappings(m => m 
      .Map<Review>(mm => mm 
       .Properties(ps => ps 
        .Keyword(k => k 
         .Name(n => n.Title) 
        ) 
        .Text(t => t 
         .Name(n => n.Comment) 
        ) 
       ) 
      ) 
     ) 
    ); 
} 

与此相关的方法为Elasticsearch型"reviews"的方式,你现在没有必要指定每个请求的类型,但如果需要,您可以根据请求覆盖它。

With AutoMap(), you can let NEST infer the Elasticsearch field types from the C# POCO property typesthen use .Properties() to override any mappings you'd like.

相关问题