我想索引一个包含世界城市名称的.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"
},
}, function (error, response, 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"
}
}
}
}有可能做到吗?
发布于 2021-03-04 09:50:45
当然,您可以将配置作为indices.create body param传递。
文档:创建
// 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);
}
});发布于 2021-03-24 13:50:19
实现这一点的理想方法是使用index_templates。参考文档这里
https://stackoverflow.com/questions/66472389
复制相似问题