2

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.

2
  • It's intentional that you use low level client instead of high level(NEST)? Commented Oct 13, 2015 at 10:29
  • I just started with that low lvl client and want to keep that code as simplel as possible. But if there is no other way, im happy with an sulution in nest. Commented Oct 13, 2015 at 10:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thx man. This worked out well. I was knowing i was missing something, but i dont know how to handel that. I still dont know what "d => d" means but it is working.
I added your solution to my code. too bad i can't give you an upvote. thx anyway.
You may want to google for "lambda expression" term, maybe it will put some light on topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.