我尝试使用node.js在弹性搜索中创建索引后尝试更新索引的设置和映射。当我尝试更新索引中的映射和设置值时,它不会更改。它仍保留在默认设置中。请建议我解决这个问题的方法。我在这里提到编码。
fs.readFile('./article_settings.json', 'utf8', function (err, data) {
if (err) throw err;
settingsValue = JSON.parse(data);
console.log("settings Json");
console.log(settingsValue);
return elasticClient.indices.putMapping({
index: indexname,
type : type_name,
body: { "settings" : settingsValue}
});
});
fs.readFile('./article_mapping.json', 'utf8', function (err, data) {
if (err) throw err;
mapping = JSON.parse(data);
console.log("Mapping Json");
console.log(mapping);
return elasticClient.indices.putMapping({
index: indexname,
type : type_name,
body: {
"mappings":{
"articles" :{
"properties" : mapping
}
}
}
});
产生错误就像。
> Unhandled rejection Error: IndexMissingException[[mhcarticle] missing]
at respond (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/transport.js:264:15)
at checkRespForFailure (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/transport.js:223:7)
at HttpConnector.<anonymous> (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/connectors/http.js:155:7)
at IncomingMessage.wrapper (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/node_modules/lodash/index.js:3095:19)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Elasticsearch DEBUG: 2016-02-25T13:13:48Z
Request complete
Elasticsearch DEBUG: 2016-02-25T13:13:48Z
Request complete
Unhandled rejection Error: MapperParsingException[Root type mapping not empty after parsing! Remaining fields: [mappings : {articles={properties={id={type=string, index=not_analyzed}, refference_id={type=string, index=not_analyzed}, key={type=string, index=not_analyzed}, url={type=string, index=not_analyzed}, created_on={type=string, index=not_analyzed}, status={type=string, index=not_analyzed}, title={type=string, index=not_analyzed}, description={type=string, index=not_analyzed}, batch_id={type=string, index=not_analyzed}, thumbnail={type=string, index=not_analyzed}, reference_key_id={type=string, index=not_analyzed}}}}]]
at respond (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/transport.js:264:15)
at checkRespForFailure (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/transport.js:223:7)
at HttpConnector.<anonymous> (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/src/lib/connectors/http.js:155:7)
at IncomingMessage.wrapper (/home/100095/nodesamp/MHC-Search/node_modules/elasticsearch/node_modules/lodash/index.js:3095:19)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
请让我知道如何解决这个问题。谢谢。
根据评论更改代码后,它看起来像:
function initMapping(indexname,type_name){
var fs = require('fs');
var mapping;
console.log(type_name);
if(type_name == "articles"){
fs.readFile('./article_settings.json', 'utf8', function (err, data) {
if (err) throw err;
settingsValue = JSON.parse(data);
console.log("settings Json");
console.log(settingsValue);
elasticClient.indices.close(indexname);
return elasticClient.indices.putSettings({
index: indexname,
type : type_name,
body: { "settings" : settingsValue}
});
});
elasticClient.indices.open(indexname);
fs.readFile('./article_mapping.json', 'utf8', function (err, data) {
if (err) throw err;
mapping = JSON.parse(data);
console.log("Mapping Json");
console.log(mapping);
return elasticClient.indices.putMapping({
index: indexname,
type : type_name,
body: {
"mappings":{
"articles" :{
"properties" : mapping
}
}
}
});
});
}
}