Reputation: 61
I am just trying to create an index using putMapping and am fairly certain this is a syntax issue. The elastic client is working for .index and .search commands, so I do not believe the setup of it is the issue. Rather it is something with my putmapping code. Any suggestions would be much appreciated. A little description of what I am trying to do. I have searched an index, and found that it does not exist. I catch the error and test to see if it equals "index_not_found_exception". If so, I need to create the index. Everything below the "..." is inside the .catch.
"use strict";
let config = require("./config"),
soap = require("soap"),
elastic = require("elasticsearch").Client(config.routeHost);
...
if (error.body.error.type == "index_not_found_exception") {
console.log("no index");
elastic.indices.putMapping({
index: "ppvevents",
type: "ppvObject",
body: {
properties: {
name: {type: "string", index: "not_analyzed"},
hdid: {type: "integer"},
sdid: {type: "integer"}
}
}
}).then(function(response){
console.log("Response: ", response);
}).catch(function(error){
console.log("putMapping Error: ", error);
});
} else if(error.body.error.type != "index_not_found_exception") {
console.log("error: elasticsearch client search");
console.log(error);
}
The console response is below:
vagrant at localhost in /vagrant on master!
± node index.js
9000
no index
putMapping Error: { [Error: [index_not_found_exception] no such index, with { resource.type=index_or_alias resource.id=ppvevents index=ppvevents }]
status: 404,
displayName: 'NotFound',
message: '[index_not_found_exception] no such index, with { resource.type=index_or_alias resource.id=ppvevents index=ppvevents }',
path: '/ppvevents/_mapping/ppvObject',
query: {},
body:
{ error:
{ root_cause: [Object],
type: 'index_not_found_exception',
reason: 'no such index',
'resource.type': 'index_or_alias',
'resource.id': 'ppvevents',
index: 'ppvevents' },
status: 404 },
statusCode: 404,
response: '{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"ppvevents","index":"ppvevents"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"ppvevents","index":"ppvevents"},"status":404}',
toString: [Function],
toJSON: [Function] }
Upvotes: 3
Views: 5823
Reputation: 5888
This answer is applicable to ElasticSearch 7.14. So, in order to create an index and also add an explicit mapping, you would need to invoke:
PUT http://localhost:9200/employee
with body:
{
"settings": {},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"role": {
"type": "keyword"
}
}
},
"aliases": {} }
Here, I have created an empty index named employee
with the given mappings. The same can be achieved through elasticsearch-rest-high-level-client
v7.14 with the below code:
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
CreateIndexRequest cir = new CreateIndexRequest("employee");
Map<String, Object> properties = new HashMap<>();
properties.put("properties", fieldMap);//fieldMap is another map containing the value for the `properties` key of the request body
cir.mapping(properties);
CreateIndexResponse cirs = esClient.indices().create(cir,RequestOptions.DEFAULT);
Upvotes: 0
Reputation: 1
new version of elasticsearch.js already removed the elastic.indices.createIndex
, you need to use client.indices.create
Upvotes: 0
Reputation: 217274
If the index does not exist, putMapping()
won't help, you need to call indices.create
instead. putMapping
can only be used to add a new mapping type to an already existing index.
Replace your putMapping call with this:
elastic.indices.createIndex({
index: "ppvevents",
body: {
mappings: {
ppvObject: {
properties: {
name: {type: "string", index: "not_analyzed"},
hdid: {type: "integer"},
sdid: {type: "integer"}
}
}
}
}
Upvotes: 3