32

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?

4
  • 5
    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 :-( Commented Mar 11, 2014 at 13:04
  • 2
    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 :) Commented Apr 19, 2014 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? Commented Oct 2, 2014 at 12:55
  • Thanks Sander, The documentation for ES is pretty but also lacks necessary information. Commented Jan 27, 2015 at 10:13

6 Answers 6

33

Refining the answer that is in the comment above from Sander Spilleman. The "mappings" property needs to be inside the "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 that 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});
Sign up to request clarification or add additional context in comments.

3 Comments

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
@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
@SpencerAlger What makes the ElasticSearch docs hard to use right now is there aren't any full examples in the client specific docs. So I'm bouncing back and forth between the client docs and the generic API docs and it's basically guesswork to determine how to correctly map the parameters.
16

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

Comments

11

Just add body around mappings:

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);
});

Comments

11

None of these examples worked for me on ElasticSearch 5.3 API.

Here is an example that works for 5.3.

elasticClient.indices.putMapping({
    index: indexName,
    type: "document",
    body: {
        properties: {
            title: { type: "string" },
            content: { type: "string" },
            suggest: {
                type: "completion",
                analyzer: "simple",
                search_analyzer: "simple",
                payloads: true
            }
        }
    }
})

Note that the type has been pulled out of the body, and only the sub-params that were under the type are now in the body.

Source: https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/

3 Comments

Thanks a lot for your comment this is the right answer
how can I create a index with multiple types
but as of 7.3 you need to add include_type_name:true
2

Note: this uses client.indices.create() and not client.indices.putMapping()

I recently succeeded in creating an index with a custom mapping like this:

client.indices.create({
  index: 'yourIndex',
  body: {
    yourIndex: {
      mappings: {
        yourType: {
          properties: {
            yourText: {
              type: 'string',
            }
          }
        }
      }
    }
  }
});

It seems you have to start defining the body with your index, followed by the mappings keyword, followed by your type and so forth. I used the elasticsearch package version 15.4.1 with elastic version 6.5.4

Comments

1

Body property is deprecated after version 8.2.1. Mapping should be set directly in parameters.

 await client.indices.create({
    index: "user",
    mappings: {
        properties: {
            "name": {
                type: "text",
                index: true
            },
            "surname": {
                type: "text",
                index: true
            }
        }
    }
});

Comments

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.