Elasticsearch Javascript API增删改查

妖狐艹你老母 2022-11-17 05:17 298阅读 0赞

查询

根据索引、类型、id进行查询:

  1. client.get({
  2. index:'myindex',
  3. type:'mytype',
  4. id:1
  5. },function(error, response){// ...});

根据某个查询条件,查询某个索引的所有数据

  1. client.search({
  2. index:'myindex',
  3. q:'title:test'
  4. },function(error, response){// ...});

复杂一点的查询:

  1. client.search({
  2. index:'myindex',
  3. body:{
  4. query:{
  5. match:{
  6. title:'test'
  7. }
  8. },
  9. facets:{
  10. tags:{
  11. terms:{
  12. field:'tags'
  13. }
  14. }
  15. }
  16. }
  17. },function(error, response){// ...});

新增

新增时,需要指定索引,类型,和id,还有保存的内容:

  1. client.create({
  2. index:'myindex',
  3. type:'mytype',
  4. id:'1',
  5. body:{
  6. title:'Test 1',
  7. tags:['y','z'],
  8. published:true,
  9. published_at:'2013-01-01', counter:1
  10. }
  11. },function(error, response){// ...});

删除

按照索引,类型和id删除:

  1. client.delete({
  2. index:'myindex',
  3. type:'mytype',
  4. id:'1'
  5. },function(error, response){// ...});

修改

修改操作通常使用update方法:

  1. client.update({
  2. index:'myindex',
  3. type:'mytype',
  4. id:'1',
  5. body:{
  6. // put the partial document under the `doc` key
  7. doc:{
  8. title:'Updated'
  9. }
  10. }
  11. },function(error, response){// ...})

一次性执行多个操作

ESClient也支持一次性执行多个操作:

  1. client.mget({
  2. body:{
  3. docs:[ {
  4. _index:'indexA', _type:'typeA', _id:'1'
  5. },{
  6. _index:'indexB', _type:'typeB', _id:'1'
  7. },{
  8. _index:'indexC', _type:'typeC', _id:'1'
  9. }]
  10. }
  11. },function(error, response){// ...});

也支持下面的风格:

  1. client.mget({
  2. index:'myindex',
  3. type:'mytype',
  4. body:{ ids:[1,2,3]}
  5. },function(error, response){// ...});

类似的也可以同时执行多个查询:

  1. client.msearch({
  2. body:[
  3. // match all query, on all indices and types
  4. {},
  5. { query:{ match_all:{}}},
  6. // query_string query, on index/mytype
  7. {
  8. _index:'myindex',
  9. _type:'mytype'
  10. },{
  11. query:{
  12. query_string:{ query:'"Test 1"'}
  13. }
  14. }]
  15. });

扩展

通过上面基本API的使用,基本可以了解js端对ESclient的操作。当然也可以使用下面的变成风格调用方法:

  1. es[method](params)
  2. 它类似
  3. es.method(params,回调方法)

在kibana中的_doc_send_to_es.js,使用了如下的封装:

  1. function (method, validateVersion, body, ignore) {
  2. // debugger;
  3. var doc = this;
  4. // straight assignment will causes undefined values
  5. var params = _.pick(this._state, ['id', 'type', 'index']);
  6. params.body = body;
  7. params.ignore = ignore || [409];
  8. if (validateVersion && params.id) {
  9. params.version = doc._getVersion();
  10. }
  11. // debugger;
  12. return es[method](params)
  13. .then(function (resp) {
  14. // debugger;
  15. if (resp.status === 409) throw new errors.VersionConflict(resp);
  16. doc._storeVersion(resp._version);
  17. doc.id(resp._id);
  18. var docFetchProm;
  19. if (method !== 'index') {
  20. docFetchProm = doc.fetch();
  21. } else {
  22. // we already know what the response will be
  23. docFetchProm = Promise.resolve({
  24. _id: resp._id,
  25. _index: params.index,
  26. _source: body,
  27. _type: params.type,
  28. _version: doc._getVersion(),
  29. found: true
  30. });
  31. }
  32. // notify pending request for this same document that we have updates
  33. docFetchProm.then(function (fetchResp) {
  34. // use the key to compair sources
  35. var key = doc._versionKey();
  36. // clear the queue and filter out the removed items, pushing the
  37. // unmatched ones back in.
  38. var respondTo = requestQueue.splice(0).filter(function (req) {
  39. var isDoc = req.source._getType() === 'doc';
  40. var keyMatches = isDoc && req.source._versionKey() === key;
  41. debugger;
  42. // put some request back into the queue
  43. if (!keyMatches) {
  44. requestQueue.push(req);
  45. return false;
  46. }
  47. return true;
  48. });
  49. return courierFetch.fakeFetchThese(respondTo, respondTo.map(function () {
  50. return _.cloneDeep(fetchResp);
  51. }));
  52. });
  53. return resp._id;
  54. })
  55. .catch(function (err) {
  56. // cast the error
  57. throw new errors.RequestFailure(err);
  58. });
  59. };

因此使用时,又变成了:

  1. xxx.call(this, 'create', false, body, []);

一层一层封装了很多,但是只要慢慢屡清除,就知道怎么使用了。

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Elasticsearch增删

    Elasticsearch的增删改查: 面向文档 document数据格式 1. 应用系统的数据结构都是面向对象的,复杂的 2. 对象数据存储到数据库中,只能拆

    相关 Elasticsearch增删

    面向文档 document数据格式 1. 应用系统的数据结构都是面向对象的,复杂的 2. 对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得