欢迎投稿

今日深度:

nodejs elasticsearch基础使用,

nodejs elasticsearch基础使用,


*链接到elasticsearch数据库*var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});
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))
});

www.htsjk.Com true http://www.htsjk.com/Elasticsearch/26000.html NewsArticle nodejs elasticsearch基础使用, / * 链接到elasticsearch数据库 * / var elasticsearch = require ( 'elasticsearch' ); var client = new elasticsearch . Client({ host: 'localhost:9200' , log : 'trace' }); client . index ({ index :...
相关文章
    暂无相关文章
评论暂时关闭