《Elastic Stack 实战手册》——三、产品能力——3.5 进阶篇——3.5.19.Elasticsearch语言开发(Python/Nodejs/Java)——3.5.19.2.Elasticsearch语言开发(Node.js)(中) https://developer.aliyun.com/article/1226589
3. Mapping API
Mapping 主要作用如下:
l 定义索引中的字段名称
l 定义字段的类型,例如数值、字符串、布尔类型等
l 定义倒排索引的相关配置,例如是否索引、记录 position 信息等。
下面示例是在空索引上定义了其 Mapping:
client.indices.putMapping({ index: 'author-index', // 默认即为 false,因 type 在 ES 7.0 被废弃 // 另外在 ES 8.0 中 include_type_name 也将被废弃 include_type_name: false, ignore_unavailable: true, body: { properties: { author: { type: 'keyword' }, article: { type: 'text' }, age: { type: 'integer' }, interests: { type: 'text', fielddata: true }, createTime: { type: 'date' }, }, }, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error);
4. Bulk API
Bulk API 接受索引、创建、删除和更新操作,下面以批量创建为例:
const dataset = [ { author: 'Tom', article: 'All time is no time when it is past.', age: 13, interests: ['sports', 'painting'], createTime: '2020-01-12',}, { author: 'Mary', article: 'No one can call back yesterday;Yesterday will not be called again.', age: 15, interests: ['sports', 'painting', 'game', 'music'], createTime: '2021-03-04', }, { author: 'David', article: 'Punctuality is the soul of business.', age: 9, interests: ['game'], createTime: '2020-09-22', }, ]; const bulkBody = dataset.flatMap((doc, index) => [ { index: { _index: 'author-index', _type: '_doc', _id: index + 1 } }, doc, ]); client .bulk({ index: 'author-index', type: '_doc', // 立即刷新数据提高搜索的实时性,缺点是消耗资源较高 refresh: true, body: bulkBody, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error);
5. Update API
使用指定脚本更新文档,下面代码展示了对 id 为 0 的文档的 age 字段执行 +1 操作:
client .update({ index: 'author-index', id: 0, refresh: true, body: { script: { source: 'ctx._source.age++', lang: 'painless', }, }, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) \ .catch(console.error);
6. UpdateByQuery API
下面代码展示了对 author 为 Mary 执行更新操作:
client .updateByQuery({ index: 'author-index', refresh: true, body: { script: { source: 'ctx._source.age++', lang: 'painless', }, query: { match: { author: 'Mary', }, }, }, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error);
7. Search API
查询 article field 中带有 time 关键字的结果:
client .search({ index: 'author-index', body: { query: { match: { article: 'time', }, }, }, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error); // search result { "took":1, "timed_out":false, "_shards":{ "total":1, "successful":1, "skipped":0, "failed":0 }, "hits":{ "total":{ "value":2, "relation":"eq" }, "max_score":0.9763484, "hits":[ { "_index":"author-index", "_type":"_doc", "_id":"1", "_score":0.9763484, "_source":{ "author":"Tom", "article":"All time is no time when it is past.", "age":13, "interests":[ "sports", "painting" ], "createTime":"2020-01-12" } }, { "_index":"author-index", "_type":"_doc", "_id":"0", "_score":0.94855565, "_source":{ "author":"John", "article":"Everything has its time and that time must be watched.", "age":11, "interests":[ "sports", "music" ], "createTime":"2021-11-15" } } ] } }
8. Aggregate API
下面代码展示了聚合 interests 信息的操作:
client .search({ index: 'author-index', body: { aggs: { all_interests: { terms: { field: 'interests' }, }, }, }, }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error); // search result { "took":3, "timed_out":false, "_shards":{ "total":1, "successful":1, "skipped":0, "failed":0 }, "hits":{ "total":{ "value":4, "relation":"eq" }, "max_score":1, "hits":[ { "_index":"author-index", "_type":"_doc", "_id":"1", "_score":1, "_source":{ "author":"Tom", "article":"All time is no time when it is past.", "age":13, "interests":[ "sports", "painting" ], "createTime":"2020-01-12" } }, { "_index":"author-index", "_type":"_doc", "_id":"3", "_score":1, "_source":{ "author":"David", "article":"Punctuality is the soul of business.", "age":9, "interests":[ "game" ], "createTime":"2020-09-22" } }, { "_index":"author-index", "_type":"_doc", "_id":"0", "_score":1, "_source":{ "author":"John", "article":"Everything has its time and that time must be watched.", "age":11, "interests":[ "sports", "music" ], "createTime":"2021-11-15" } }, { "_index":"author-index", "_type":"_doc", "_id":"2", "_score":1, "_source":{ "createTime":"2021-03-04", "author":"Mary", "interests":[ "sports", "painting", "game", "music" ], "article":"No one can call back yesterday;Yesterday will not be called again.", "age":16 } } ] }, "aggregations":{ "all_interests":{ "doc_count_error_upper_bound":0, "sum_other_doc_count":0, "buckets":[ { "key":"sports", "doc_count":3 }, { "key":"game", "doc_count":2 }, { "key":"music", "doc_count":2 }, { "key":"painting", "doc_count":2 } ] } } }
9. Delete Index API
下面展示了删除索引:
client.indices .delete({ index: 'author-index', }) .then(({ body }) => { console.log(JSON.stringify(body)); }) .catch(console.error);
五、github 代码
完整的 API 示例代码请参考:https://github.com/jkhhuse/elasticsearch-nodejs
参考资料
l https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html
创作人简介:
姜康,一名会点 vue、Angular、React、Node.js、Java、ELK 的伪全栈前端工程师,目前正在掌握数据开发技术栈的途中。