/*链接到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))
});