映射 - ElasticSearch [7+]
Mapping - ElasticSearch [7+]
我已经解决了很多这个问题我现在想知道问题是什么以及我可能做错了什么。
我打算使用 NEST 重新创建的控制台查询是:
{
"tdindex" : {
"mappings" : {
"documentModel" : {
"properties" : {
"accessControl" : {
"type" : "boolean",
"copy_to" : [
"copyTo"
]
},
"comments" : {
"properties" : {
"comment" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]},
"createDate" : {
"type" : "date",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
},
"copyTo" : {
"type" : "text"
},
"documentType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"filename" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"folderID" : {
"type" : "long",
"copy_to" : [
"copyTo"
]
},
"metadata" : {
"properties" : {
"key" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"value" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
},
"uploadDate" : {
"type" : "date",
"copy_to" : [
"copyTo"
]
},
"uploadUser" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
}
}
}
}
我目前正在创建以下控制台查询:
{
"mappings": {
"properties": {
"folderid": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"filename": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"documenttype": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"uploaddate": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"uploaduser": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"comments": {
"properties": {
"createdate": {
"type": "date"
},
"comment": {
"type": "text"
},
"user": {
"type": "text"
}
},
"copy_to": [
"CopyTo"
],
"type": "nested"
},
"metadata": {
"properties": {
"key": {
"type": "text"
},
"value": {
"type": "text"
}
},
"copy_to": [
"CopyTo"
],
"type": "nested"
},
"CopyTo": {
"type": "text"
}
}
}
}
如您所见,它遗漏了 DocumentModel…
我使用的NEST代码是:
_elasticClient.CreateIndex(indexParameters.IndexName, c =>
{
c.Map<DocumentModel>(m => m
.Properties(ps => ps
.Text(t => t.Name(n => n.FolderID).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.Filename).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.DocumentType).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.UploadDate).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.uploadUser).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Nested<Comments>(cm => cm.Name(n => n.Comments).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
.Nested<Metadata>(md => md.Name(n => n.Metadata).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
.Text(t => t.Name(n => n.CopyTo)))
我的 DocumentModel 看起来像这样(出于某种原因我无法在模型中使用 [ElasticsearchType(Name = "documentModel")] 不确定这是否是问题的一部分?):
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "CopyTo")]
public string CopyTo { get; set; }
}
public class Comments
{
[Date(Format = "MMddyyyy")]
public DateTime CreateDate { get; set; }
[Text(Name = "comment")]
public string Comment { get; set; }
[Text(Name = "user")]
public string User { get; set; }
}
public class Metadata
{
[Text(Name = "key")]
public string Key { get; set; }
[Text(Name = "value")]
public string Value { get; set; }
}
当 运行 查询时,我从控制台得到的错误是:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]"
}
],
"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [properties]: Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]",
"caused_by": {
"type": "mapper_parsing_exception",
"reason": "Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]"
}
},
"status": 400
有什么想法吗?
我假设问题是由于地图引起的?
P.S。我是本地主机上的 运行 Kibana 6.7.1 和 Elastic 6.7.1。我是 运行 查询(来自使用 NEST 请求创建的 JSON 对象)
对于 Elasticsearch 6.7.1,请使用最新的 6.x NEST 客户端,即 is 6.7.0 at this time。客户端的主要版本与 Elasticsearch 的主要版本兼容。
对于 NEST 6.7.0,映射类似于
private static void Main()
{
var defaultIndex = "tdindex";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
var visitor = new MyVisitor();
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<DocumentModel>(mm => mm
.AutoMap(visitor)
.Properties(ps => ps
.Nested<Comments>(cm => cm
.Name(n => n.Comments)
.AutoMap(visitor)
)
.Nested<Metadata>(md => md
.Name(n => n.Metadata)
.AutoMap(visitor)
)
.Text(t => t.Name(n => n.CopyTo))
)
)
)
);
}
public class MyVisitor : NoopPropertyVisitor
{
public override void Visit(ITextProperty property, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
{
base.Visit(property, propertyInfo, attribute);
property.CopyTo = Infer.Fields<DocumentModel>(f => f.CopyTo);
property.Fields = new Properties
{
{ "keyword", new KeywordProperty { IgnoreAbove = 256 } }
};
}
}
[ElasticsearchType(Name = "documentModel")]
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "copyTo")]
public string CopyTo { get; set; }
}
public class Comments
{
[Date(Format = "MMddyyyy")]
public DateTime CreateDate { get; set; }
[Text(Name = "comment")]
public string Comment { get; set; }
[Text(Name = "user")]
public string User { get; set; }
}
public class Metadata
{
[Text(Name = "key")]
public string Key { get; set; }
[Text(Name = "value")]
public string Value { get; set; }
}
因为除了CopyTo
属性之外的所有文本映射都有关键字multi_fields和copy_to复制到CopyTo
字段,最简单的方法到 define this is with a visitor。首先,调用 Automap()
,传递访问者。自动映射将获取模型上的属性映射,而访问者的 Visit 方法将允许我们覆盖其中的任何一个。接下来,Properties()
将覆盖自动映射过程中的所有映射。
最终输出映射为
PUT http://localhost:9200/tdindex?pretty=true
{
"mappings": {
"documentModel": {
"properties": {
"filename": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"folderid": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uploaduser": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uploadDate": {
"type": "date",
"format": "MMddyyyy"
},
"documenttype": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"accessControl": {
"type": "boolean",
"null_value": false
},
"comments": {
"type": "nested",
"properties": {
"createDate": {
"type": "date",
"format": "MMddyyyy"
},
"comment": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"metadata": {
"type": "nested",
"properties": {
"key": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"copyTo": {
"type": "text"
}
}
}
}
}
与 Elasticsearch 7.x 兼容的 NEST 7.x 语法相同;有一些过时的东西 可以 更改以删除警告,但 6.x 语法按原样工作
client.CreateIndex(defaultIndex, c => c
// remove .Mappings()
.Map<DocumentModel>(mm => mm
.AutoMap(visitor)
.Properties(ps => ps
.Nested<Comments>(cm => cm
.Name(n => n.Comments)
.AutoMap(visitor)
)
.Nested<Metadata>(md => md
.Name(n => n.Metadata)
.AutoMap(visitor)
)
.Text(t => t.Name(n => n.CopyTo))
)
)
);
// Use RelationName instead of Name
[ElasticsearchType(RelationName = "documentModel")]
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "copyTo")]
public string CopyTo { get; set; }
}
我已经解决了很多这个问题我现在想知道问题是什么以及我可能做错了什么。
我打算使用 NEST 重新创建的控制台查询是:
{
"tdindex" : {
"mappings" : {
"documentModel" : {
"properties" : {
"accessControl" : {
"type" : "boolean",
"copy_to" : [
"copyTo"
]
},
"comments" : {
"properties" : {
"comment" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]},
"createDate" : {
"type" : "date",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
},
"copyTo" : {
"type" : "text"
},
"documentType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"filename" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"folderID" : {
"type" : "long",
"copy_to" : [
"copyTo"
]
},
"metadata" : {
"properties" : {
"key" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
},
"value" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
},
"uploadDate" : {
"type" : "date",
"copy_to" : [
"copyTo"
]
},
"uploadUser" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"copyTo"
]
}
}
}
}
}
}
我目前正在创建以下控制台查询:
{
"mappings": {
"properties": {
"folderid": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"filename": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"documenttype": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"uploaddate": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"uploaduser": {
"copy_to": [
"CopyTo"
],
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"comments": {
"properties": {
"createdate": {
"type": "date"
},
"comment": {
"type": "text"
},
"user": {
"type": "text"
}
},
"copy_to": [
"CopyTo"
],
"type": "nested"
},
"metadata": {
"properties": {
"key": {
"type": "text"
},
"value": {
"type": "text"
}
},
"copy_to": [
"CopyTo"
],
"type": "nested"
},
"CopyTo": {
"type": "text"
}
}
}
}
如您所见,它遗漏了 DocumentModel…
我使用的NEST代码是:
_elasticClient.CreateIndex(indexParameters.IndexName, c =>
{
c.Map<DocumentModel>(m => m
.Properties(ps => ps
.Text(t => t.Name(n => n.FolderID).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.Filename).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.DocumentType).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.UploadDate).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Text(t => t.Name(n => n.uploadUser).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
.Nested<Comments>(cm => cm.Name(n => n.Comments).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
.Nested<Metadata>(md => md.Name(n => n.Metadata).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
.Text(t => t.Name(n => n.CopyTo)))
我的 DocumentModel 看起来像这样(出于某种原因我无法在模型中使用 [ElasticsearchType(Name = "documentModel")] 不确定这是否是问题的一部分?):
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "CopyTo")]
public string CopyTo { get; set; }
}
public class Comments
{
[Date(Format = "MMddyyyy")]
public DateTime CreateDate { get; set; }
[Text(Name = "comment")]
public string Comment { get; set; }
[Text(Name = "user")]
public string User { get; set; }
}
public class Metadata
{
[Text(Name = "key")]
public string Key { get; set; }
[Text(Name = "value")]
public string Value { get; set; }
}
当 运行 查询时,我从控制台得到的错误是:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]"
}
],
"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [properties]: Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]",
"caused_by": {
"type": "mapper_parsing_exception",
"reason": "Root mapping definition has unsupported parameters: [metadata : {copy_to=[CopyTo], type=nested, properties={value={type=text}, key={type=text}}}] [filename : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [comments : {copy_to=[CopyTo], type=nested, properties={createdate={type=date}, comment={type=text}, user={type=text}}}] [uploaddate : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [CopyTo : {type=text}] [documenttype : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [folderid : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}] [uploaduser : {copy_to=[CopyTo], fields={keyword={ignore_above=256, type=keyword}}, type=text}]"
}
},
"status": 400
有什么想法吗?
我假设问题是由于地图引起的?
P.S。我是本地主机上的 运行 Kibana 6.7.1 和 Elastic 6.7.1。我是 运行 查询(来自使用 NEST 请求创建的 JSON 对象)
对于 Elasticsearch 6.7.1,请使用最新的 6.x NEST 客户端,即 is 6.7.0 at this time。客户端的主要版本与 Elasticsearch 的主要版本兼容。
对于 NEST 6.7.0,映射类似于
private static void Main()
{
var defaultIndex = "tdindex";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
var visitor = new MyVisitor();
client.CreateIndex(defaultIndex, c => c
.Mappings(m => m
.Map<DocumentModel>(mm => mm
.AutoMap(visitor)
.Properties(ps => ps
.Nested<Comments>(cm => cm
.Name(n => n.Comments)
.AutoMap(visitor)
)
.Nested<Metadata>(md => md
.Name(n => n.Metadata)
.AutoMap(visitor)
)
.Text(t => t.Name(n => n.CopyTo))
)
)
)
);
}
public class MyVisitor : NoopPropertyVisitor
{
public override void Visit(ITextProperty property, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
{
base.Visit(property, propertyInfo, attribute);
property.CopyTo = Infer.Fields<DocumentModel>(f => f.CopyTo);
property.Fields = new Properties
{
{ "keyword", new KeywordProperty { IgnoreAbove = 256 } }
};
}
}
[ElasticsearchType(Name = "documentModel")]
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "copyTo")]
public string CopyTo { get; set; }
}
public class Comments
{
[Date(Format = "MMddyyyy")]
public DateTime CreateDate { get; set; }
[Text(Name = "comment")]
public string Comment { get; set; }
[Text(Name = "user")]
public string User { get; set; }
}
public class Metadata
{
[Text(Name = "key")]
public string Key { get; set; }
[Text(Name = "value")]
public string Value { get; set; }
}
因为除了CopyTo
属性之外的所有文本映射都有关键字multi_fields和copy_to复制到CopyTo
字段,最简单的方法到 define this is with a visitor。首先,调用 Automap()
,传递访问者。自动映射将获取模型上的属性映射,而访问者的 Visit 方法将允许我们覆盖其中的任何一个。接下来,Properties()
将覆盖自动映射过程中的所有映射。
最终输出映射为
PUT http://localhost:9200/tdindex?pretty=true
{
"mappings": {
"documentModel": {
"properties": {
"filename": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"folderid": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uploaduser": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uploadDate": {
"type": "date",
"format": "MMddyyyy"
},
"documenttype": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"accessControl": {
"type": "boolean",
"null_value": false
},
"comments": {
"type": "nested",
"properties": {
"createDate": {
"type": "date",
"format": "MMddyyyy"
},
"comment": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"metadata": {
"type": "nested",
"properties": {
"key": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"copy_to": [
"copyTo"
],
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"copyTo": {
"type": "text"
}
}
}
}
}
与 Elasticsearch 7.x 兼容的 NEST 7.x 语法相同;有一些过时的东西 可以 更改以删除警告,但 6.x 语法按原样工作
client.CreateIndex(defaultIndex, c => c
// remove .Mappings()
.Map<DocumentModel>(mm => mm
.AutoMap(visitor)
.Properties(ps => ps
.Nested<Comments>(cm => cm
.Name(n => n.Comments)
.AutoMap(visitor)
)
.Nested<Metadata>(md => md
.Name(n => n.Metadata)
.AutoMap(visitor)
)
.Text(t => t.Name(n => n.CopyTo))
)
)
);
// Use RelationName instead of Name
[ElasticsearchType(RelationName = "documentModel")]
public class DocumentModel
{
[Text(Name = "filename")]
public string Filename { get; set; }
[Text(Name = "folderid")]
public int FolderID { get; set; }
[Text(Name = "uploaduser")]
public string uploadUser { get; set; }
[Date(Format = "MMddyyyy")]
public DateTime UploadDate { get; set; }
[Text(Name = "documenttype")]
public string DocumentType { get; set; }
[Boolean(NullValue = false)]
public bool AccessControl { get; set; }
[Nested]
public List<Comments> Comments { get; set; }
[Nested]
public List<Metadata> Metadata { get; set; }
[Text(Name = "copyTo")]
public string CopyTo { get; set; }
}