Reputation: 21
I have the following mapping for my elasticsearch database:
{
"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"}
}
}
}
}
Now I want to index my child(purchase) by using the client(elasticsearch.net) in visual studio. I can index and upgrade my parent(customer) without problems. But I don't find a way jet to index a child.
I got the following not complete working code :
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));
So now i hope this code is self explaining and someone can help me. I really tryed to make it the simplest way and i tryed a lot of other not working indexing lines.I also hope there is a solution to index a child to a parent without using the nest-client for visual studio.
Upvotes: 2
Views: 861
Reputation: 9979
I believe you are missing parent id parameter when indexing child document:
client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));
Also notice, I've changed first parameter in Index(..)
method from test/customer
to test
, You have to specify only index name in this parameter, not index with type.
Hope it helps.
Upvotes: 1