【发布时间】:2017-03-14 13:31:07
【问题描述】:
我的 ElasticSearch 索引中有一个列表。如何使用 NEST 2 向列表中添加新元素?我已经搜索了我的问题的答案,但我找到的都是answer for the version 1.x,它对我不起作用。
型号:
public class TicketModel
{
public Guid Id { get; set; }
public Guid IdUser { get; set; }
public Guid IdAuthor { get; set; }
public Guid? IdAssignedUser { get; set; }
public Guid? IdAsset { get; set; }
public Guid? IdOwner { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? ClosingDate { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? LastDateUserView { get; set; }
public DateTime? LastDateAssignedUserView { get; set; }
public string TicketCode { get; set; }
public string Title { get; set; }
public int IdCategory { get; set; }
public int? IdSubcategory { get; set; }
public short? IdPriority { get; set; }
public short IdState { get; set; }
public bool IsNew { get; set; }
public List<TicketMessageModel> TicketMessageList { get; set; }
}
public class TicketMessageModel
{
public Guid Id { get; set; }
public Guid IdTicket { get; set; }
public Guid IdUserFrom { get; set; }
public Guid? IdUserTo { get; set; }
public DateTime? CreationDate { get; set; }
public DateTime? DeleteDate { get; set; }
public string Message { get; set; }
public bool HideToFinalUser { get; set; }
public byte? MessageType { get; set; }
public List<TicketMessageFilesModel> MessageFileList { get; set; }
}
public class TicketMessageFilesModel
{
public Guid Id { get; set; }
public Guid? IdTicketMessage { get; set; }
public string FileName { get; set; }
public string FileTitle { get; set; }
public string FileOriginalName { get; set; }
public byte FileType { get; set; }
}
我试图使用其他答案找到出路,但我被困在这里:
client.Update<ElasticSearchTickets.TicketMessageModel, object>(new DocumentPath<ElasticSearchTickets.TicketMessageModel>(elem.Id),
q => q.Script(x => x.Inline("ctx._source.MessageFileList += elem"))./*??*/);
提前致谢。
编辑:
这是我尝试在列表中插入新元素的代码:
ElasticSearchTickets.TicketMessageModel elem = new ElasticSearchTickets.TicketMessageModel()
{
CreationDate = message.CreationDate,
DeleteDate = message.DeleteDate,
HideToFinalUser = message.HideToFinalUser,
Id = message.Id,
IdTicket = message.IdTicket,
IdUserFrom = message.IdUserFrom,
IdUserTo = message.IdUserTo,
Message = message.Message,
MessageType = message.MessageType,
MessageFileList = tempList
};
var response = client.Update<ElasticSearchTickets.TicketModel, object>(new DocumentPath<ElasticSearchTickets.TicketModel>(elem.IdTicket.ToString()), q => q
.Script(s => s
.Inline("if (ctx._source.TicketMessageList == null) { ctx._source.TicketMessageList = element; } else { ctx._source.TicketMessageList += element; }")
.Params(d => d.Add("element", new [] { elem }))
));
但现在我收到了服务器错误,因为尚未声明属性“元素”。如何解决这个问题?
这是映射:
"mappings": {
"ticket": {
"properties": {
"ClosingDate": {
"type": "date"
},
"CreationDate": {
"type": "date"
},
"DueDate": {
"type": "date"
},
"Id": {
"type": "keyword"
},
"IdAsset": {
"type": "keyword"
},
"IdAssignedUser": {
"type": "keyword"
},
"IdAuthor": {
"type": "keyword"
},
"IdCategory": {
"type": "integer"
},
"IdOwner": {
"type": "keyword"
},
"IdPriority": {
"type": "short"
},
"IdState": {
"type": "short"
},
"IdSubcategory": {
"type": "integer"
},
"IdUser": {
"type": "keyword"
},
"IsNew": {
"type": "boolean"
},
"LastDateAssignedUserView": {
"type": "date"
},
"LastDateUserView": {
"type": "date"
},
"TicketCode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"TicketMessageList": {
"type": "nested",
"properties": {
"CreationDate": {
"type": "date"
},
"DeleteDate": {
"type": "date"
},
"HideToFinalUser": {
"type": "boolean"
},
"Id": {
"type": "keyword"
},
"IdTicket": {
"type": "keyword"
},
"IdUserFrom": {
"type": "keyword"
},
"IdUserTo": {
"type": "keyword"
},
"Message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"MessageFileList": {
"properties": {
"FileName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FileOriginalName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FileTitle": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FileType": {
"type": "short"
},
"Id": {
"type": "keyword"
},
"IdTicketMessage": {
"type": "keyword"
}
}
},
"MessageType": {
"type": "short"
}
}
},
"Title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
【问题讨论】:
-
映射是什么样的?
TicketMessageList是映射为object类型还是nested类型? -
还有
MessageFileList的映射 -
@RussCam TicketMessageList 是一个嵌套类型,MessageFileList 是一个对象。
标签: c# asp.net-mvc elasticsearch nest