[英]elasticsearch: create index with mappings using javascript
我正在尝试使用官方 javascript 客户端创建一个带有映射的弹性搜索索引。
我的代码如下:
client.indices.create({
index: "aName",
"mappings": {
"aType": {
"properties": {
"aProp1": { "type": "string", "index": "not_analyzed" },
"aProp2": { "type": "string", "index": "not_analyzed" },
"aProp3": { "type": "string", "index": "not_analyzed" },
"aProp4": { "type": "string", "index": "not_analyzed" }
}
}
}
}, function(err,resp,respcode){
console.log(err,resp,respcode);
});
但是......创建了索引但没有映射......输出是:
undefined { ok: true, acknowledged: true } 200
我究竟做错了什么?
完善上述 Sander Spilleman 评论中的答案。 “mappings”属性需要在“body”属性内。 我也在使用 Javascript 客户端 1.3.0 并且文档仍然没有更新示例。
在 NPM 1.3.0 上使用 elasticsearch 提供的 javascript API 添加一个对我有用的示例
var body = {
tweet:{
properties:{
tag : {"type" : "string", "index" : "not_analyzed"},
type : {"type" : "string", "index" : "not_analyzed"},
namespace : {"type" : "string", "index" : "not_analyzed"},
tid : {"type" : "string", "index" : "not_analyzed"}
}
}
}
client.indices.putMapping({index:"tweets", type:"tweet", body:body});
我尝试了同样的方法,但从索引名称中得到了错误。 aName 无效,错误与使用小写索引名称有关。 然后它使用映射创建。
it.only('putMapping', function (done) {
client.indices.create({
index: "aname",
body: {
"mappings": {
"aType": {
"properties": {
"aProp1": {"type": "string", "index": "not_analyzed"},
"aProp2": {"type": "string", "index": "not_analyzed"},
"aProp3": {"type": "string", "index": "not_analyzed"},
"aProp4": {"type": "string", "index": "not_analyzed"}
}
}
}
}
}, function (err, resp, respcode) {
console.log(err, resp, respcode);
});
})
输出:
Elasticsearch DEBUG: 2015-08-08T15:23:09Z
starting request { method: 'POST',
path: '/aname',
body: { mappings: { aType: [Object] } },
query: {} }
Elasticsearch TRACE: 2015-08-08T15:23:10Z
-> POST http://localhost:9200/aname
{
"mappings": {
"aType": {
"properties": {
"aProp1": {
"type": "string",
"index": "not_analyzed"
},
"aProp2": {
"type": "string",
"index": "not_analyzed"
},
"aProp3": {
"type": "string",
"index": "not_analyzed"
},
"aProp4": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
<- 200
{
"acknowledged": true
}
只需在映射周围添加正文:
client.indices.create({
index: "aName",
body: {
"mappings": {
"aType": {
"properties": {
"aProp1": { "type": "string", "index": "not_analyzed" },
"aProp2": { "type": "string", "index": "not_analyzed" },
"aProp3": { "type": "string", "index": "not_analyzed" },
"aProp4": { "type": "string", "index": "not_analyzed" }
}
}
}
}
}, function (err, resp, respcode) {
console.log(err, resp, respcode);
});
这些示例都不适用于 ElasticSearch 5.3 API。
这是一个适用于 5.3 的示例。
elasticClient.indices.putMapping({
index: indexName,
type: "document",
body: {
properties: {
title: { type: "string" },
content: { type: "string" },
suggest: {
type: "completion",
analyzer: "simple",
search_analyzer: "simple",
payloads: true
}
}
}
})
请注意,该类型已从主体中拉出,只有该类型下的子参数现在在主体中。
来源: https ://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/
注意:这使用client.indices.create()
而不是client.indices.putMapping()
我最近成功地使用这样的自定义映射创建了一个索引:
client.indices.create({
index: 'yourIndex',
body: {
yourIndex: {
mappings: {
yourType: {
properties: {
yourText: {
type: 'string',
}
}
}
}
}
}
});
似乎您必须开始使用索引定义正文,然后是mappings
关键字,然后是您的类型等等。 我使用弹性搜索包版本15.4.1
和弹性版本6.5.4
Body 属性在8.2.1
版本之后被弃用。 映射应该直接在参数中设置。
await client.indices.create({
index: "user",
mappings: {
properties: {
"name": {
type: "text",
index: true
},
"surname": {
type: "text",
index: true
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.