如何在创建索引时将设置和映射添加到索引

How to add settings and mapping to an index while creating it

我想索引包含世界城市名称的 .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);
    }
});

当我 运行 这段代码时,它实际上 运行s 并创建了索引,但映射和设置是默认创建的。但是我想在创建索引时添加以下设置和映射。

"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"
        }
      }
    }
  }

可以吗?

当然,您可以将配置作为 indices.create body 参数传递。

文档:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_indices_create

// 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