Elasticsearch: создать индекс с сопоставлениями с помощью javascript
Я пытаюсь создать индекс elasticsearch с сопоставлениями с использованием официального 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
Что я делаю неправильно?
Ответы
Ответ 1
Уточнение ответа, приведенного в комментарии выше от Сандера Спиллемана. Свойство "сопоставления" должно находиться внутри свойства "тело". Я также использую Javascript client 1.3.0, и документы все еще не обновляются с примером.
Добавление примера, который работал у меня с API-интерфейсом javascript, предоставленным elasticsearch на NPM 1.3.0
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});
Ответ 2
Я попробовал то же самое, но получил ошибку от имени индекса. 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
}
![введите описание изображения здесь]()
Ответ 3
Просто добавьте тело вокруг отображений:
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);
});
Ответ 4
Ни один из этих примеров не работал у меня в 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/