首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在创建索引时添加设置和映射到索引

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

Stack Overflow用户
提问于 2021-03-04 09:42:31
回答 2查看 552关注 0票数 0

我想索引一个包含世界城市名称的.json文件。在创建索引时,我有我的自定义设置和映射。我的代码是:

代码语言:javascript
复制
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);
    }
});

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

代码语言:javascript
复制
"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"
        }
      }
    }
  }

有可能做到吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-04 09:50:45

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

文档:创建

代码语言:javascript
复制
// 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);
    }
});
票数 1
EN

Stack Overflow用户

发布于 2021-03-24 13:50:19

实现这一点的理想方法是使用index_templates。参考文档这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66472389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档