如何解决如何在创建索引时向索引添加设置和映射
我想索引一个包含世界城市名称的 .json 文件。 我在创建索引时有我的自定义设置和映射。 我的代码是:
const elasticsearchLoading = require("elasticsearch");
const indexNameLoading = "cities";
const dataJsonFile = "./cities.json";
const loadindexclient = new elasticsearchLoading.Client({
hosts: ["http://localhost:9200"],});
// create a new index
loadindexclient.indices.create({
index: indexNameLoading,},function (error,response,status) {
if (error) {
console.log(error);
}
else {
console.log("created a new index",response);
}
});
// add 1 data to the index that has already been created
loadindexclient.index({
index: indexNameLoading,type: "cities_list",body: {
name: "Content for key one"
},status) {
console.log(response);
});
// require the array of cities that was downloaded
const cities = require(dataJsonFile);
// declare an empty array called bulk
let bulk = [];
cities.forEach((city) => {
bulk.push({
index: {
_index: indexNameLoading,_type: "cities_list",});
bulk.push(city);
});
//perform bulk indexing of the data passed
loadindexclient.bulk({ body: bulk },function (err,response) {
if (err) {
// @ts-ignore
console.log("Failed Bulk operation".red,err);
}
else {
// @ts-ignore
console.log("Successfully imported ",cities.length);
}
});
当我运行此代码时,它实际上会运行并创建索引,但默认情况下会创建映射和设置。但我希望在创建索引时添加以下设置和映射。
"settings": {
"analysis": {
"filter": {
"my_ascii_folding": {
"type": "asciifolding","preserve_original": true
}
},"analyzer": {
"turkish_analyzer": {
"tokenizer": "standard","filter": [
"lowercase","my_ascii_folding"
]
}
}
}
},"mappings": {
"test": {
"properties": {
"name": {
"type": "string","analyzer": "turkish_analyzer"
}
}
}
}
可以吗?
解决方法
当然,您可以将配置作为 index.create body
参数传递。
// create a new index
loadIndexclient.indices.create({
index: indexNameLoading,body: {
"settings": {
"analysis": {
"filter": {
"my_ascii_folding": {
"type": "asciifolding","preserve_original": true
}
},"analyzer": {
"turkish_analyzer": {
"tokenizer": "standard","filter": [
"lowercase","my_ascii_folding"
]
}
}
}
},"mappings": {
"properties": {
"name": {
"type": "string","analyzer": "turkish_analyzer"
}
}
}
}
},function (error,response,status) {
if (error) {
console.log(error);
}
else {
console.log("created a new index",response);
}
});
,
执行此操作的理想方法是使用 index_templates
。请参阅文档 here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。