Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm trying to create an elasticsearch index with mappings using the official javascript client.

My code is as follows:

client.indices.create({
    index: "aName",
    "mappings": {
        "aType": {
            "properties": {
                "aProp1": { "type": "string", "index": "not_analyzed" },
                "aProp2": { "type": "string", "index": "not_analyzed" },
                "aProp3": { "type": "string", "index": "not_analyzed" },
                "aProp4": { "type": "string", "index": "not_analyzed" }
            }
        }
    }
}, function(err,resp,respcode){
    console.log(err,resp,respcode);
});

However... the index is created but without the mappings.... The output is:

undefined { ok: true, acknowledged: true } 200

What am I doing wrong?

share|improve this question
1  
Ok.... found it after many trial/error attempts... Guess I need to wrap the mappings "block" in a "body" property. Documentation is still laggin behind :-( – Sander Spilleman Mar 11 '14 at 13:04
1  
Thanks. I think you should post your comment as an answer and mark it as correct. It's easy to miss comments on SO, plus you would probably gather some points :) – Tadeusz Łazurski Apr 19 '14 at 9:47
    
How do you know that your mapping doesn't exist? Could you show result of executing GET on http://<host>:<port>/aName/_mapping/aType? – kopiczko Oct 2 '14 at 12:55
    
Thanks Sander, The documentation for ES is pretty but also lacks necessary information. – Forbesmyester Jan 27 '15 at 10:13

Adding the answer that is in the comment above from Sander Spilleman. The "mappings" property needs to be inside a "body" property. I am also using the Javascript client 1.3.0 and the docs are still not updated with an example.

Adding an example to worked for me with the javascript API provided by elasticsearch on NPM 1.3.0

var body = {
    tweet:{
        properties:{
            tag         : {"type" : "string", "index" : "not_analyzed"},
            type        : {"type" : "string", "index" : "not_analyzed"},
            namespace   : {"type" : "string", "index" : "not_analyzed"},
            tid         : {"type" : "string", "index" : "not_analyzed"}
        }
    }
}

client.indices.putMapping({index:"tweets", type:"tweet", body:body});
share|improve this answer
1  
would you mind submitting a pull request or filling an issue for methods which you think would benefit from docs? I'd be happy to add them – Spencer Alger Sep 8 '14 at 8:43
1  
@tim i think before put mapping we must call client.indices.create({ index: "aName", type: "tweet", body:body, }, function(err,resp,respcode){ console.log(err,resp,respcode); }); otherwise they will get the error indexnotexistexception – Sudhanshu Gaur Sep 22 '15 at 6:45

I tried same but got error from the name of index. aName is not valid, error was about the using lowercase index name. Then It created with mappings.

it.only('putMapping', function (done) {
    client.indices.create({
        index: "aname",
        body: {
            "mappings": {
                "aType": {
                    "properties": {
                        "aProp1": {"type": "string", "index": "not_analyzed"},
                        "aProp2": {"type": "string", "index": "not_analyzed"},
                        "aProp3": {"type": "string", "index": "not_analyzed"},
                        "aProp4": {"type": "string", "index": "not_analyzed"}
                    }
                }
            }
        }
    }, function (err, resp, respcode) {
        console.log(err, resp, respcode);
    });
})

Output:

Elasticsearch DEBUG: 2015-08-08T15:23:09Z
  starting request { method: 'POST',
    path: '/aname',
    body: { mappings: { aType: [Object] } },
    query: {} }


Elasticsearch TRACE: 2015-08-08T15:23:10Z
  -> POST http://localhost:9200/aname
  {
    "mappings": {
      "aType": {
        "properties": {
          "aProp1": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp2": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp3": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp4": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
  <- 200
  {
    "acknowledged": true
  }

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.