nodejs elasticsearch基础使用

来源:互联网 发布:电脑pe手动备份数据 编辑:程序博客网 时间:2024/06/05 21:14
*链接到elasticsearch数据库*var elasticsearch = require('elasticsearch');var client = new elasticsearch.Client({  host: 'localhost:9200',  log: 'trace'});
  1. 插入数据
client.index({  index: 'myindex', //相当于database  type: 'mytype2',  //相当于table  id: JSON.stringify(new Date().getTime()),// 数据到唯一标示,id存在则为更新,不存在为插入  body: {    title: 'Test 1',    tags: ['y', 'z'],    published: true,    published_at: '2013-01-01',    counter: 1,    name: '999'  }//文档到内容}, (error, response)=>{  //   console.log(error)  console.log(response)});

删除数据:

 client.delete({   index: 'myindex',   type: 'mytype',   id: '3' }, (error, response)=>{   // ...删除id为3的数据 });

查询数据:

client.search({  index: 'myindex',  type:'mytype',  body:{    query:{      // match:{      //   name:'yyk'      // }      "terms" :{           "_id" : ["6"]           }    }  }}, function (error, response) {  // ...  response.hits.hits.map((v)=>console.log(v))});
原创粉丝点击