如何:使用c#和elasticsearch.net为elasticsearch的子项编制索引?
我的elasticsearch数据库有以下映射:如何:使用c#和elasticsearch.net为elasticsearch的子项编制索引?,c#,.net,visual-studio,
elasticsearch,nest,C#,.net,Visual Studio,
elasticsearch,Nest,我的elasticsearch数据库有以下映射: { "mappings": { "customer": { "properties": { "name": {"type": "string"}, "lastname": {"type": "string"}, "age": {"type": "date"}, "homet
{
"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"}
}
}
}
}
现在,我想使用VisualStudio中的客户端(elasticsearch.net)为我的孩子(购买)编制索引。我可以毫无问题地索引和升级我的父母(客户)。但我找不到一个方法来索引一个孩子
我得到了以下不完整的工作代码:
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));
使用Newtonsoft.Json;
使用Elasticsearch.Net;
命名空间程序名
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Class_GetData Class_GetData=新类_GetData();
//连接到elasticsearch(**正在工作**)
var node=新Uri(“http://localhost:9200");
var config=new-Elasticsearch.Net.Connection.ConnectionConfiguration(节点);
var client=新的ElasticsearchClient(配置);
//一些变量
字符串username=Class_GetData.Getname();
字符串userlastname=Class_GetData.Getlastname();
字符串userhomiance=Class_GetData.gethomiance();
字符串userstreet=Class_GetData.Getstreet()
字符串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=今天.Year+“-”+今天.Month+“-”+今天.Day;
var id=username+“\u”+userlastname+“\u”+日期;
//检查父级是否存在(这也在工作)
ElasticsearchResponse=client.Get(“测试”,“客户”,id);
动态发现;
response.response.TryGetValue(“find”,out-find);
如果(!found.HasValue)
{
返回;
}
如果(!found.Value)
{
var客户=新客户
{
name=用户名,
lastname=userlastname,
年龄=用户年龄,
家乡,
街道=用户街道,
nr=用户主页nr,
};
客户索引(“测试”、“客户”、id、客户);
}
//**也许这已经是假的了**
var purchaseproperties=new
{
时间=DateTime.Now.Ticks,
productnr1=userpruductnr1,
productnr2=userpruductnr2,
productnr3=userpruductnr3,
totalcost=usertotalcost,
};
//**这是主要的问题…孩子不会创造**
客户索引(“测试/客户/”,“购买”,purchaseproperties);
//工作线:
client.Index(“test”、“purchase”、purchaseproperties,d=>d.Parent(_parentId));
因此,现在我希望这段代码是自我解释的,有人可以帮助我。我确实尝试了最简单的方法,并且尝试了许多其他不起作用的索引行。我还希望有一种解决方案,可以在不使用visual studio的nest client的情况下将子项索引到父项。我相信在索引子项时缺少父项id参数文件:
client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));
另外请注意,我已将Index(..)
方法中的第一个参数从test/customer
更改为test
,您必须在此参数中仅指定索引名称,而不是使用类型指定索引
希望能有所帮助。您有意使用低级客户端而不是高级客户端(NEST)?我刚从那个低级别的客户端开始,希望代码尽可能简单。但是如果没有其他方法,我很高兴nest中的解决方案。Thx man。这很好。我知道我遗漏了一些东西,但我不知道如何处理。我仍然不知道“d=>d”是什么意思是说,但它正在工作。我将您的解决方案添加到我的代码中。太糟糕了,我不能给您一个向上投票。无论如何,thx。您可能想在谷歌上搜索“lambda expression”一词,也许这会对该主题有所帮助。