npm i -S elasticsearch
const elasticsearch = require('elasticsearch');
//没有设置账号密码
host:http://ip:port
//设置了账号密码
host:http://user:password@ip:port
//单节点
const client = new elasticsearch.Client({
host: 'http://test:test@127.0.0.1:9500',
log: 'trace',
apiVersion: '7.6', //使用与Elasticsearch实例相同的版本
});
//集群
const client = new elasticsearch.Client({
hosts: [
'https://user:pass@box1.server.org:9200',
'https://user:pass@box2.server.org:9200'
]
});
//需要指定id
await client.create({
index: 'database', //相当于数据库
type: 'datasheet', //相当于数据表
id: '1', //id,必须,且不能重复
body: { //添加内容
test:'add...'
}
});
//无需要指定id
await client.index({
index: 'database',
type: 'datasheet',
body: {
character: 'Daenerys Targaryen',
quote: 'I am the blood of the dragon.'
}
})
//根据id获取数据
const response = await client.get({
index: 'myindex',
type: 'mytype',
id: 1
});
//mget,支持同时获取多个
const response = await client.mget({
body: {
docs: [
{ _index: 'indexA', _type: 'typeA', _id: '1' },
{ _index: 'indexB', _type: 'typeB', _id: '1' },
{ _index: 'indexC', _type: 'typeC', _id: '1' }
]
}
} )
//mget,支持同时获取多个ids
const response = await client.mget({
index: 'myindex',
type: 'mytype',
body: {
ids: [1, 2, 3]
}
});
await client.delete({
index: 'database',
type: 'datasheet',
id: '1'
});
//删除数组里的一个对象
await client.update({
index: 'rest',
id: '1v2zG34BnY7le6vibRiv',
body: {
script : {
inline: "ctx._source.arr.removeIf(item -> item.name == '邓紫棋')"
}
}
})
//会清空之前的数据,然后重新插入,相当于重写
await client.index({
index: 'database',
type: 'datasheet',
id: '1',
body: {
title: 'cascsa'
}
});
//修改
await client.update({
index: 'database',
type: 'datasheet',
id: '1',
body: {
title: 'cascsa'
}
});
//追加
//字符串追加
const response = await client.update({
index: 'database',
type: 'datasheet',
id: '1',
body: {
script: 'ctx._source.title += title',
params: { title: '字符串追加内容' }
}
});
//数组追加
await client.update({
index: 'rest',
id: '1v2zG34BnY7le6vibRiv',
body: {
script : {
inline: "ctx._source.arr.add(params.item)", //还有方法indexOf、contains、remove、removeIf
params: {
item: {
name:'阿细',
title:'一双手'
}
}
}
}
})
//数组对象修改
await client.update({
index: 'rest',
id: '1v2zG34BnY7le6vibRiv',
body: {
script : {
inline: "for(e in ctx._source.arr){if (e.name == '阿细') {e.name=params.name}}",
params:{
name:'邓紫棋'
}
}
}
})
//更新数据,不会清空数据
await client.update({
index: 'database',
type: 'datasheet',
id: '6',
body: {
doc: {
title: 'test',
tags:'51213'
}
}
})
//模糊搜索
const response = await client.search({
index: 'database',
type: 'datasheet',
body:{
query: {
match: {
title:'测试' //搜索title中,以测试为开头的title
}
}
}
});
match: {
title:'*测试*' //搜索title中包含测试的字段,模糊搜索,*为占位符
}
//获取全部数据
const response = await client.search({
index: 'database',
type: 'datasheet',
body:{
query: {
match_all: {}
}
}
})
//filter,过滤
const response = await client.search({
index: 'database',
type: 'datasheet',
body:{
query: {
bool:{
filter:{
range: {
count: {
gte:90 // 大于等于
}
}
}
}
},
}
})
gt
大于
gte
大于等于
lt
小于
lte
小于等于
// must有点类似and关系
const response = await client.search({
index: 'database',
type: 'datasheet',
body:{
query: {
bool:{
must:[
{
match:{
count:86 //count为86
}
},
{
match:{
test:'*试*' //test包含'试'
}
}
]
}
},
}
})
//should类似or的关系
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
query: {
bool:{
should:[
{
match:{
count:100 //count为100
}
},
{
match:{
test: 'title' //test包含'title'
}
}
]
}
},
}
})
//must_not类似not
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
query: {
bool:{
must_not:[
{
match:{
count:86
}
},
{
match:{
title: '745',
}
}
]
}
},
}
})
//term使用的是精确匹配
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
query: {
bool:{
must_not:[
{
term:{
title: 'test'
}
},
{
term:{
title: '745',
}
},
{
term:{
title: 'mysql',
}
}
]
}
},
}
})
//term使用的是精确匹配
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
query: {
term:{
title: 'mysql'
}
}
}
})
//terms和term一样也是精确匹配
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
query: {
terms:{
title:['mysql','745'] //如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件
}
},
}
})
//sort排序
const response = await client.search({
index: 'databases',
type: 'datasheet',
size: 100,
from: 0,
body:{
sort: {
"_id": {
"order": "desc"
}
}
}
})
desc 倒序
asc 升序
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 30, //截取条数
from: 0, //从哪里开始截取,不包括当前位置
body:{
query: {
match_all: {}
}
}
})
//{ demo:{name:'邓紫棋',title:'光年之外'} },从对象的搜索过滤
const response = await client.search({
index: 'rest02',
body:{
query: {
match:{
"demo.name": "邓紫棋"
}
}
}
})
// [{name:'邓紫棋',title:'光年之外'}],丛数组对象里搜索过滤
const response = await client.search({
index: 'rest',
body:{
query: {
nested: {
path: "arr",
query: {
match: {
"arr.name": "邓紫棋"
}
}
}
}
}
})
//_source
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
_source:['title'] //返回的结果字段里,只有title字段
}
})
const response = await client.search({
index: 'database',
type: 'datasheet',
size: 100,
from: 0,
body:{
_source:{
includes:['title','test'], //返回的结果包含得字段
excludes:['test'] //返回的结果不包含的字段
}
}
})
//获取这个节点的数量
const { count } = await client.count()
//获取指定索引的数量
const { count } = await client.count({
index: 'index_name'
})
await client.exists({
index: 'database',
type: 'datasheet',
id: 1
});
const response = await client.msearch({
body: [
{ index: 'database', type: 'datasheet'},
{ query: { match_all: {} } },
{ index: 'databases', type: 'datasheet'},
{ query: { match_all: {} } }
]
})
client.bulk({
body: [
{ index: { _index: 'myindex', _type: 'mytype', _id: 1 } },
{ title: 'foo' },
{ update: { _index: 'myindex', _type: 'mytype', _id: 2 } },
{ doc: { title: 'foo' } },
{ delete: { _index: 'myindex', _type: 'mytype', _id: 3 } },
]
})
字符串类型:text、keyword
数值型:long、integer、short、byte、double、float、half、scaled
日期型:date
布尔型:boolean
二进制型:binary
对象:object
数组对象:nested
等等...
await client.indices.create({
index:'rest',
body:{
settings : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
mappings : {
properties : {
title : {
type : "text",
analyzer: "ik_max_word",
search_analyzer:"ik_max_word"
},
demo: { //demo:{name:'',title:''}
type:'object',
properties: {
"name": { type: "text" },
"title": { type: "text" }
}
},
arr: { //arr:[{name:'',age:0}]
type: "nested",
properties: {
name:{ type: "text" },
age:{ type: "long" }
}
},
age : {
type : "long"
},
log_time : {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
})
const response = await client.indices.getMapping({
index: 'rest'
})
//往最外层追加字段
await client.indices.putMapping({
index: 'rest',
body: {
properties:{
demo_test:{
type:'text'
}
}
}
})
//往数组对象里追加字段
await client.indices.putMapping({
index: 'rest',
body: {
properties:{
arr: {
type: "nested",
properties: {
test:{ type: "text" }
}
}
}
}
})
//往对象里追加字段
await client.indices.putMapping({
index: 'rest',
body: {
properties:{
demo:{
type:'object',
properties: {
text: { type: "text" }
}
}
}
}
})
client.indices.delete({
index:'test'
})
由于type属性已经在7.0+版本废除,所以以上的type可以没有,本文档借鉴 https://github.com/elastic/elasticsearch-js-legacy/blob/16.x/docs/api_methods_7_5.asciidoc