illegal_argument_exception请求包含无法识别的参数: – ElasticSearch索引创build

自从昨天以来,我第一次使用elasticsearch,并且由于我的知识有限,在两天的工作之后,我正在努力获得简单的function。

我的主要目标是用NodeJs + ElasticSearch完成一个糟糕的工作。 现在我坚持通过使用“映射”function创build索引。

我直接的问题是:为了使这个代码有效,我必须做些什么?

return client.indices.create({ index: 'index_created_with_map', mapping: { posts: { user: { type: 'string' }, post_date: { type: 'string' }, message: { type: 'string' } } } }); 

任何build议什么检查将不胜感激。

另外,虽然不是我主要问题的一部分,但是如何将get和search函数正确地返回给“response.send(JSON.stringify(来自elasticsearch的数据)”)以及如何将“post_date”映射到datetypesstring将不胜感激,因为我卡在它。

贝娄是我迄今为止所尝试过的。 我可以看到抛出“Chrome ElastiSearch工具箱扩展”,当我尝试没有“映射”function像addToIndex下面它的工作,但我想有单独的function,一个用于创build索引,我将只运行一次明显和另一个创build“logging”将成为我的一部分。

PS。 我在这里发现了非常类似的问题,没有任何答案

illegal_argument_exception:没有为字段find映射

错误日志:

 Unhandled rejection Error: [illegal_argument_exception] request [/index_created_with_map] contains unrecognized parameter: [mapping] at respond (/home/demetrio/dev/WSs/NodeJs/greencard-dmz-es-oracle/node_modules/elasticsearch/src/lib/transport.js:289:15) at checkRespForFailure (/home/demetrio/dev/WSs/NodeJs/greencard-dmz-es-oracle/node_modules/elasticsearch/src/lib/transport.js:248:7) at HttpConnector.<anonymous> (/home/demetrio/dev/WSs/NodeJs/greencard-dmz-es-oracle/node_modules/elasticsearch/src/lib/connectors/http.js:164:7) at IncomingMessage.wrapper (/home/demetrio/dev/WSs/NodeJs/greencard-dmz-es-oracle/node_modules/lodash/lodash.js:4968:19) at emitNone (events.js:72:20) at IncomingMessage.emit (events.js:166:7) at endReadableNT (_stream_readable.js:905:12) at nextTickCallbackWith2Args (node.js:441:9) at process._tickCallback (node.js:355:17) 

我的控制器NodeJs

 var elasticsearch = require('elasticsearch'); var Promise = require('bluebird'); exports.teste = function (req, res) { var log = console.log.bind(console); var client = new elasticsearch.Client({ host: 'localhost:9200', log: 'trace' }); function createIndexWithMapping() { return client.indices.create({ index: 'index_created_with_map', mapping: { posts: { user: { type: 'string' }, post_date: { type: 'string' }, message: { type: 'string' } } } }); } function createIndexWithoutMapping() { return client.create({ index: 'index_created_without_map', type: 'posts', id: '1', body: { user: 'me', post_date: new Date(), message: 'Hello World!' }, refresh: true }); } function addToIndex() { return client.index({ index: 'index_created_...according to the test', type: 'posts', id: '1', body: { user: 'me2', post_date: new Date(), message: 'Hello World!2' }, refresh: true }); } function search() { return client.search({ index: 'index_created_...according to the test', type: 'posts', body: { query: { match: { body: 'Hello' } } } }).then(log); } function getFromIndex() { return client.get({ index: 'index_created_...according to the test', type: 'posts', id: 1 }).then(log); } function closeConnection() { client.close(); } Promise.resolve() .then(createIndexWithMapping) //.then(createIndexWithoutMapping) // .then(addToIndex) // .then(search) // .then(getFromIndex) .then(closeConnection); return res.send("a"); }; 

的package.json

 { "name": "my-first-elasticsearch-app", "main": "server.js", "dependencies": { "bcrypt-nodejs": "0.0.3", "body-parser": "^1.0.2", "ejs": "^1.0.0", "elasticsearch": "^12.1.3", "express": "^4.1.1", "express-session": "^1.6.1", "mongoose": "^3.8.8", "node-rest-client": "^2.5.0", "oauth2orize": "^1.0.1", "passport": "^0.2.0", "passport-http": "^0.2.2", "passport-http-bearer": "^1.0.1", "reqclient": "^2.1.0" } } 

根据elasticsearch.js 5.5 API文档

client.indices.create([params,[callback]])

PARAMS

  • 身体

对象,JSON – 一个可选的请求体,可以是JSON或JSON序列化对象。 有关可在此处指定的内容的详细信息,请参阅elasticsearch文档。

根据API Convension doc

通用参数

  • 身体

string,任何东西 – 与此请求一起发送的正文。 如果主体是一个string,它将按原样传递,否则传递给序列化器,并根据API方法转换为JSON或换行符分隔的JSON对象列表。

所以你应该发送mapping属性作为body属性内的请求主体

一个工作的例子:

 const es = require('elasticsearch'); const elasticsearchClient = new es.Client({ host: 'localhost:9200', log: 'trace', }); elasticsearchClient.indices.create({ index: `index_created_with_map`, body: { mappings: { type_name: { // ... }, }, } });