我遇到了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 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 pool = new StaticConnectionPool(nodes);
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"))
)
);
最终结果就是这样。观察正在创建的评论和审核映射。我只想要&#34;评论&#34;而不是&#34;评论&#34;。
{
"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 :(得分:0)
您尚未展示如何为Review
类型编制索引,但我怀疑您在编制索引时未将类型指定为"reviews"
;在这种情况下,NEST将从C#POCO类型名称推断出类型。
NEST提供了一种方法,使用"reviews"
Review
将Elasticsearch类型InferMappingFor<T>()
与C#POCO类型ConnectionSettings
相关联
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)
)
)
)
)
);
}
使用这种方法,您现在不需要在每个请求上指定类型,但是如果需要,您可以根据请求覆盖它。
With AutoMap()
, you can let NEST infer the Elasticsearch field types from the C# POCO property types,then use .Properties()
to override any mappings you'd like.