elasticsearch 将同一类型嵌套映射两次
我和Nest有麻烦了。我在Elasticsearch 5.1.1上有一个全新的索引,我正试图通过dotnet core定义一个类型映射 我的课程是这样的:
elasticsearch 将同一类型嵌套映射两次,
elasticsearch,.net-core,nest,
elasticsearch,.net Core,Nest,我和Nest有麻烦了。我在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
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; }
}
在我的应用程序中,我试图定义一种简短的类型映射(仅用于测试),如下所示:
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; }
}
变量池=新的StaticConnectionPool(节点)
我做错了什么?如果我也使用AutoMap()会发生这种情况。我不想映射属性,因为我想保留我的POCO类,但如果这是唯一的方法,我可以这样做
有什么帮助吗?您没有说明如何索引
审阅
类型,但我怀疑您在索引时没有将类型指定为“审阅”
;在这种情况下,NEST将根据C#POCO类型名推断类型
NEST提供了一种方法,使Elasticsearch类型“reviews”
与C#POCO类型Review
关联,方法是使用ConnectionSettings
上的InferMappingFor()
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)
)
)
)
)
);
}
公共课复习
{
公共Guid Id{get;set;}
公共用户{get;set;}
公共电影{get;set;}
公共整数等级{get;set;}
公共字符串标题{get;set;}
公共字符串注释{get;set;}
公共布尔HasSpoiler{get;set;}
公共bool BoughtInIngresso{get;set;}
public ReviewStatus状态{get;set;}
公共日期时间日期{get;set;}
}
公共enum ReviewStatus
{
好,,
不好,,
衣衫褴褛
}
公共类用户
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
公映
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
void Main()
{
变量池=新的SingleNodeConnectionPool(新Uri(“http://localhost:9200"));
var defaultIndex=“默认索引”;
var connectionSettings=新连接设置(池)
.InferMappingFor(m=>m
.TypeName(“评论”)
)
.DefaultIndex(默认索引);
var客户端=新的ElasticClient(连接设置);
CreateIndex(“我的索引”,c=>c
.Mappings(m=>m
.Map(mm=>mm
.Properties(ps=>ps
.Keyword(k=>k
.Name(n=>n.Title)
)
.Text(t=>t
.Name(n=>n.Comment)
)
)
)
)
);
}
使用这种方法,您现在不需要在每个请求上指定类型,尽管如果需要,您可以在每个请求上重写它
,
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)
)
)
)
)
);
}