如何:用c#和elasticsearch.net为elasticsearch索引子节点
本文关键字:elasticsearch net 索引 子节点 如何 | 更新日期: 2023-09-27 18:17:17
我有以下映射为我的elasticsearch数据库:
{
"mappings": {
"customer": {
"properties": {
"name": {"type": "string"},
"lastname": {"type": "string"},
"age": {"type": "date"},
"hometown": {"type": "string"},
"street": {"type": "string"},
"nr": {"type": "integer"}
}
},
"purchase": {
"_parent": {"type": "customer"},
"properties": {
"time": {"type": "long"},
"productnr1": {"type": "integer"},
"productnr2": {"type": "integer"},
"productnr3": {"type": "integer"},
"totalcost": {"type": "double"}
}
}
}
}
现在我想索引我的孩子(购买)通过使用客户端(elasticsearch.net)在visual studio。我可以索引和升级我的父母(客户)没有问题。但我找不到索引子节点的方法。
我得到了以下不完整的工作代码:
using Newtonsoft.Json;
using Elasticsearch.Net;
namespace programname
{
static class program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Class_GetData Class_GetData = new Class_GetData();
//connecting to elasticsearch (**this is working**)
var node = new Uri("http://localhost:9200");
var config = new Elasticsearch.Net.Connection.ConnectionConfiguration(node);
var client = new ElasticsearchClient(config);
//some variables
string username = Class_GetData.Getname();
string userlastname = Class_GetData.Getlastname();
string userhometown = Class_GetData.Gethometown();
string userstreet = Class_GetData.Getstreet()
string userage = Class_GetData.Getage()
int userhomenr = Class_GetData.Getnr();
int userpruductnr1 = Class_GetData.Getproductnr1();
int userpruductnr2 = Class_GetData.Getproductnr2();
int userpruductnr3 = Class_GetData.Getproductnr3();
double usertotalcost = Class_GetData.Gettotalcost();
var today = DateTime.Today;
var date = today.Year + "-" + today.Month + "-" + today.Day;
var id = username + "_" + userlastname + "_" + date;
//check if parent exist (this is also working)
ElasticsearchResponse<DynamicDictionary> response = client.Get("test", "customer", id);
dynamic found;
response.Response.TryGetValue("found", out found);
if (!found.HasValue)
{
return;
}
if (!found.Value)
{
var customer = new
{
name = username,
lastname = userlastname,
age = userage,
hometown = userhometown,
street = userstreet,
nr = userhomenr,
};
client.Index("test", "customer", id, customer);
}
//**maybe this is already false?**
var purchaseproperties = new
{
time = DateTime.Now.Ticks,
productnr1 = userpruductnr1,
productnr2 = userpruductnr2,
productnr3 = userpruductnr3,
totalcost = usertotalcost,
};
//**this is the main problem... child wont create**
client.Index("test/customer/", "purchase", purchaseproperties);
//working line:
client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));
所以现在我希望这个代码是自我解释和有人可以帮助我。我真的试图使它最简单的方式,我尝试了很多其他不工作的索引行。我也希望有一个解决方案,索引一个孩子的父母不使用巢客户端为visual studio。
我认为您在索引子文档时缺少父id参数:
client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));
还请注意,我已经将Index(..)
方法中的第一个参数从test/customer
更改为test
,您必须在此参数中仅指定索引名称,而不是索引类型。
希望能有所帮助。